using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Treasure : MonoBehaviour
{    
    [SerializeField] LayerMask PlayerMask;
    [SerializeField] AudioClip SFXPickUp;
    [SerializeField] Animator Animator;
    [SerializeField] float Delay = 3f;

    public static System.Action OnGameWon;

    private void OnCollisionEnter(Collision collision)
    {
        if (PlayerMask != (PlayerMask | (1 << collision.gameObject.layer))) return;

        StartCoroutine(Win());
    }

    private IEnumerator Win()
    {
        if (SFXPickUp != null)
            AudioSource.PlayClipAtPoint(SFXPickUp, transform.position);
        if (Animator != null)
            Animator.SetTrigger("Pickup");

        OnGameWon?.Invoke();
        yield return new WaitForSeconds(Delay);

        SceneManager.LoadScene("Menu");
        Destroy(gameObject);
    }

    public void OnAnimationEnd()
    {
    }
}
