using GadgetCore.API;
using System.Collections;
using UnityEngine;

namespace Subworlds.Scripts
{
  public class OldChestScript : MonoBehaviour
  {
    private static int[] dropsLoot = new int[] { 1, 2, 3, 4, 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34 };
    private static int[] dropsEmblem = new int[] { 101, 102, 103, 104, 111, 112, 113, 114, 121, 122, 123, 124, 131, 132, 133, 134 };

    private bool opened = false;

    private void Awake()
    {
      base.name = "chest";
    }

    private void Start() { }

    [RPC]
    private void Open()
    {
      if (!this.opened)
      {
        this.opened = true;
        base.GetComponent<NetworkView>().RPC("OpenMat", RPCMode.AllBuffered, new object[0]);
        this.DropItems();
      }
    }

    private void DropItems()
    {
      base.GetComponent<NetworkView>().RPC("DropLocal", RPCMode.All, new object[0]);
    }

    [RPC]
    private void DropLocal()
    {
      if (Menuu.curAugment == 12)
        StartCoroutine(DropLocalLoot());
      StartCoroutine(DropLocalLoot());
    }

    private IEnumerator DropLocalLoot()
    {
      yield return new WaitForSeconds(0.2f);
      if (Random.Range(0, 3) == 0)
      {
        var item = GadgetCoreAPI.EmptyItem();
        item.id = Core.itemOldTex.GetID();
        GadgetCoreAPI.DropItemLocal(gameObject.transform.position, item);
      }
      else
      {
        var item = GadgetCoreAPI.EmptyItem();
        item.id = dropsEmblem[Random.Range(0, dropsEmblem.Length)];
        GadgetCoreAPI.DropItemLocal(gameObject.transform.position, item);
      }
      for (int i = 0; i < 3; i++)
      {
        if (Random.Range(0, 3) > 0)
        {
          var item = GadgetCoreAPI.EmptyItem();
          item.id = dropsLoot[Random.Range(0, dropsLoot.Length)];
          GadgetCoreAPI.DropItemLocal(gameObject.transform.position, item);
        }
      }
      yield return new WaitForSeconds(0.2f);
      GadgetCoreAPI.SpawnExp(gameObject.transform.position, 25);
      yield return new WaitForSeconds(0.2f);
      GadgetCoreAPI.SpawnExp(gameObject.transform.position, 10);
      yield return new WaitForSeconds(0.2f);
      GadgetCoreAPI.SpawnExp(gameObject.transform.position, 20);
      yield break;
    }

    private static Texture2D textureOldChestOpen = GadgetCoreAPI.LoadTexture2D("Ship/oldChestOpen.png");

    [RPC]
    private void OpenMat()
    {
      Menuu.characterStat[8]++;
      GameScript.cadetValue += 2;
      base.GetComponent<AudioSource>().PlayOneShot((AudioClip)Resources.Load("Au/chest"), Menuu.soundLevel / 10f);
      base.GetComponent<Collider>().enabled = false;
      Renderer renderer = gameObject.transform.GetChild(0).gameObject.GetComponentInChildren<Renderer>();
      renderer.material = new Material(Shader.Find("Unlit/Transparent"))
      {
        mainTexture = textureOldChestOpen
      };
    }
  }
}