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

public class OldChestScript : MonoBehaviour
{
  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);
    var item = GadgetCoreAPI.EmptyItem();
    item.id = Core.itemOldTex.GetID();
    GadgetCoreAPI.DropItemLocal(gameObject.transform.position, item);
    yield return new WaitForSeconds(0.2f);
    GadgetCoreAPI.SpawnExp(gameObject.transform.position, 2);
    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("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
    };
  }
}
