using GadgetCore.API;
using ScrapYard.API;
using System;
using System.Reflection;
using UnityEngine;

namespace ScrapYard
{
  public class ScrapYardGenerator
  {
    private static readonly FieldInfo chunksField = typeof(SpawnerScript).GetField("chunks", BindingFlags.NonPublic | BindingFlags.Instance);
    private static readonly Material materialaParalex0 = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("bScrapYardbg0.png") };
    private static readonly Material materialaParalex1 = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("bScrapYardbg1.png") };
    private static readonly Material materialaParalex2 = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("bScrapYardbg2.png") };
    private static readonly Material materialaParalex3 = new Material(Shader.Find("Unlit/Transparent")) { mainTexture = GadgetCoreAPI.LoadTexture2D("bScrapYardbg3.png") };

    public static readonly FieldInfo networkStuffField = typeof(Chunk).GetField("networkStuff", BindingFlags.NonPublic | BindingFlags.Instance);
    public static readonly FieldInfo tempField = typeof(Chunk).GetField("temp", BindingFlags.NonPublic | BindingFlags.Instance);

    public static void GenarateTown(SpawnerScript s, int[] param)
    {
      try
      {
        s.backLights.SetActive(true);
        s.mainLight.SetActive(true);
        s.mainLight.GetComponentInChildren<Light>().color = new Color(0.05f, 0.05f, 0.05f, 0.1f);

        s.bg[0].GetComponent<Renderer>().material = materialaParalex0;
        s.bg[1].GetComponent<Renderer>().material = materialaParalex1;
        s.bg[2].GetComponent<Renderer>().material = materialaParalex2;
        s.bg[3].GetComponent<Renderer>().material = materialaParalex3;

        var chunks = (GameObject[])chunksField.GetValue(s);
        var num = 0;

        chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/entrance"), new Vector3(224.5f, 0f, 5f), Quaternion.Euler(0f, 0f, 0f));
        num++;
        chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/ScrapYard/chunkBase"), new Vector3((float)(300 + 0 * 128), 0f, 5f), Quaternion.Euler(0f, 180f, 180f));
        num++;
        int rowsNeeded = 0;
        foreach (var entry in ShopPlatform.ShopPlatforms)
        {
          rowsNeeded += entry.GetRowsNeeded();
        }
        int extensionAmount = System.Math.Max((rowsNeeded + 3) / 8, 0);
        for (int i = 0; i < extensionAmount; i++)
        {
          chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/ScrapYard/chunkExtension"), new Vector3((float)(300 + 0 * 128), 64 * (i + 1), 5f), Quaternion.Euler(0f, 180f, 180f));
          num++;
        }
        chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/ScrapYard/chunkTop"), new Vector3((float)(300 + 0 * 128), 64 * (extensionAmount + 1), 5f), Quaternion.Euler(0f, 180f, 180f));
        num++;
        chunks[num] = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("z/exit"), new Vector3(375.5f, 0f, 5f), Quaternion.Euler(0f, 0f, 180f));
        num++;
        if (Network.isServer)
        {
          chunks[num] = new GameObject("chunk");
          var chunk = chunks[num].AddComponent<Chunk>();
          PopulateTown(chunk);
          num++;
        }
      }
      catch (System.Exception e) { Core.logger.LogConsole(e); }
    }

    public static void PopulateTown(Chunk chunk)
    {
      var networkStuff = new GameObject[400];
      networkStuffField.SetValue(chunk, networkStuff);

      try
      {

        int temp = (int)tempField.GetValue(chunk);

        networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("prop/ScrapYard/scrapYardShopBuilding"), new Vector3(250f, -6f + 0.118f, 0.3f), Quaternion.identity, 0);
        temp++;
        networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("prop/ScrapYard/scrapYardMerchant"), new Vector3(255f, -6f - 0.118f * 12, 0.2f), Quaternion.identity, 0);
        temp++;
        networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npcStorage"), new Vector3(234.5f, -6.118f, 0.2f), Quaternion.identity, 0);
        temp++;
        networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npcStorage"), new Vector3(362f, -6.118f, 0.2f), Quaternion.identity, 0);
        temp++;
        networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("npc2103"), new Vector3(366f, -6.118f, 0.2f), Quaternion.identity, 0);
        temp++;

        int rowNumber = -1;
        foreach (var platform in ShopPlatform.ShopPlatforms)
        {
          for (int i = 0; i < platform.Entries.Count; i++)
          {
            ShopPlatformEntry entry = platform.Entries[i];
            if ((i % platform.GetSpacesPerRow()) == 0)
            {
              rowNumber++;
              networkStuff[temp] = (GameObject)Network.Instantiate(Resources.Load("prop/ScrapYard/metalPlatform"), new Vector3(300, rowNumber * 8f, 0.4f), Quaternion.identity, 0);
              temp++;
            }
            var step = (i % platform.GetSpacesPerRow()) / (float)(platform.GetSpacesPerRow() - 1);
            networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(300 - (40 / 2) + (step * 40), rowNumber * 8f + 0.5f - 0.05f, 0.2f), Quaternion.identity, 0);
            networkStuff[temp].GetComponent<ScrapYardShopStandScript>().StartCallSet(entry.ItemID, entry.Price, entry.Quantity, entry.CurrencyItemID);
            temp++;
          }
        }
        networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(268.5f, -6.1175f - 1.45f, 0.2f), Quaternion.identity, 0);
        networkStuff[temp].GetComponent<ScrapYardShopStandScript>().StartCallSet(ItemRegistry.GetItemIDByRegistryName("ScrapYard:Scrap Trophy"), 9999, 1, 57);
        temp++;
        networkStuff[temp] = (GameObject)Network.Instantiate((GameObject)Resources.Load("prop/ScrapYard/stand"), new Vector3(272.5f, -6.1175f - 1.45f, 0.2f), Quaternion.identity, 0);
        networkStuff[temp].GetComponent<ScrapYardShopStandScript>().StartCallSet(57, 1, 9999, ItemRegistry.GetItemIDByRegistryName("ScrapYard:Scrap Trophy"));
        temp++;
      }
      catch (Exception e) { Core.logger.LogError(e); }
      GameScript.endPortal[3] = (GameObject)Network.Instantiate((GameObject)Resources.Load("portal"), new Vector3(226f, -6.118f, 0f), Quaternion.identity, 0);
      GameScript.endPortalUA[3] = GameScript.endPortal[3].transform.GetChild(0).gameObject;
      GameScript.endPortal[3].GetComponent<NetworkView>().RPC("Activate", RPCMode.All, new object[0]);
      GameScript.endPortalUA[3].GetComponent<NetworkView>().RPC("Set", RPCMode.AllBuffered, new object[] { 98, 0, 3 });
    }
  }
}
