using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class DialogueTextUI : MonoBehaviour
{
    [SerializeField] protected TextMeshProUGUI _nextText;
    [SerializeField] protected Image _textbox;
    protected DialogueDisplayer _displayer;

    private void Awake()
    {
        _displayer = GetComponent<DialogueDisplayer>();
        DialogueManager.instance.onStartDialogue += OnStart;
        DialogueManager.instance.onAdvanceDialogue += OnAdvance;
        DialogueManager.instance.onEndDialogue += (x,y,z) => { OnEnd(); };
        _textbox.gameObject.SetActive(false);
    }

    public void Advance()
    {
        DialogueManager.instance.Advance();
    }

    void OnStart(FixedDialogue d,string speaker, string text)
    {
        OnAdvance(d,speaker, text);
    }

    void OnAdvance(FixedDialogue d, string speaker,string text)
    {
        _textbox.gameObject.SetActive(true);
        _nextText.text = d.idx<d.lines.Length ? "next" : "done";
        _displayer.ShowLine(speaker,text);
    }

    void OnEnd ()
    {
        _textbox.gameObject.SetActive(false);
    }
}