using UnityEngine;

namespace Nan.Scripts.Stage
{
    public class Exit : MonoBehaviour
    {
        [SerializeField] private AudioSource _audioSource;
        public PlayerControl PlayerControl { get; set; }

        private void Update()
        {
            GetComponent<Animator>().SetBool("Opened", PlayerControl.HasKey);
        }

        private void OnTriggerEnter2D(Collider2D other)
        {
            var player = other.GetComponent<PlayerControl>();
            if (player == null) return;
            if (!player.HasKey) return;
            
            ScoreManager.Instance.AddScore(10000, transform.position);

            _audioSource.Play();
            _audioSource.transform.SetParent(null);
            
            Destroy(_audioSource.gameObject, 1);
            Destroy(this);
        }
    }
}
