using GadgetCore.API;
using GadgetCore.Util;
using RecipeMenuCore.API;
using System.Collections;
using UnityEngine;

namespace Subworlds
{
  [Gadget("SubworldCore", LoadAfter: new string[] { "RecipeMenuCore" }, Dependencies: new string[] { "RecipeMenuCore" })]
  public class CoreGadget : Gadget<CoreGadget>
  {
    public const string MOD_VERSION = "1.0"; // Set this to the version of your mod.
    public const string CONFIG_VERSION = "1.0"; // 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)");
      }

      Config.Save();
    }

    public override string GetModDescription()
    {
      return "A mod that adds ships that can be reaided.";
    }

    protected override void Initialize()
    {
      Logger.Log("Subworlds v" + Info.Mod.Version);
      Core.logger = Logger;

      ItemInfo itemInfoPorter5 = new ItemInfo(ItemType.GENERIC, "port5", "", null as Texture2D).Register("port5");
      TileInfo tilePorter5 = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("port5"), itemInfoPorter5).Register("port5");
      tilePorter5.OnInteract += Tile_OnInteract5;
      ItemInfo itemInfoPorter6 = new ItemInfo(ItemType.GENERIC, "port6", "", null as Texture2D).Register("port6");
      TileInfo tilePorter6 = new TileInfo(TileType.INTERACTIVE, null as Texture2D, new GameObject("port6"), itemInfoPorter6).Register("port6");
      tilePorter6.OnInteract += Tile_OnInteract6;

      Core.itemOldTex = new ItemInfo(ItemType.GENERIC, "Old Tec", "Some piece of old\ntechnology", GadgetCoreAPI.LoadTexture2D("Core/iOldTec.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTex, "oldtec");

      Core.itemOldTexShield = new ItemInfo(ItemType.OFFHAND, "Old Tec Shield", "", GadgetCoreAPI.LoadTexture2D("Core/iOldTecShield.png"), Stats: new EquipStats(1, 1, 1, 1, 3, 0),
         HeldTex: GadgetCoreAPI.LoadTexture2D("Core/iOldTecShield.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTexShield, "oldtecshield");

      Core.itemOldTexHelmet = new ItemInfo(ItemType.HELMET, "Old Tec Helmet", "", GadgetCoreAPI.LoadTexture2D("Core/iOldTecHelmet.png"), Stats: new EquipStats(1, 1, 1, 1, 3, 0),
         HeadTex: GadgetCoreAPI.LoadTexture2D("Core/hOldTecHelmet.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTexHelmet, "oldtechelmet");

      Core.itemOldTexArmor = new ItemInfo(ItemType.ARMOR, "Old Tec Armor", "", GadgetCoreAPI.LoadTexture2D("Core/iOldTecArmor.png"), Stats: new EquipStats(1, 1, 1, 1, 3, 0),
         BodyTex: GadgetCoreAPI.LoadTexture2D("Core/bOldTecArmor.png"), ArmTex: GadgetCoreAPI.LoadTexture2D("Core/aOldTecArmor.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTexArmor, "oldtecarmor");

      Core.itemOldTexDroid = new ItemInfo(ItemType.DROID, "Old Tec Droid", "", GadgetCoreAPI.LoadTexture2D("Core/iOldTecDroid.png"), Stats: new EquipStats(1, 1, 1, 1, 3, 0),
         BodyTex: GadgetCoreAPI.LoadTexture2D("Core/dOldTecDroidBase.png"), HeadTex: GadgetCoreAPI.LoadTexture2D("Core/dOldTecDroidTop.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTexDroid, "oldtecdroid");

      Core.itemOldTexMod = new ItemInfo(ItemType.MOD, "GearModStackSize+", "GEAR MOD\nAttach to weapons\nand armor in Mech City.", GadgetCoreAPI.LoadTexture2D("Core/iOldTecMod.png"));
      ItemRegistry.Singleton.Register(Core.itemOldTexMod, "oldtecmod");

      var recipePage = new RecipePage(RecipePageType.UniversalCrafter, "Old Tec", GadgetCoreAPI.LoadTexture2D("Core/recipesOldTec.png")).Register();
      recipePage.AddRecipePageEntry(new RecipePageEntry(Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 133, Core.itemOldTexShield.GetID(), allwaysShowInput: true));
      recipePage.AddRecipePageEntry(new RecipePageEntry(Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 123, Core.itemOldTexHelmet.GetID(), allwaysShowInput: true));
      recipePage.AddRecipePageEntry(new RecipePageEntry(Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 103, Core.itemOldTexArmor.GetID(), allwaysShowInput: true));
      recipePage.AddRecipePageEntry(new RecipePageEntry(Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 113, Core.itemOldTexDroid.GetID(), allwaysShowInput: true));
      recipePage.AddRecipePageEntry(new RecipePageEntry(Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 210, Core.itemOldTexMod.GetID(), allwaysShowInput: true));

      var universalCrafter = ((CraftMenuInfo)MenuRegistry.Singleton["Gadget Core:Crafter Menu"]);
      universalCrafter.AddCraftPerformer(CraftMenuInfo.CreateSimpleCraftPerformer(
        Tuple.Create(new int[] { Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 133 }, new Item(Core.itemOldTexShield.GetID(), 1, 0, 0, 0, new int[3], new int[3]), 0)));
      universalCrafter.AddCraftPerformer(CraftMenuInfo.CreateSimpleCraftPerformer(
       Tuple.Create(new int[] { Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 123 }, new Item(Core.itemOldTexHelmet.GetID(), 1, 0, 0, 0, new int[3], new int[3]), 0)));
      universalCrafter.AddCraftPerformer(CraftMenuInfo.CreateSimpleCraftPerformer(
        Tuple.Create(new int[] { Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 103 }, new Item(Core.itemOldTexArmor.GetID(), 1, 0, 0, 0, new int[3], new int[3]), 0)));
      universalCrafter.AddCraftPerformer(CraftMenuInfo.CreateSimpleCraftPerformer(
        Tuple.Create(new int[] { Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 113 }, new Item(Core.itemOldTexDroid.GetID(), 1, 0, 0, 0, new int[3], new int[3]), 0)));
      universalCrafter.AddCraftPerformer(CraftMenuInfo.CreateSimpleCraftPerformer(
        Tuple.Create(new int[] { Core.itemOldTex.GetID(), Core.itemOldTex.GetID(), 210 }, new Item(Core.itemOldTexMod.GetID(), 1, 0, 0, 0, new int[3], new int[3]), 0)));

    }

    private IEnumerator Tile_OnInteract5()
    {
      InstanceTracker.PlayerScript.Portal(5);
      yield break;
    }
    private IEnumerator Tile_OnInteract6()
    {
      InstanceTracker.PlayerScript.Portal(6);
      yield break;
    }
  }
}
