using BTKUILib;
using BTKUILib.UIObjects;
using NAK.Stickers.Networking;
using NAK.Stickers.Utilities;

namespace NAK.Stickers.Integrations;

public static partial class BTKUIAddon
{
    private static Page _rootPage;
    private static string _rootPageElementID;
    
    private static bool _isOurTabOpened;

    public static void Initialize()
    {
        Setup_Icons();
        Setup_StickerModTab();
        Setup_PlayerOptionsPage();
    }

    #region Setup

    private static void Setup_Icons()
    {
        // All icons used - https://www.flaticon.com/authors/gohsantosadrive
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-alphabet", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-alphabet.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-eraser", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-eraser.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-folder", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-folder.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-headset", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-headset.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-magnifying-glass", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-magnifying-glass.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-magic-wand", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-magic-wand.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-mouse", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-mouse.png"));
        //QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-pencil", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-pencil.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-puzzle", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-puzzle.png"));
        QuickMenuAPI.PrepareIcon(ModSettings.ModName, "Stickers-rubbish-bin", UIUtils.GetIconStream("Gohsantosadrive_Icons.Stickers-rubbish-bin.png"));
    }

    private static void Setup_StickerModTab()
    {
        _rootPage = new Page(ModSettings.ModName, ModSettings.SM_SettingsCategory, true, "Stickers-puzzle")
        {
            MenuTitle = ModSettings.SM_SettingsCategory,
            MenuSubtitle = "Stickers! Double-click the tab to quickly toggle Sticker Mode.",
        };

        _rootPageElementID = _rootPage.ElementID;
        
        QuickMenuAPI.OnTabChange += OnTabChange;
        ModNetwork.OnTextureOutboundStateChanged += (isSending) =>
        {
            if (_isOurTabOpened && isSending) QuickMenuAPI.ShowAlertToast("Sending Sticker over Mod Network...", 2);
            //_rootPage.Disabled = isSending; // TODO: fix being able to select stickers while sending
        };
        
        StickerSystem.OnStickerLoaded += (slotIndex, imageRelativePath) =>
        {
            if (_isOurTabOpened) QuickMenuAPI.ShowAlertToast($"Sticker loaded: {imageRelativePath}", 2);
            _stickerSelectionButtons[slotIndex].ButtonIcon = StickerCache.GetBtkUiIconName(imageRelativePath);
        };
        
        StickerSystem.OnStickerLoadFailed += (slotIndex, error) =>
        {
            if (_isOurTabOpened) QuickMenuAPI.ShowAlertToast(error, 3);
        };

        Setup_StickersModCategory();
        Setup_StickerSelectionCategory();
        Setup_OtherOptionsCategory();
    }

    #endregion Setup

    #region Double-Click Place Sticker

    private static DateTime lastTime = DateTime.Now;

    private static void OnTabChange(string newTab, string previousTab)
    {
        _isOurTabOpened = newTab == _rootPageElementID;
        if (!_isOurTabOpened) return;
        
        TimeSpan timeDifference = DateTime.Now - lastTime;
        if (timeDifference.TotalSeconds <= 0.5)
        {
            switch (ModSettings.Entry_TabDoubleClick.Value)
            {
                default:
                case TabDoubleClick.ToggleStickerMode:
                    OnPlaceStickersButtonClick();
                    break;
                case TabDoubleClick.ClearAllStickers:
                    OnClearAllStickersButtonClick();
                    break;
                case TabDoubleClick.ClearSelfStickers:
                    OnClearSelfStickersButtonClick();
                    break;
                case TabDoubleClick.None:
                    break;
            }
            return;
        }
        lastTime = DateTime.Now;
    }

    #endregion Double-Click Place Sticker
}