using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UI_TextWindow : UI_Window
{
    [SerializeField] TextMeshProUGUI m_TextRender;
    [SerializeField] float m_TextSpeed = 10.0f;

    private string[] m_nextLine;
    private string m_currentText = "";
    private float m_currentTextIndex = 0;
    private int m_DialogIndex = 0;
    private bool running = false;
    private static UI_TextWindow Instance = null;

    public static System.Action Complete;
    public static void SetTextWindow(string[] nextLine)
    {
        Instance.SetText(nextLine);
    }
    public static void SetTextWindow(DialogueObject dialogue)
    {
        Instance.SetDialogue(dialogue);
    }
    public static bool Advance()
    {
        return Instance.AdvanceDialogue();
    }

    private void Start()
    {
        Instance = this;
        m_State = WindowState.Open;
        //SetText("Hey, what's up. I'm a ghost, and I think that's pretty cool...");
        CloseWindow();
    }

    public void SetText(string nextLine)
    {
        SetText(new string[]{ nextLine });
    }

    public void SetDialogue(DialogueObject dialogue)
    {
        m_currentTextIndex = 0;
        m_DialogIndex = 0;
        m_nextLine = dialogue.dialogues.ToArray();

        CheckLine();
        if (m_State == WindowState.Closed)
        {
            OpenWindow();
        }
    }

    void CheckLine()
    {
        m_currentTextIndex = 0;
        if (m_nextLine[m_DialogIndex][0] == '#')
        {
            print("Bingus");
            string code = m_nextLine[m_DialogIndex];
            RunCommand(code);
            m_DialogIndex++;
            running = false;
            //Do something
        }
        else
        {
            if (m_State == WindowState.Closed)
            {
                OpenWindow();
            }
            running = true;
        }
    }

    void RunCommand(string cmd)
    {
        switch (cmd)
        {
            case "#Test":
                print("Test");
                CloseWindow();
                Invoke("Resume", 1.0f);
                break;
            case "#Inventory":
                print("Test");
                CloseWindow();
                Invoke("Resume", 1.0f);
                break;
            default:
                print("Default");
                Resume();
                break;
        }
    }

    public void SetText(string[] nextLine)
    {
        m_currentTextIndex = 0;
        m_DialogIndex = 0;
        m_nextLine = nextLine;

        if(m_State == WindowState.Closed)
        {
            OpenWindow();
        }
    }

    public void Resume()
    {
        if (m_DialogIndex < m_nextLine.Length)
        {
            running = true;
            OpenWindow();
        }
        else
        {
            if (Complete != null)
                Complete();
            Complete = null;
        }
    }

    private void NextLine()
    {

    }

    public bool AdvanceDialogue()
    {
        if (!running)
            return false;
        if(m_currentTextIndex < m_nextLine[m_DialogIndex].Length)
        {
            m_currentTextIndex = m_nextLine[m_DialogIndex].Length;
        }
        else
        {
            m_currentTextIndex = 0;
            m_DialogIndex++;
            if (m_DialogIndex >= m_nextLine.Length)
            {
                m_nextLine = null;
                CloseWindow();
                if (Complete != null)
                    Complete();
                Complete = null;
                return false;
            }
            else
            {
                CheckLine();
            }
        }
        return false;
    }
    private void Update()
    {
        if(running && m_State == WindowState.Open )
        {
            m_currentTextIndex = Mathf.Min(m_nextLine[m_DialogIndex].Length, m_currentTextIndex + (Time.deltaTime * m_TextSpeed));
            m_currentText = m_nextLine[m_DialogIndex].Substring(0, Mathf.RoundToInt(m_currentTextIndex));
            m_TextRender.text = m_currentText;
        }
    }
    protected override void Cleanup()
    {
        base.Cleanup();

        m_TextRender.text = "";
    }
}
