using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Processes;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.HealthCheck;
using NzbDrone.Core.ThingiProvider;
using NzbDrone.Core.Validation;

namespace NzbDrone.Core.Notifications.CustomScript
{
    public class CustomScript : NotificationBase<CustomScriptSettings>
    {
        private readonly IConfigFileProvider _configFileProvider;
        private readonly IConfigService _configService;
        private readonly IDiskProvider _diskProvider;
        private readonly IProcessProvider _processProvider;
        private readonly Logger _logger;

        public CustomScript(IConfigFileProvider configFileProvider,
            IConfigService configService,
            IDiskProvider diskProvider,
            IProcessProvider processProvider,
            Logger logger)
        {
            _configFileProvider = configFileProvider;
            _configService = configService;
            _diskProvider = diskProvider;
            _processProvider = processProvider;
            _logger = logger;
        }

        public override string Name => "Custom Script";

        public override string Link => "https://wiki.servarr.com/prowlarr/settings#connections";

        public override ProviderMessage Message => new("Testing will execute the script with the EventType set to Test, ensure your script handles this correctly", ProviderMessageType.Warning);

        public override void OnGrab(GrabMessage message)
        {
            var environmentVariables = new StringDictionary();

            environmentVariables.Add("Prowlarr_EventType", "Grab");
            environmentVariables.Add("Prowlarr_InstanceName", _configFileProvider.InstanceName);
            environmentVariables.Add("Prowlarr_ApplicationUrl", _configService.ApplicationUrl);
            environmentVariables.Add("Prowlarr_Release_Title", message.Release.Title);
            environmentVariables.Add("Prowlarr_Release_Indexer", message.Release.Indexer ?? string.Empty);
            environmentVariables.Add("Prowlarr_Release_Size", message.Release.Size.ToString());
            environmentVariables.Add("Prowlarr_Release_Genres", string.Join("|", message.Release.Genres));
            environmentVariables.Add("Prowlarr_Release_Categories", string.Join("|", message.Release.Categories.Select(f => f.Name)));
            environmentVariables.Add("Prowlarr_Release_IndexerFlags", string.Join("|", message.Release.IndexerFlags.Select(f => f.Name)));
            environmentVariables.Add("Prowlarr_Release_PublishDate", message.Release.PublishDate.ToUniversalTime().ToString("s") + "Z");
            environmentVariables.Add("Prowlarr_Download_Client", message.DownloadClientName ?? string.Empty);
            environmentVariables.Add("Prowlarr_Download_Client_Type", message.DownloadClientType ?? string.Empty);
            environmentVariables.Add("Prowlarr_Download_Id", message.DownloadId ?? string.Empty);
            environmentVariables.Add("Prowlarr_Source", message.Source ?? string.Empty);
            environmentVariables.Add("Prowlarr_Host", message.Host ?? string.Empty);
            environmentVariables.Add("Prowlarr_Redirect", message.Redirect.ToString());

            ExecuteScript(environmentVariables);
        }

        public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
        {
            var environmentVariables = new StringDictionary();

            environmentVariables.Add("Prowlarr_EventType", "HealthIssue");
            environmentVariables.Add("Prowlarr_InstanceName", _configFileProvider.InstanceName);
            environmentVariables.Add("Prowlarr_ApplicationUrl", _configService.ApplicationUrl);
            environmentVariables.Add("Prowlarr_Health_Issue_Level", Enum.GetName(typeof(HealthCheckResult), healthCheck.Type));
            environmentVariables.Add("Prowlarr_Health_Issue_Message", healthCheck.Message);
            environmentVariables.Add("Prowlarr_Health_Issue_Type", healthCheck.Source.Name);
            environmentVariables.Add("Prowlarr_Health_Issue_Wiki", healthCheck.WikiUrl.ToString() ?? string.Empty);

            ExecuteScript(environmentVariables);
        }

        public override void OnHealthRestored(HealthCheck.HealthCheck previousCheck)
        {
            var environmentVariables = new StringDictionary();

            environmentVariables.Add("Prowlarr_EventType", "HealthRestored");
            environmentVariables.Add("Prowlarr_InstanceName", _configFileProvider.InstanceName);
            environmentVariables.Add("Prowlarr_ApplicationUrl", _configService.ApplicationUrl);
            environmentVariables.Add("Prowlarr_Health_Restored_Level", Enum.GetName(typeof(HealthCheckResult), previousCheck.Type));
            environmentVariables.Add("Prowlarr_Health_Restored_Message", previousCheck.Message);
            environmentVariables.Add("Prowlarr_Health_Restored_Type", previousCheck.Source.Name);
            environmentVariables.Add("Prowlarr_Health_Restored_Wiki", previousCheck.WikiUrl.ToString() ?? string.Empty);

            ExecuteScript(environmentVariables);
        }

        public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
        {
            var environmentVariables = new StringDictionary();

            environmentVariables.Add("Prowlarr_EventType", "ApplicationUpdate");
            environmentVariables.Add("Prowlarr_InstanceName", _configFileProvider.InstanceName);
            environmentVariables.Add("Prowlarr_ApplicationUrl", _configService.ApplicationUrl);
            environmentVariables.Add("Prowlarr_Update_Message", updateMessage.Message);
            environmentVariables.Add("Prowlarr_Update_NewVersion", updateMessage.NewVersion.ToString());
            environmentVariables.Add("Prowlarr_Update_PreviousVersion", updateMessage.PreviousVersion.ToString());

            ExecuteScript(environmentVariables);
        }

        public override ValidationResult Test()
        {
            var failures = new List<ValidationFailure>();

            if (!_diskProvider.FileExists(Settings.Path))
            {
                failures.Add(new NzbDroneValidationFailure("Path", "File does not exist"));
            }

            if (failures.Empty())
            {
                try
                {
                    var environmentVariables = new StringDictionary();
                    environmentVariables.Add("Prowlarr_EventType", "Test");
                    environmentVariables.Add("Prowlarr_InstanceName", _configFileProvider.InstanceName);
                    environmentVariables.Add("Prowlarr_ApplicationUrl", _configService.ApplicationUrl);

                    var processOutput = ExecuteScript(environmentVariables);

                    if (processOutput.ExitCode != 0)
                    {
                        failures.Add(new NzbDroneValidationFailure(string.Empty, $"Script exited with code: {processOutput.ExitCode}"));
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                    failures.Add(new NzbDroneValidationFailure(string.Empty, ex.Message));
                }
            }

            return new ValidationResult(failures);
        }

        private ProcessOutput ExecuteScript(StringDictionary environmentVariables)
        {
            _logger.Debug("Executing external script: {0}", Settings.Path);

            var processOutput = _processProvider.StartAndCapture(Settings.Path, Settings.Arguments, environmentVariables);

            _logger.Debug("Executed external script: {0} - Status: {1}", Settings.Path, processOutput.ExitCode);
            _logger.Debug("Script Output: \r\n{0}", string.Join("\r\n", processOutput.Lines));

            return processOutput;
        }
    }
}
