using GadgetCore;
using GadgetCore.API;
using HarmonyLib;
using System;
using System.Reflection;
using UnityEngine;

namespace AncientReassembler.Patches
{

  [HarmonyPatch(typeof(GameScript))]
  [HarmonyPatch("Update")]
  [HarmonyGadget("AncientReassembler")]
  public static class Patch_GameScript_Update
  {
    [HarmonyPrefix]
    public static void Prefix(GameScript __instance, ref Item ___holdingItem, ref int ___curhoveringstat, ref bool ___holdingCombatChip, bool ___exitingcombatmode)
    {
      try
      {
        if (MenuScript.player)
        {
          if (!GameScript.pausing && Input.GetMouseButtonDown(0) && GameScript.inventoryOpen)
          {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 7f))
            {
              if (hit.transform.gameObject.name.StartsWith("ancient_reassembler_"))
              {
                var slot = 2;
                if (hit.transform.gameObject.name.StartsWith("ancient_reassembler_in"))
                  slot = int.Parse(hit.transform.gameObject.name.Substring("ancient_reassembler_in".Length));

                if (slot >= 0 && slot < 16)
                {
                  if (___holdingItem.id != 0)
                  {
                    if (Core.itemStore.CanUse(___holdingItem) && slot != 2)
                    {
                      if (Core.itemStore.Items[slot].id == 0)
                      {
                        Place(__instance, slot, ref ___holdingItem);
                      }
                      else
                      {
                        Swap(__instance, slot, ref ___holdingItem);
                      }
                    }
                  }
                  else if (Core.itemStore.Items[slot].id != 0)
                  {
                    Select(__instance, slot, ref ___holdingItem);
                  }
                }
              }
              else if (hit.transform.gameObject.name.StartsWith("button_ancient_reassembler"))
                Core.itemStore.Combine();
            }
          }
        }
      }
      catch (Exception e) { Core.logger.LogConsole(e.Message); }
    }


    [HarmonyPostfix]
    public static void Postfix()
    {
      if (MenuScript.player)
      {
        if (GameScript.inventoryOpen)
        {
          var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
          RaycastHit hit;
          if (Physics.Raycast(ray, out hit, 7f))
          {
            if (hit.transform.gameObject.name.StartsWith("ancient_reassembler_"))
            {
              var slot = 2;
              if (hit.transform.gameObject.name.StartsWith("ancient_reassembler_in"))
                slot = int.Parse(hit.transform.gameObject.name.Substring("ancient_reassembler_in".Length));
              if ((Core.itemStore.Items[slot]?.id ?? 0) != 0)
                PatchMethods.HoverItem(Core.itemStore.Items[slot]);
            }
          }
        }
      }
    }
    private static void Swap(GameScript __instance, int slot, ref Item ___holdingItem)
    {
      __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);

      Item item = ___holdingItem;
      ___holdingItem = Core.itemStore.Items[slot];
      Core.itemStore.Items[slot] = item;

      Core.itemStore.UpdateLatestUI();
      __instance.RefreshHoldingSlot();
    }

    private static void Select(GameScript __instance, int slot, ref Item ___holdingItem)
    {
      __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);

      ___holdingItem = Core.itemStore.Items[slot];
      Core.itemStore.Items[slot] = __instance.EmptyItem();

      Core.itemStore.UpdateLatestUI();
      __instance.RefreshHoldingSlot();
    }

    private static void Place(GameScript __instance, int slot, ref Item ___holdingItem)
    {
      __instance.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/CLICK1"), Menuu.soundLevel / 10f);

      Core.itemStore.Items[slot] = ___holdingItem;
      ___holdingItem = __instance.EmptyItem();

      Core.itemStore.UpdateLatestUI();
      __instance.RefreshHoldingSlot();
    }
  }
}