﻿using Rocket.API.Commands;
using Rocket.API.Permissions;
using Rocket.API.Player;
using Rocket.API.Plugins;
using Rocket.Core.Commands;
using Rocket.Core.I18N;
using Rocket.Core.Permissions;
using Rocket.Core.Player;
using Rocket.Core.User;
using System;

namespace RG.PlayerReport
{
    public class CommandReport : ICommand
    {
        internal PlayerReport PlayerReport;

        public CommandReport(IPlugin plugin)
        {
            PlayerReport = (PlayerReport)plugin;
        }

        public void Execute(ICommandContext context)
        {
            var parameters = context.Parameters;
            var permissionProvider = context.Container.Resolve<IPermissionProvider>();
            var caller = ((IPlayerUser)context.User).GetPlayer();

            if (permissionProvider.CheckPermission(caller, "PlayerReport.Report") != PermissionResult.Grant && permissionProvider.CheckPermission(caller, "PlayerReport.*") != PermissionResult.Grant)
            {
                throw new NotEnoughPermissionsException(context.User, "PlayerReport.Report");
            }

            if (parameters.Length < 2)
            {
                throw new CommandWrongUsageException();
            }

            IPlayer Reported = parameters.Get<IPlayer>(0);

            if (Reported == null || !Reported.IsOnline)
            {
                throw new PlayerNameNotFoundException(parameters.Get<string>(0));
            }

            if (Reported.Id == caller.Id)
            {
                caller.GetUser().SendLocalizedMessage(PlayerReport.Translations, "command_report_yourself");
                return;
            }

            string Reason = null;
            int num = 0;
            foreach (var rs in parameters)
            {
                if (num == 0)
                    continue;
                if (Reason == null)
                    Reason = rs;
                else
                    Reason = Reason + " " + rs;
            }

            if (Reason == null)
            {
                throw new CommandWrongUsageException();
            }

            if (PlayerReport.ConfigurationInstance.MaxCharacter)
            {
                if (PlayerReport.ConfigurationInstance.LimCharacter <= 50)
                {
                    PlayerReport.ConfigurationInstance.LimCharacter = 50;
                    PlayerReport.SaveConfig();
                }

                if (Reason.Length > PlayerReport.ConfigurationInstance.LimCharacter)
                {
                    caller.GetUser().SendLocalizedMessage(PlayerReport.Translations, "command_report_maxchar");
                    return;
                }
            }

            PlayerReport.DataBase.Add(new ReportData(caller.Id, caller.Name, Reported.Id, Reported.Name, Reason, DateTime.Now));
            caller.GetUser().SendLocalizedMessage(PlayerReport.Translations, "command_add_successful", Reported.Name);

            if (!PlayerReport.ConfigurationInstance.Notify)
                return;

            
            foreach (var pla in PlayerReport.PlayerManager.OnlineUsers)
            {
                if (permissionProvider.CheckPermission(pla, "PlayerReport.Notification") != PermissionResult.Grant && permissionProvider.CheckPermission(pla, "PlayerReport.Notify") != PermissionResult.Grant && permissionProvider.CheckPermission(pla, "PlayerReport.*") != PermissionResult.Grant)
                    continue;

                pla.SendLocalizedMessage(PlayerReport.Translations, "new_reports_to_see");
            }
        }

        public string Name => "report";
        public string Summary => "Report malicious players";
        public string Description => null;
        public string Syntax => "<player> [reason]";
        public string[] Aliases => null;
        public string Permission => null;
        public IChildCommand[] ChildCommands => null;

        public bool SupportsUser(Type user)
        {
            return true;
        }
    }
}
