using System;
using System.Collections.Generic;
using System.Linq;
using BaseAdminApi.Enums;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Menu;

namespace BaseAdmin.Menu;

public class MenuService : IMenuService
{
    private static readonly Dictionary<MenuItem, IMenu> Menus = new();
    private IMenu _adminMenu = null!;

    public readonly BaseAdmin BaseAdmin;

    public List<ChatMenuOption> MenuOptions => _adminMenu.MenuOptions;

    public MenuService(BaseAdmin baseAdmin)
    {
        BaseAdmin = baseAdmin;
        CreateMenu();
    }

    public IMenu CreateMenu(string title)
    {
        return BaseAdmin.Config.UseCenterHtmlMenu ? new CenterHtmlMenu(title) : new ChatMenu(title);
    }
    
    public void AddMenuOptions(string display, Action<CCSPlayerController, ChatMenuOption> handler, bool disabled)
    {
        _adminMenu.AddMenuOption(display, handler, disabled);
    }

    public void AddMenuOptions(MenuItem type, string display, Action<CCSPlayerController, ChatMenuOption> handler,
        bool disabled)
    {
        if (!Menus.TryGetValue(type, out var menu)) return;
        
        menu.AddMenuOption(display, handler, disabled);
    }

    private void CreateMenu()
    {
        _adminMenu = CreateMenu(BaseAdmin.Localizer["menu_title"]);
        _adminMenu.AddMenuOption(BaseAdmin.Localizer["menu.players_control"], PlayersControlMenu); 
        _adminMenu.AddMenuOption(BaseAdmin.Localizer["menu.server_control"], ServerControlMenu);
        _adminMenu.AddMenuOption(BaseAdmin.Localizer["menu.lock_control"],  );
    }

    private void ServerControlMenu(CCSPlayerController player, ChatMenuOption option)
    {
        throw new NotImplementedException();
    }

    private void PlayersControlMenu(CCSPlayerController player, ChatMenuOption option)
    {
        var menu = CreateMenu(BaseAdmin.Localizer["menu.players_control"]);
        if (BaseAdmin.CheckingForAdminAndFlag(player, AdminFlag.Kick))
        {
            menu.AddMenuOption(BaseAdmin.Localizer["menu.players.kick_player"], (_, _) => PlayersHandle(player, controller =>
            {
                controller.Kick("Kick");
                BaseAdmin.PrintToChatAll(BaseAdmin.Localizer["player.kick", player.PlayerName, controller.PlayerName]);
            }));
        }

        if (BaseAdmin.CheckingForAdminAndFlag(player, AdminFlag.Slay))
        {
            menu.AddMenuOption(BaseAdmin.Localizer["menu.players.kill_player"], (_, _) => PlayersHandle(player, controller =>
            {
                controller.PlayerPawn.Value?.CommitSuicide(true, true); 
                BaseAdmin.PrintToChatAll(BaseAdmin.Localizer["player.kill", player.PlayerName, controller.PlayerName]);
            }));
        }

        OpenMenu(player, menu);
        Menus.TryAdd(MenuItem.PlayerControl, menu);
    }

    private void LockControlMenu(CCSPlayerController player, ChatMenuOption option)
    {
        var menu = CreateMenu(BaseAdmin.Localizer["menu.lock_control"]);

        if (BaseAdmin.CheckingForAdminAndFlag(player, AdminFlag.Ban))
        {
            menu.AddMenuOption(BaseAdmin.Localizer["menu.lock.ban_player"], null!, true);
        }

        if (BaseAdmin.CheckingForAdminAndFlag(player, AdminFlag.Generic))
        {
            menu.AddMenuOption(BaseAdmin.Localizer["menu.lock.mute_player"], null!, true);
        }

        OpenMenu(player, menu);
        Menus.TryAdd(MenuItem.LockControl, menu);
    }

    private void PlayersHandle(CCSPlayerController player, Action<CCSPlayerController> handler)
    {
        var menu = CreateMenu(BaseAdmin.Localizer["menu.all_players"]);
        menu.AddMenuOption(BaseAdmin.Localizer["menu.pick_player"], null!, true);
        foreach (var players in Utilities.GetPlayers().Where(u => u.IsValid))
        {
            if (!BaseAdmin.PlayerImmunityComparison(player, players)) continue;
            
            menu.AddMenuOption($"{players.PlayerName} [{players.UserId}]", (_, _) => handler(players));
        }
        
        OpenMenu(player, menu);
    }

    public void OpenMenu(CCSPlayerController controller, IMenu? menu = null)
    {
        menu ??= _adminMenu;
        if (BaseAdmin.Config.UseCenterHtmlMenu)
            MenuManager.OpenCenterHtmlMenu(BaseAdmin, controller, (CenterHtmlMenu)menu);
        else
            MenuManager.OpenChatMenu(controller, (ChatMenu)menu);
    }
}

хочу реализовать так чтобы я мог писать в отдельных классах PlayersControlMenu, ServerControlMenu, LockControlMenu и уже их тут открывать чтобы класс не был слишком огромен