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

namespace Subworlds
{
  public partial class SubworldsGadget : Gadget<SubworldsGadget>
  {
    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 CreateObject(string name, Texture2D texture, bool large = false)
    {
      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
      };
      if (large)
        gameObject.transform.localScale = new Vector3(gameObject.transform.localScale.x * 2, gameObject.transform.localScale.y * 2, 1);
      GadgetCoreAPI.AddCustomResource("obj/Subworlds/" + name, gameObject);
    }

    private void CreateBrakableObject(string name, Texture2D texture, bool large = false, ObjectType type = ObjectType.ORE)
    {
      var i = new ObjectInfo(type, new Item(57, 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);
      Core.spawnObjects.Add(i.GetID());
    }

    private void CreateSliverEnemy(string name, Texture2D textureHead, Texture2D textureBody, Texture2D textureTail)
    {
      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("e/sliver"));
      Renderer rendererHead = gameObject.transform.GetChild(4).GetChild(0).GetChild(0).GetComponentInChildren<Renderer>();
      rendererHead.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = textureHead
      };
      Renderer rendererBody = gameObject.transform.GetChild(4).GetChild(0).GetChild(1).GetComponentInChildren<Renderer>();
      rendererBody.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = textureBody
      };
      Renderer rendererTail = gameObject.transform.GetChild(4).GetChild(0).GetChild(2).GetComponentInChildren<Renderer>();
      rendererTail.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = textureTail
      };
      GadgetCoreAPI.AddCustomResource("e/Subworlds/" + name, gameObject);
    }

    private void CreateDroidEnemy(string name, Texture2D texture)
    {
      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 = texture
      };
      GameObject.DestroyImmediate(gameObject.transform.GetChild(0).GetChild(0).GetChild(1).gameObject);
      Component.DestroyImmediate(gameObject.GetComponent<WispScript>());
      gameObject.AddComponent<AttackDroidScript>();

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

      AnimationClip clip = new AnimationClip
      {
        name = "spinn",
        wrapMode = WrapMode.Once,
        legacy = true
      };
      clip.SetCurve("", typeof(Transform), "localEulerAngles.z", AnimationCurve.Linear(0, 0, 0.5f, 360));
      var animation = gameObject.transform.GetChild(0).gameObject.AddComponent<Animation>();
      animation.AddClip(clip, "spinn");
      animation.clip = clip;

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

    private void CreateLaserHazard(string name, Texture2D texture)
    {
      GameObject gameObjectWisp = Object.Instantiate<GameObject>((GameObject)Resources.Load("e/wisp"));
      var gameObject = gameObjectWisp.transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
      gameObjectWisp.transform.GetChild(3).SetParent(gameObject.transform);
      gameObject.transform.SetParent(null);
      GameObject.DestroyImmediate(gameObjectWisp);
      gameObject.name = name;
      Renderer rendererHead = gameObject.GetComponentInChildren<Renderer>();
      rendererHead.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = texture
      };
      gameObject.AddComponent<BrokenLaserScript>();

      var haz = gameObject.transform.GetChild(0).GetComponent<HazardScript>();
      haz.damage = 3;

      gameObject.transform.localScale = new Vector3(2f, 8f, 1);
      gameObject.transform.localRotation = Quaternion.Euler(0, 180, 0);
      gameObject.transform.GetChild(0).localScale = new Vector3(1, 1, 1);
      var trigger = gameObject.transform.GetChild(0).GetComponent<BoxCollider>();
      trigger.size = new Vector3(0.6f, 2, 1);
      trigger.isTrigger = true;
      var collider = gameObject.transform.GetChild(0).gameObject.AddComponent<BoxCollider>();
      collider.size = new Vector3(0.3f, 2, 1);
      collider.isTrigger = false;

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

    private void CreateOldChest(string name, Texture texture)
    {
      GameObject gameObject = Object.Instantiate<GameObject>((GameObject)Resources.Load("obj/chest"));
      Renderer renderer = gameObject.transform.GetChild(0).gameObject.GetComponentInChildren<Renderer>();
      renderer.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = texture
      };
      Component.DestroyImmediate(gameObject.GetComponent<ChestScript>());
      gameObject.AddComponent<OldChestScript>();
      GadgetCoreAPI.AddCustomResource("obj/Subworlds/" + name, gameObject);
    }
    private void CreateBaseBlock(string name, Texture2D texture, float w, float h)
    {
      var gameObject = Object.Instantiate((GameObject)Resources.Load("z/chunk"));
      gameObject.transform.localScale = new Vector3(w, h, 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>());


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

      collision.transform.localScale = new Vector3(1, 1, 1);
      collision.transform.localPosition = new Vector3(0, 0, 0);

      Core.logger.LogConsole("created: " + "z/Subworlds/" + name);

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

  }
}
