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

public class UIManager : MonoBehaviour
{
    public static UIManager Instance;
    public Animator notepadAnimator;
    public string animatorTurnPage;
    public GameObject containter_NPC;
    public GameObject container_PLAYER;
    public Image image_Player;
    public Image image_NPC;
    public DialogueAudio dialogueAudio;
    public TextMeshProUGUI text_NPC;
    public TextMeshProUGUI[] text_Choices;

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);
    }

    // Start is called before the first frame update
    void Start()
    {
        containter_NPC.SetActive(false);
        container_PLAYER.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (VD.isActive)
            {
                VD.Next();
            }
        }
    }

    private string genericNpc = null;
    public void Begin(VIDE_Assign dialogueData, string genericNpcString = null)
    {
        genericNpc = genericNpcString;
        VD.OnNodeChange += UpdateUi;
        VD.OnEnd += End;
        VD.BeginDialogue(dialogueData);
    }

    void UpdateUi(VD.NodeData data)
    {
        containter_NPC.SetActive(true);
        container_PLAYER.SetActive(false);
        if (data.isPlayer)
        {
            container_PLAYER.SetActive(true);
            image_Player.sprite = data.sprite;
            
            for (int i = 0; i < text_Choices.Length; i++)
            {
                if (i < data.comments.Length)
                {
                    text_Choices[i].transform.parent.gameObject.SetActive(true);
                    text_Choices[i].text = data.comments[i];
                }
                else
                {
                    text_Choices[i].transform.parent.gameObject.SetActive(false);
                }
            }
        }
        else
        {
            if (genericNpc != null)
            {
                data.comments[0] = genericNpc;
            }
            containter_NPC.SetActive(true);
            image_NPC.sprite = data.sprite;
            StartCoroutine(TypeSentence(data.comments[data.commentIndex]));
        }
    }
    
    IEnumerator TypeSentence(string sentence)
    {
        AudioManager.instance.PlaySounds("Pencil");
        text_NPC.text = "";
        notepadAnimator.SetTrigger(animatorTurnPage);
        yield return new WaitForSeconds(0.30f);
        foreach (char letter in sentence.ToCharArray())
        {
            dialogueAudio.ReproduceSound(letter);
            text_NPC.text += letter;
            yield return new WaitForSeconds(0.05f);
        }
        AudioManager.instance.StopSounds("Pencil");
    }

    void End(VD.NodeData data)
    {
        containter_NPC.SetActive(false);
        container_PLAYER.SetActive(false);
        VD.OnNodeChange -= UpdateUi;
        VD.OnEnd -= End;
        VD.EndDialogue();
    }

    private void OnDisable()
    {
        if (containter_NPC != null)
        {
            End(null);
        }
    }

    public void SetPlayerChoice(int choice)
    {
        VD.nodeData.commentIndex = choice;
        if (Input.GetMouseButtonUp(0))
        {
            VD.Next();
        }
    }
}
