using UnityEngine;
using GadgetCore.API;
using UnityEngine.SceneManagement;
using System;
using QuickStack.ConfigEnums;
using System.Reflection;
using GadgetCore.API.ConfigMenu;

namespace QuickStack
{
  [Gadget("QuickStack", RequiredOnClients: false)]
  public class QuickStack : Gadget<QuickStack>
  {
    public const string MOD_VERSION = "1.1"; // Set this to the version of your mod.
    public const string CONFIG_VERSION = "1.1"; // Increment this whenever you change your mod's config file.

    protected override void LoadConfig()
    {
      Config.Load();
      string fileVersion = Config.ReadString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
      if (fileVersion != CONFIG_VERSION)
      {
        Config.Reset();
        Config.WriteString("ConfigVersion", CONFIG_VERSION, comments: "The Config Version (not to be confused with mod version)");
      }

      Core.settingStackSource = Config.ReadEnum("TakeItemsFrom", StackSourceInventoryEnum.Both, comments: new string[] { "Were should items be taken from?" });
      Core.settingStackPotionsSource = Config.ReadEnum("TakePotionsFrom", StackSourceInventoryPotionsEnum.OnlyMainInventory, comments: new string[] { "Were should potions be taken from?" });
      Core.settingStackMode = Config.ReadEnum("StackMode", StackModeEnum.FillPageContaining, comments: new string[] { "How should items be inserted?" });
      Core.settingStackRange = Config.ReadEnum("StackRange", StackRangeEnum.AllPages, comments: new string[] { "Were should items be inserted?" });

      Config.Save();
    }

    private T GetFromStatic<T>(string classString, string paramString)
    {
      var type = Type.GetType("GadgetCore" + "." + classString + ", GadgetCore");
      var field = type.GetProperty(paramString, BindingFlags.Public | BindingFlags.Static);
      MethodInfo strGetter = field.GetGetMethod();
      return (T)strGetter.Invoke(null, null);
    }

    public override string GetModDescription()
    {
      return "A mod that add a quick stack button to the chest menu.";
    }

    protected override void Initialize()
    {
      Logger.Log("QuickStack v" + Info.Mod.Version);
      Core.logger = Logger;
      SceneManager.sceneLoaded += OnSceneLoaded;
    }

    internal static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    {
      if (scene.buildIndex == 1)
      {
        try
        {
          var storageMenu = GameObject.Find("Main Camera").transform.Find("menuStorage").gameObject;
          var pagge0 = storageMenu.transform.Find("page0");
          var btn = UnityEngine.Object.Instantiate(pagge0, pagge0.transform.parent).gameObject;
          btn.name = "button_quick_stack";
          Component.Destroy(btn.GetComponentInChildren<ButtonMenu>());
          btn.transform.localPosition = pagge0.transform.localPosition + new Vector3(-0.175f, 0.475f, -2);
          btn.transform.localScale = new Vector3(1f / 16f, 1f / 16f, 1);
          btn.AddComponent<StackButtonScript>();

          btn.GetComponentInChildren<BoxCollider>().size = new Vector3(2, 2, 2);

          btn.transform.SetParent(storageMenu.transform);
        }
        catch (Exception e) { Core.logger.Log(e.Message); }
      }
    }
  }
}
