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

public class CinematicDialogueManager : MonoBehaviour
{
    public static CinematicDialogueManager instance;
    public DialogueAudio dialogueAudio;
    public Animator typeWriterAnimator;
    public GameObject typeWriteHead;
    private string executeTypingAnimation = "StartTyping";
    private Queue<string> _sentences;
    //public GameObject heroImageGameObject;
    //public GameObject dialogueCanvasBkg;


    //public TextMeshProUGUI nameText;
    public TextMeshProUGUI dialogueTextTmPro;
    public GameObject dialogueCanvas;

    public GameObject continueButton;
    public GameObject endButton;

    public PlayableDirector playableDirector;
    public DialogueData testDialogue;

    void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {
        dialogueCanvas.SetActive(false);
        _sentences = new Queue<string>();
    }

    private void Update()
    {
        if (Input.GetKeyUp(KeyCode.K))
        {
            StartDialogue(testDialogue);
        }
    }

    //Open dialogue box with dialogue for player
    public void StartDialogue(DialogueData dialogue)
    {
        if (dialogueCanvas.activeSelf == false)
        {
            if (playableDirector)
            {
                playableDirector.Pause();
            }
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
            _sentences = new Queue<string>();
            dialogueCanvas.SetActive(true);
            DialougeInteraction(dialogue);
            continueButton.SetActive(true);
            endButton.SetActive(false);
        }
        else
        {
            DialougeInteraction(dialogue);
        }
    }
    
    //Queues strings to appear one after the other
    private void DialougeInteraction(DialogueData dialogue)
    {
        foreach (string sentence in dialogue.conversationBlock)
        {
            _sentences.Enqueue(sentence);
        }

        DisplayNextSentence();
    }
    
    int letterNum = 1;
    //Used to move threw the queue
    public void DisplayNextSentence()
    {
        if (_sentences.Count == 0)
        {
            continueButton.SetActive(false);
            endButton.SetActive(true);
        }
        else
        {
            string sentence = _sentences.Dequeue();
            StopAllCoroutines();
            letterNum = 1;
            dialogueTextTmPro.text = "";
            StartCoroutine(TypeSentence(sentence));
        }
    }


    //Prints every char of the sentence one after the other
    IEnumerator TypeSentence(string sentence)
    {
        if (typeWriteHead.transform.localPosition.x >= 42f)
        {
            AudioManager.instance.PlaySounds("TypeWriter_Ding");
            AudioManager.instance.PlaySounds("Typewriter_Typing");
            typeWriteHead.transform.localPosition = 
                new Vector3(0f, typeWriteHead.transform.localPosition.y);
            StartCoroutine(TypeSentence(sentence));
        }
        else
        {
            //typeWriterAnimator.SetTrigger(executeTypingAnimation);
            Char[] sentenceChars = sentence.ToCharArray();
            typeWriteHead.transform.localPosition = 
                new Vector3(typeWriteHead.transform.localPosition.x + 1f, typeWriteHead.transform.localPosition.y);
            dialogueAudio.ReproduceSound(sentenceChars[letterNum-1]);
            dialogueTextTmPro.text += sentenceChars[letterNum-1];
            yield return new WaitForSeconds(0.06f);
            letterNum++;
            if (letterNum <= sentenceChars.Length)
            {
                StartCoroutine(TypeSentence(sentence));
            }
            else
            {
                typeWriteHead.transform.localPosition = 
                    new Vector3(0f, typeWriteHead.transform.localPosition.y);
                AudioManager.instance.StopSounds( "Typewriter_Typing");
                AudioManager.instance.PlaySounds("TypeWriter_Ding");
            }
        }
    }

    
    public void ExitDialogue()
    {
        //dialogueCanavsAnimator.SetBool(executeAnimation, false);
        _sentences.Clear();
        dialogueCanvas.SetActive(false);
        StopAllCoroutines();
        if (playableDirector)
        {
            playableDirector.Play();
        }
    }
    
    public void ClearVariables()
    {
        //nameText.text = "";
        //heroImageGameObject.GetComponent<Image>().sprite = null;
        dialogueCanvas.SetActive(false);
    }
}
