using GadgetCore.API;
using UnityEngine;

namespace PlatformBlock
{
  public static class ItemUtil
  {
    public static int CreatePlacablePlatformItem(string tileImgPath, string itemImgPath, string name)
    {

      Texture2D textureTile = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D textureItem = GadgetCoreAPI.LoadTexture2D(itemImgPath);

      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/2501"));
      Renderer renderer = gameObject.GetComponentInChildren<Renderer>();
      renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
      {
        mainTexture = textureTile
      };

      GameObject collidereObject = new GameObject();
      collidereObject.transform.localScale = new Vector3(4.0f, 0, 5);
      collidereObject.transform.localPosition = new Vector3(10f, 2, 0);
      collidereObject.transform.SetParent(gameObject.transform);
      collidereObject.layer = 11;

      BoxCollider collider = collidereObject.AddComponent<BoxCollider>();
      collider.size = new Vector3(1, 0, 1);


      GameObject collidereDeactivationTriggerObject = new GameObject();
      collidereDeactivationTriggerObject.transform.localScale = new Vector3(4.7f, 3.8f, 5);
      collidereDeactivationTriggerObject.transform.localPosition = new Vector3(10f, 0, 0);
      collidereDeactivationTriggerObject.transform.SetParent(gameObject.transform);
      collidereDeactivationTriggerObject.layer = 0;

      BoxCollider deactivationTrigger = collidereDeactivationTriggerObject.AddComponent<BoxCollider>();
      deactivationTrigger.size = new Vector3(1, 1, 1);
      deactivationTrigger.isTrigger = true;

      deactivationTrigger.gameObject.AddComponent<PlatformColliderScript>();


      GameObject collidereOnTopTriggerObject = new GameObject();
      collidereOnTopTriggerObject.transform.localScale = new Vector3(4.3f, 3.98f, 5);
      collidereOnTopTriggerObject.transform.localPosition = new Vector3(10f, 4, 0);
      collidereOnTopTriggerObject.transform.SetParent(gameObject.transform);
      collidereOnTopTriggerObject.layer = 0;

      BoxCollider onTopTrigger = collidereOnTopTriggerObject.AddComponent<BoxCollider>();
      onTopTrigger.size = new Vector3(1, 1, 1);
      onTopTrigger.isTrigger = true;

      onTopTrigger.gameObject.AddComponent<PlatformOnTopScript>();


      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", textureItem);
      int itemID = ItemRegistry.Singleton.Register(itemInfo, name);
      Core.logger.Log("Registerd ItemID for " + name + ":" + itemID);
      if (itemID < 0)
        throw new System.Exception("Could not register ItemInfo");

      TileInfo tileInfo = new TileInfo(TileType.NONSOLID, textureTile, gameObject, itemInfo);
      int tileID = TileRegistry.Singleton.Register(tileInfo, name);
      Core.logger.Log("Registerd TileID for " + name + ":" + tileID);
      if (tileID < 0)
        throw new System.Exception("Could not register TileInfo");

      return itemID;
    }
  }
}
