using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class HiddenObject : MonoBehaviour
{
    private MeshRenderer _renderer;
    private GameManager _gameManager;

    private void Awake()
    {
        _renderer = GetComponent<MeshRenderer>();
        Assert.IsNotNull(_renderer);
        _renderer.enabled = false;
        
        _gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        Assert.IsNotNull(_gameManager);
    }

    public void ShowObjectForSeconds(int seconds)
    {
        if (_renderer.enabled)
        {
            return;
        }
        StartCoroutine(ShowObjectForSecondsEnumerator(seconds));
    }


    private IEnumerator ShowObjectForSecondsEnumerator(int seconds)
    {
        _renderer.enabled = true;
        yield return new WaitForSeconds(seconds);
        _renderer.enabled = false;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (!other.name.Equals("Player"))
        {
            return;
        }
        
        _gameManager.CollectKey();
        Destroy(gameObject);
    }
}
