using UnityEngine;

namespace MenuMap
{
    public class NPC_Controller: MonoBehaviour
    {
        [SerializeField] private string[] dialogues;
        private bool _playerStayInTrigger;
        private int _currentDialogue;

        void Update()
        {
            if (_playerStayInTrigger)
            {
                if (Input.GetKeyDown(KeyCode.X))
                {

                    if (_currentDialogue >= dialogues.Length)
                    {
                        MapCharacterController.BlockMovement?.Invoke(false);
                        DialoguePanel.ShowDialogues?.Invoke(false);
                        _currentDialogue = 0;
                        return;
                    }
                    
                    MapCharacterController.BlockMovement?.Invoke(true);
                    DialoguePanel.ShowDialogues?.Invoke(true);
                    DialoguePanel.UpdateText?.Invoke(dialogues[_currentDialogue]);
                    _currentDialogue++;
                }
            }
        }
        
        private void OnTriggerEnter2D(Collider2D col)
        {
            if (col.CompareTag("Player"))
            {
                _playerStayInTrigger = true;
                DialoguePanel.ShowOpenKey?.Invoke(_playerStayInTrigger);
            }
        }

        private void OnTriggerExit2D(Collider2D col)
        {
            if (col.CompareTag("Player"))
            {
                _playerStayInTrigger = false;
                DialoguePanel.ShowOpenKey?.Invoke(_playerStayInTrigger);
            }
        }
    }
}