using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Download.Clients.RTorrent;
using NzbDrone.Core.Test.Framework;

namespace NzbDrone.Core.Test.Datastore.Migration
{
    [TestFixture]
    public class cdh_per_downloadclientFixture : MigrationTest<cdh_per_downloadclient>
    {
        [Test]
        public void should_set_cdh_to_enabled()
        {
            var db = WithMigrationTestDb(c =>
            {
                c.Insert.IntoTable("DownloadClients").Row(new
                {
                    Enable = true,
                    Name = "Deluge",
                    Implementation = "Deluge",
                    Priority = 1,
                    Settings = new DelugeSettings85
                    {
                        Host = "127.0.0.1",
                        MovieCategory = "abc",
                        UrlBase = "/my/"
                    }.ToJson(),
                    ConfigContract = "DelugeSettings"
                });
            });

            var items = db.Query<DownloadClientDefinition158>("SELECT * FROM \"DownloadClients\"");

            items.Should().HaveCount(1);
            items.First().RemoveCompletedDownloads.Should().BeFalse();
            items.First().RemoveFailedDownloads.Should().BeTrue();
        }

        [Test]
        public void should_set_cdh_to_disabled_when_globally_disabled()
        {
            var db = WithMigrationTestDb(c =>
            {
                c.Insert.IntoTable("Config").Row(new
                {
                    Key = "removecompleteddownloads",
                    Value = "True"
                });

                c.Insert.IntoTable("DownloadClients").Row(new
                {
                    Enable = true,
                    Name = "Deluge",
                    Implementation = "Deluge",
                    Priority = 1,
                    Settings = new DelugeSettings85
                    {
                        Host = "127.0.0.1",
                        MovieCategory = "abc",
                        UrlBase = "/my/"
                    }.ToJson(),
                    ConfigContract = "DelugeSettings"
                });
            });

            var items = db.Query<DownloadClientDefinition158>("SELECT * FROM \"DownloadClients\"");

            items.Should().HaveCount(1);
            items.First().RemoveCompletedDownloads.Should().BeTrue();
            items.First().RemoveFailedDownloads.Should().BeTrue();
        }

        [Test]
        public void should_disable_remove_for_existing_rtorrent()
        {
            var db = WithMigrationTestDb(c =>
            {
                c.Insert.IntoTable("DownloadClients").Row(new
                {
                    Enable = true,
                    Name = "RTorrent",
                    Implementation = "RTorrent",
                    Priority = 1,
                    Settings = new RTorrentSettings
                    {
                        Host = "127.0.0.1",
                        MovieCategory = "abc",
                        UrlBase = "/my/"
                    }.ToJson(),
                    ConfigContract = "RTorrentSettings"
                });
            });

            var items = db.Query<DownloadClientDefinition158>("SELECT * FROM \"DownloadClients\"");

            items.Should().HaveCount(1);
            items.First().RemoveCompletedDownloads.Should().BeFalse();
            items.First().RemoveFailedDownloads.Should().BeTrue();
        }
    }

    public class DownloadClientDefinition158
    {
        public int Id { get; set; }
        public bool Enable { get; set; }
        public int Priority { get; set; }
        public string Name { get; set; }
        public string Implementation { get; set; }
        public JsonElement Settings { get; set; }
        public string ConfigContract { get; set; }
        public bool RemoveCompletedDownloads { get; set; }
        public bool RemoveFailedDownloads { get; set; }
    }

    public class DelugeSettings85
    {
        public string Host { get; set; }
        public int Port { get; set; }
        public string UrlBase { get; set; }
        public string Password { get; set; }
        public string MovieCategory { get; set; }
        public int RecentTvPriority { get; set; }
        public int OlderTvPriority { get; set; }
        public bool UseSsl { get; set; }
    }
}
