using GadgetCore.API;
using Subworlds.Scripts;
using UnityEngine;

namespace Subworlds
{
  public partial class MeteroidGadget : Gadget<MeteroidGadget>
  {
    private void CreateBaseBlock(string name, Texture2D texture, float w, float h)
    {
      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/2501"));
      Renderer renderer = gameObject.GetComponentInChildren<Renderer>();
      renderer.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = texture
      };
      gameObject.transform.localScale = new Vector3(w * 2, h * 2, 1);
      var boxCollider = gameObject.AddComponent<BoxCollider>();
      boxCollider.size = new Vector3(4, 4, 20);
      gameObject.layer = 23;

      GadgetCoreAPI.AddCustomResource("z/Subworlds/" + name, gameObject);
    }

    private void CreateBrakableObject(string name, Texture2D texture, bool large = false, ObjectType type = ObjectType.ORE, int itemId = 57)
    {
      var i = new ObjectInfo(type, new Item(itemId, 1, 0, 0, 0, new int[3], new int[3]), 16, texture).Register(name);
      if (large)
      {
        var t = i.Object.transform.GetChild(0);
        t.localScale = new Vector3(4, 4, 1);
      }
      GadgetCoreAPI.AddCustomResource("obj/Subworlds/" + name, i.Object);
    }

    private void CreateShipPart(string name, Texture texture, Texture textureBG, int[][] collisions, int[][] lamp, int tileW = 32, int tileH = 16)
    {
      var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
      gameObject.transform.localScale = new Vector3(tileW * 2, tileH * 2, 1);

      var rendererBody = gameObject.GetComponentInChildren<Renderer>();
      rendererBody.material = new Material(Shader.Find("Transparent/Diffuse"))
      {
        mainTexture = texture
      };

      for (int i = 0; i < 6; i++)
        GameObject.DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
      for (int i = 0; i < 16; i++)
        GameObject.DestroyImmediate(gameObject.transform.GetChild(1).gameObject);

      for (int i = 0; i < 7; i++)
        GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);

      Component.DestroyImmediate(gameObject.GetComponent<Chunk>());

      var bgRenderObject = GameObject.Instantiate(gameObject);
      for (int i = 0; i < bgRenderObject.transform.childCount; i++)
        GameObject.DestroyImmediate(bgRenderObject.transform.GetChild(0).gameObject);

      bgRenderObject.transform.SetParent(gameObject.transform);
      bgRenderObject.transform.localScale = new Vector3(1, 1, 1);
      bgRenderObject.transform.localPosition = new Vector3(0, 0, -0.3f);
      var rendererBG = bgRenderObject.GetComponent<Renderer>();
      rendererBG.material = new Material(Shader.Find("Transparent/Diffuse"))
      {
        mainTexture = textureBG
      };

      GameObject root = gameObject.transform.GetChild(0).gameObject;
      GameObject inst = root.transform.GetChild(0).gameObject;

      for (int i = 0; i < collisions.Length; i++)
      {
        GameObject entry = GameObject.Instantiate(inst);

        entry.transform.SetParent(root.transform);

        int[] coll = collisions[i];

        float w = coll[2] * (2.0f / tileW);
        float h = coll[3] * (2.0f / tileH);

        float x = coll[0] * (2.0f / tileW) + w / 2;
        float y = coll[1] * (2.0f / tileH) + h / 2;

        entry.transform.localScale = new Vector3(w, h, 1);
        entry.transform.localPosition = new Vector3(x - 1, y - 1, 0);
      }

      for (int i = 0; i < lamp.Length; i++)
      {
        int[] coll = lamp[i];

        float w = (2.0f / tileW);
        float h = (2.0f / tileH);
        int type = coll[2];

        float x = coll[0] * (2.0f / tileW) + w / 2;
        float y = coll[1] * (2.0f / tileH) + h / 2;

        GameObject entry = null;

        if (type == 0)
          entry = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/" + 2401));
        if (type == 1)
          entry = Object.Instantiate<GameObject>((GameObject)Resources.Load("prop/" + 2402));

        entry.transform.SetParent(root.transform);

        entry.transform.localScale = new Vector3(w / 4, h / 4, 1);
        entry.transform.localPosition = new Vector3(x - 1, y - 1, -1);
      }

      GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).gameObject);

      GadgetCoreAPI.AddCustomResource("z/Subworlds/" + name, gameObject);
    }

    private void CreateJellyfishEnemy(string name, Texture2D texture, Texture2D texture2)
    {
      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("e/wisp"));
      gameObject.name = name;
      gameObject.transform.GetChild(0).GetChild(0).gameObject.name = name;
      Renderer rendererHead = gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetComponentInChildren<Renderer>();
      rendererHead.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = texture2
      };
      Renderer rendererLegs = gameObject.transform.GetChild(0).GetChild(0).GetChild(1).GetComponentInChildren<Renderer>();
      rendererLegs.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = texture
      };
      Component.DestroyImmediate(gameObject.GetComponent<WispScript>());
      gameObject.AddComponent<SpaceJellyfishScript>();

      gameObject.transform.GetChild(1).gameObject.SetActive(true);
      gameObject.transform.GetChild(1).localScale = new Vector3(65, 40, 1);

      GadgetCoreAPI.AddCustomResource("e/Subworlds/" + name, gameObject);
    }

  }
}
