using UnityEngine;
using TMPro;
using System.Collections;
using UnityEngine.InputSystem;

public class Dialogue : MonoBehaviour
{

    public TextMeshProUGUI textComp;
    public TextMeshProUGUI textSpeaker;
    private string[] lines; //
    public float textSpeed;
    public InputActionReference interact;
    public GameObject top;
    private int index; //
    public bool textActive;
    private bool slightDelay;
    private string sendDoneLocation = string.Empty;
    private GameObject source = null;
    private bool sendReturn;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        textComp.text = string.Empty;
        interact.action.started += Interact;
    }

    private void Interact(InputAction.CallbackContext ctx)
    {
        if(textActive)
        {
            if(textComp.text == lines[index])
            {
                NextLine();
            }
            else
            {
                if(!slightDelay)
                {
                    StopAllCoroutines();
                    textComp.text = lines[index];
                }
            }
        }
    }

    public void StartDialogue(string[] inLines)
    {
        sendReturn = false;
        gameObject.SetActive(true);
        top.SetActive(false);
        index = 0;
        lines = inLines;
        textActive = true;
        slightDelay = true;
        textComp.text = string.Empty;
        StartCoroutine(TypeLine());
    }

    public void StartDialogue(string[] inLines, string speaker, GameObject src, string callBack)
    {
        gameObject.SetActive(true);
        top.SetActive(true);
        index = 0;
        lines = inLines;
        textActive = true;
        slightDelay = true;

        textComp.text = string.Empty;
        textSpeaker.text = speaker;

        Debug.Log("src is " + src);

        source = src;
        sendDoneLocation = callBack;

        Debug.Log("Source is " + source);

        sendReturn = true;
        
        StartCoroutine(TypeLine());
    }

    public void StartDialogue(string[] inLines, string speaker)
    {
        sendReturn = false;
        gameObject.SetActive(true);
        top.SetActive(true);
        index = 0;
        lines = inLines;
        textActive = true;
        slightDelay = true;

        textComp.text = string.Empty;
        textSpeaker.text = speaker;

        
        StartCoroutine(TypeLine());
    }

    IEnumerator TypeLine()
    {
        yield return new WaitForEndOfFrame();
        slightDelay = false;
        foreach(char c in lines[index].ToCharArray())
        {
            textComp.text += c;
            yield return new WaitForSeconds(textSpeed);
        }
    }

    void NextLine()
    {
        if(index < lines.Length - 1)
        {
            index++;
            textComp.text = string.Empty;
            StartCoroutine(TypeLine());
        }
        else
        {
            textActive = false;
            textComp.text = string.Empty;
            lines = new string[0];

            top.SetActive(false);
            gameObject.SetActive(false);


            if (sendReturn)
            {
                Debug.Log(source);
                Debug.Log(sendDoneLocation);
                source.SendMessage(sendDoneLocation);
            }
        }
    }
}
