using UnityEngine;
using GadgetCore.API;

namespace ShipDecorations
{
  public static class ItemUtil
  {
    public static int CreatePlacableDecoItem(string tileImgPath, string itemImgPath, string name)
    {

      Texture2D texture2D = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2D2 = GadgetCoreAPI.LoadTexture2D(itemImgPath);

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

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", texture2D2);
      itemInfo.Register(name);
      new TileInfo(TileType.NONSOLID, texture2D, gameObject, itemInfo).Register(name);

      return itemInfo.GetID();
    }

    public static int CreatePlacableSlideDoorItem(string tileImgPath, string tileOpenImgPath, string itemImgPath, string name)
    {

      Texture2D texture2D = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2DOpen = GadgetCoreAPI.LoadTexture2D(tileOpenImgPath);

      Texture2D texture2DItem = GadgetCoreAPI.LoadTexture2D(itemImgPath);

      GameObject hostObject = new GameObject("door");
      hostObject.transform.localPosition = new Vector3(10f, 0, 0);

      {
        GameObject closedDoorObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/2501"));
        Renderer renderer = closedDoorObject.GetComponentInChildren<Renderer>();
        renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
        {
          mainTexture = texture2D
        };
        closedDoorObject.transform.SetParent(hostObject.transform);
        closedDoorObject.SetActive(true);
      }

      {
        GameObject openDoorObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/2501"));
        Renderer renderer = openDoorObject.GetComponentInChildren<Renderer>();
        renderer.material = new Material(Shader.Find("Transparent/Diffuse"))
        {
          mainTexture = texture2DOpen
        };
        openDoorObject.transform.SetParent(hostObject.transform);
        openDoorObject.SetActive(false);
      }

      GameObject doorTriggerObject = new GameObject();
      doorTriggerObject.transform.localScale = new Vector3(4f, 4f, 5);
      doorTriggerObject.transform.localPosition = new Vector3(10f, 0, 0);
      doorTriggerObject.transform.SetParent(hostObject.transform);
      doorTriggerObject.layer = 0;

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

      AudioSource audio = doorTriggerObject.AddComponent<AudioSource>();

      var script = doorTrigger.gameObject.AddComponent<DoorColliderScript>();

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", texture2DItem);
      itemInfo.Register(name);
      new TileInfo(TileType.NONSOLID, texture2D, hostObject, itemInfo).Register(name);

      Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, false, true);

      return itemInfo.GetID();
    }

    public static int CreateWallItem(string tileImgPath, string itemImgPath, string name)
    {

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

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", textureItem);
      itemInfo.Register(name);
      new TileInfo(TileType.WALL, textureTile, null, itemInfo).Register(name);

      return itemInfo.GetID();
    }

    public static int CreateEngineItem(string tileImgPath, string itemImgPath, string componentName, string name, string description, string id)
    {
      Texture2D texture2D = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2D2 = GadgetCoreAPI.LoadTexture2D(itemImgPath);

      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/ScrapYard/component/" + componentName));

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, description, texture2D2);
      itemInfo.Register(id);
      new TileInfo(TileType.NONSOLID, texture2D, gameObject, itemInfo).Register(id);

      return itemInfo.GetID();
    }

    public static int CreateOverrideEngineItem(string tileImgPath, string itemImgPath, string componentName, string name, string description, string id)
    {
      Texture2D texture2D = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2D2 = GadgetCoreAPI.LoadTexture2D(itemImgPath);

      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/ScrapYard/component/" + componentName));

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, description, texture2D2);
      itemInfo.Register(id, 2400);
      new TileInfo(TileType.NONSOLID, texture2D, gameObject, itemInfo).Register(id, 2400);

      return itemInfo.GetID();
    }

    public static int CreateBlockItem(string tileImgPath, string itemImgPath, string name)
    {

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

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", textureItem);
      itemInfo.Register(name);
      new TileInfo(TileType.SOLID, textureTile, null, itemInfo).Register(name);

      return itemInfo.GetID();
    }

    public static int CreatePlacableStairsItemL(string tileImgPath, string itemImgPath, string name, string registerName = null)
    {
      if (registerName == null)
        registerName = name;

      Texture2D texture2DTile = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2DItem = 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 = texture2DTile
      };

      for (int i = 0; i < 14; i++)
      {
        GameObject colliderObject = new GameObject();
        colliderObject.transform.localScale = new Vector3(4f, 4f, 5);
        colliderObject.transform.localPosition = new Vector3(10f, -0.22f, 0);
        colliderObject.transform.SetParent(gameObject.transform);
        colliderObject.layer = 0;
        BoxCollider doorTrigger = colliderObject.AddComponent<BoxCollider>();
        doorTrigger.size = new Vector3(Mathf.Sqrt(2f), 0.2f, 1);
        doorTrigger.transform.rotation = Quaternion.Euler(0, 0, -45);
      }

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", texture2DItem);
      itemInfo.Register(registerName);
      new TileInfo(TileType.NONSOLID, texture2DTile, gameObject, itemInfo).Register(registerName);

      return itemInfo.GetID();
    }

    public static int CreatePlacableStairsItemR(string tileImgPath, string itemImgPath, string name, string registerName = null)
    {
      if (registerName == null)
        registerName = name;

      Texture2D texture2DTile = GadgetCoreAPI.LoadTexture2D(tileImgPath);
      Texture2D texture2DItem = 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 = texture2DTile
      };

      for (int i = 0; i < 14; i++)
      {
        GameObject colliderObject = new GameObject();
        colliderObject.transform.localScale = new Vector3(4f, 4f, 5);
        colliderObject.transform.localPosition = new Vector3(10f, -0.22f, 0);
        colliderObject.transform.SetParent(gameObject.transform);
        colliderObject.layer = 0;
        BoxCollider doorTrigger = colliderObject.AddComponent<BoxCollider>();
        doorTrigger.size = new Vector3(Mathf.Sqrt(2f), 0.2f, 1);
        doorTrigger.transform.rotation = Quaternion.Euler(0, 0, 45);
      }

      ItemInfo itemInfo = new ItemInfo(ItemType.GENERIC, name, "", texture2DItem);
      itemInfo.Register(registerName);
      new TileInfo(TileType.NONSOLID, texture2DTile, gameObject, itemInfo).Register(registerName);

      return itemInfo.GetID();
    }
  }
}
