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

public class DialogueManager : MonoSingleton<DialogueManager>
{
    public delegate void DialogueMagD(FixedDialogue d, string speaker, string text);

    public event DialogueMagD onStartDialogue;
    public event DialogueMagD onAdvanceDialogue;
    public event DialogueMagD onEndDialogue;

    protected FixedDialogue _d;


    protected override void Awake()
    {
        base.Awake();
    }

    public void StartDialogue(FixedDialogue d, string speaker, string line)
    {
        _d = d;

        PlayerController plr = GameObject.FindObjectOfType<PlayerController>();
        plr.Freeze();

        onStartDialogue?.Invoke(d, speaker, line);
    }

    public void Advance()
    {
        if (_d == null)
            return;

        _d.Advance();
    }

    private void Update()
    {
        bool e = Input.GetKeyDown(KeyCode.E);
        if (e)
            Advance();
    }

    public void AdvanceDialogue(FixedDialogue d, string speaker, string line)
    {
        onAdvanceDialogue?.Invoke(d, speaker, line);
    }

    public void EndDialogue(FixedDialogue d, string speaker, string line)
    {
        PlayerController plr = GameObject.FindObjectOfType<PlayerController>();
        plr.UnFreeze();

        _d = null;
        onEndDialogue?.Invoke(d, speaker, line);
    }
}