using System;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace BanquetForCyka {
    [HarmonyPatch]
    public class MultiBuy {
        public static UIShopBlockSelect panel;
        public static Traverse panelTraverse;

        [HarmonyPostfix]
        [HarmonyPatch(typeof(UIShopBlockSelect), "OnSpawn")]
        public static void PostfixCreate(UIShopBlockSelect __instance) {
            panel = __instance;
            if (Main.debugBuyAll.Value)
                Console.WriteLine("UISnapshotPanel.OnPool: {0}", __instance);
            panelTraverse = Traverse.Create(__instance);
            var placeButton = panelTraverse.Field("m_PurchaseBlockButton").GetValue<Button>();
            placeButton.gameObject.AddComponent<MultiBuyRightClickHandler>();
        }
    }

    class MultiBuyRightClickHandler : MonoBehaviour, IPointerClickHandler {
        // private void Awake() {
        // }

        public void OnPointerClick(PointerEventData eventData) {
            if (Main.debugBuyAll.Value)
                Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: {0} {1}", gameObject.name,
                                  eventData.button);
            try {
                if (eventData.button == PointerEventData.InputButton.Right) {
                    UIBlockSelectGrid grid = MultiBuy.panelTraverse.Field("m_Grid").GetValue<UIBlockSelectGrid>();
                    BlockTypes blockTypes;
                    bool ok = grid.TryGetSelection(out blockTypes);
                    if (!ok) {
                        if (Main.debugBuyAll.Value)
                            Console.WriteLine(
                                "MultiBuyRightClickHandler.OnPointerClick: Failed to get block selection from grid");
                        return;
                    }
                    uint shopBlockPoolID = MultiBuy.panelTraverse.Field("m_ShopBlockPoolID").GetValue<uint>();

                    MethodInfo canPurchaseMethod =
                        AccessTools.Method(typeof(UIShopBlockSelect), "CanPurchaseBlock", new[] { typeof(BlockTypes) });
                    Func<BlockTypes, bool> canPurchase = (Func<BlockTypes, bool>)Delegate.CreateDelegate(
                        typeof(Func<BlockTypes, bool>), MultiBuy.panel, canPurchaseMethod);

                    for (int i = 0; i < Main.multiBuyAmount.Value; i++) {
                        if (!canPurchase.Invoke(blockTypes)) {
                            if (Main.debugBuyAll.Value)
                                Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Can purchase no more {0}",
                                                  blockTypes);
                            return;
                        }
                        Singleton.Manager<ManPurchases>.inst.RequestPurchaseBlock(shopBlockPoolID, blockTypes, 1);
                        if (Main.debugBuyAll.Value)
                            Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Purchased {0} block",
                                              blockTypes);
                    }
                }
            } catch (Exception e) {
                if (Main.debugBuyAll.Value)
                    Console.WriteLine("MultiBuyRightClickHandler.OnPointerClick: Exception occurred: {0}", e);
            }
        }
    }
}
