using Exiled.API.Features;
using Exiled.API.Enums;
using Exiled.Events.EventArgs;
using Exiled.API.Interfaces;
using PlayerRoles;
using System;

namespace CustomRolesPlugin
{
    public class Plugin : Plugin<Config>
    {
        public override string Name => "CustomRolesPlugin";
        public override string Author => "ВашеИмя";
        public override string Prefix => "custom_roles";
        public override Version Version => new Version(1, 0, 0);
        public override Version RequiredExiledVersion => new Version(5, 0, 0);

        private EventHandlers handlers;

        public override void OnEnabled()
        {
            base.OnEnabled();

            handlers = new EventHandlers(this);
            Exiled.Events.Handlers.Player.InteractingDoor += handlers.OnInteractingDoor;
            Exiled.Events.Handlers.Player.Hurting += handlers.OnHurting;
            Exiled.Events.Handlers.Server.RoundStarted += handlers.OnRoundStarted;

            Log.Info("CustomRolesPlugin включен.");
        }

        public override void OnDisabled()
        {
            Exiled.Events.Handlers.Player.InteractingDoor -= handlers.OnInteractingDoor;
            Exiled.Events.Handlers.Player.Hurting -= handlers.OnHurting;
            Exiled.Events.Handlers.Server.RoundStarted -= handlers.OnRoundStarted;
            handlers = null;

            base.OnDisabled();

            Log.Info("CustomRolesPlugin выключен.");
        }
    }

    public class EventHandlers
    {
        private readonly Plugin plugin;

        public EventHandlers(Plugin pluginInstance)
        {
            plugin = pluginInstance;
        }

        private bool RandomChance(int percentage) => new Random().Next(0, 100) < percentage;

        private void SetPlayerRole(Player player, RoleTypeId role)
        {
            if (player.Role.Type != role)
            {
                player.SetRole(role);
            }
        }

        private bool IsHacker(Player player) => player.Role.Type == RoleTypeId.Scientist;
        private bool IsContainmentTeam(Player player) => player.Role.Type == RoleTypeId.NtfLieutenant;

        public void OnRoundStarted()
        {
            foreach (Player player in Player.List)
            {
                if (RandomChance(10))
                {
                    SetPlayerRole(player, RoleTypeId.Scientist);
                    player.Broadcast(10, "Вы Хакер и можете блокировать двери!");
                }

                if (player.IsAdmin)
                {
                    SetPlayerRole(player, RoleTypeId.NtfLieutenant);
                    player.Broadcast(10, "Вы член Отряда по сдерживанию SCP Кетер!");
                }
            }
        }

        public void OnInteractingDoor(InteractingDoorEventArgs ev)
        {
            Player player = ev.Player;

            if (IsHacker(player) && RandomChance(50))
            {
                ev.IsAllowed = false;
                player.Broadcast(2, "Вы заблокировали дверь!");
            }
        }

        public void OnHurting(HurtingEventArgs ev)
        {
            Player attacker = ev.Attacker;
            Player victim = ev.Victim;

            if (attacker != null && IsContainmentTeam(attacker) && victim.Role.Type == RoleTypeId.Scp079)
            {
                ev.Amount = 0;
                attacker.Broadcast(2, "Вы не можете атаковать SCP-079!");
            }
        }
    }

    public class Config : IConfig
    {
        public bool IsEnabled { get; set; } = true;
        public bool Debug { get; set; } = false;
    }
}