using GadgetCore.API;
using System;
using UnityEngine;

namespace AncientReassembler
{
  public class ItemStore
  {
    public ItemStore(int size)
    {
      m_Items = new Item[size];
    }

    private Item[] m_Items;

    public Item[] Items
    {
      get { return m_Items; }
      set { m_Items = value; }
    }

    public void Clear()
    {
      for (int i = 0; i < m_Items.Length; i++)
        m_Items[i] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);
    }

    public bool CanUse(Item item)
    {
      var type = ItemRegistry.GetTypeByID(item.id);
      if ((type & ItemType.LEVELING) == ItemType.LEVELING)
        return
           true;
      return false;
    }

    public bool ConfirmCombine()
    {

      if ((Items[0]?.id ?? 0) > 0 && (Items[1]?.id ?? 0) > 0 && (Items[2]?.id ?? 0) == 0)
      {
        Item item1 = Items[0];
        Item item2 = Items[1];
        if (item1.id == item2.id
          && item1.q == 1 && item2.q == 1
          && item1.tier == item2.tier
          && item1.tier < 3)
          return true;
      }
      return false;
    }

    public void Combine()
    {
      if (ConfirmCombine())
      {
        Items[2] = Items[0];

        Items[2].corrupted = Max(Items[0].corrupted, Items[1].corrupted);
        Items[2].exp = Max(Items[0].exp, Items[1].exp);
        Items[2].tier++;

        Items[0] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);
        Items[1] = new Item(0, 0, 0, 0, 0, new int[3], new int[3]);

        InstanceTracker.GameScript.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/create"), Menuu.soundLevel / 10f);

        UpdateLatestUI();
      }
    }

    private static int Max(int i1, int i2)
    {
      return i1 > i2 ? i1 : i2;
    }

    public void UpdateLatestUI()
    {
      UpdateUI(lastUsed);
    }

    private static readonly Texture2D textureButton0 = GadgetCoreAPI.LoadTexture2D("bAncientReassembler0.png");
    private static readonly Texture2D textureButton1 = GadgetCoreAPI.LoadTexture2D("bAncientReassembler1.png");
    private static readonly Texture2D textureButton2 = GadgetCoreAPI.LoadTexture2D("bAncientReassembler2.png");

    private GameObject lastUsed;
    public void UpdateUI(GameObject menu)
    {
      lastUsed = menu;
      try
      {
        for (int i = 0; i < m_Items.Length; i++)
        {
          var obj = menu.transform.GetChild(2 + i).GetChild(0).gameObject;
          obj.GetComponent<Renderer>().material = (Material)Resources.Load("i/i" + (m_Items[i]?.id ?? 0));
          var objBack = obj.transform.GetChild(1).gameObject;
          AnimIcon animIcon = (AnimIcon)objBack.GetComponent("AnimIcon");
          if ((m_Items[i]?.id ?? 0) > 0)
          {
            objBack.GetComponent<Renderer>().material = (Material)Resources.Load("iBack" + m_Items[i].tier);
            if (m_Items[i].tier == 3)
              animIcon.anim = 1;
            else
              animIcon.anim = 0;
            objBack.SetActive(true);
          }
          else
          {
            animIcon.anim = 0;
            objBack.SetActive(false);
          }
        }

        if (ConfirmCombine())
          menu.transform.GetChild(5).GetChild(0).gameObject.SetActive(true);
        else
          menu.transform.GetChild(5).GetChild(0).gameObject.SetActive(false);

        if ((m_Items[2]?.id ?? 0) > 0)
        {
          menu.transform.GetChild(2 + 0).gameObject.SetActive(false);
          menu.transform.GetChild(2 + 1).gameObject.SetActive(false);
          menu.transform.GetChild(2 + 2).gameObject.SetActive(true);
        }
        else
        {
          menu.transform.GetChild(2 + 0).gameObject.SetActive(true);
          menu.transform.GetChild(2 + 1).gameObject.SetActive(true);
          menu.transform.GetChild(2 + 2).gameObject.SetActive(false);
        }
      }
      catch (Exception e)
      {
        Core.logger.LogConsole(e.Message);
      }
    }
  }
}
