using System;
using Sources;
using Sources.Gui;
using UnityEngine;
using UnityEngine.UI;
using Zenject;

public class Fremd : MonoBehaviour
{
    [SerializeField] private string[] texts;
    [SerializeField] private string name;

    [Inject] private GuiController _guiController;

    private int _currentIndex = -1;
    
    private void OnTriggerEnter(Collider other)
    {
        var player = other.GetComponentInParent<Player>();
        if (!player)
            return;

        _currentIndex = 0;
        SetMessage(_currentIndex, texts.Length > 1);
    }

    private void OnTriggerExit(Collider other)
    {
        var player = other.GetComponentInParent<Player>();
        if (!player)
            return;
        
        _guiController.SetDialogVisible(false);
        _currentIndex = -1;
    }

    private void Update()
    {
        if (_currentIndex < 0 || _currentIndex >= texts.Length - 1)
            return;

        if (Input.GetKeyDown(KeyCode.F))
        {
            _currentIndex++;
            SetMessage(_currentIndex, _currentIndex < texts.Length - 1);
        }
    }

    private void SetMessage(int index, bool showNext)
    {
        var currentText = texts[index];
      
        _guiController.SetDialogVisible(true, name, currentText, showNext);
    }
}
