using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class UI : MonoBehaviour
{
    public static UI Instance;
    public GameObject canvas;
    public GameObject clickBlocker;
    public Button next;
    private String buttonText;
    
    public Text text;

    private List<String> pages = new List<string>();
    private int page_i = 0;
    private bool displaying = false;

    private UnityAction messageFinishedCallback;

    private void Awake()
    {
        Instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        next.onClick.AddListener(nextPage);
    }

    // Update is called once per frame
    void Update()
    {
        canvas.SetActive(displaying);
        clickBlocker.SetActive(displaying);

        if (displaying)
        {
            text.text = pages[page_i];
        }
    }

    public static void displayMessage(List<String> pages, UnityAction callback = null, String buttonText = null)
    {
        Instance.pages.Clear();
        Instance.pages.AddRange(pages);
        Instance.page_i = 0;
        Time.timeScale = 0;
        Instance.displaying = true;
        ZoomWindow.Instance.close();
        Instance.messageFinishedCallback = callback;
        Instance.buttonText = buttonText;
    }

    void setButtonText()
    {
        if (page_i == pages.Count-1)
        {
            if (buttonText != null)
            {
                next.GetComponentInChildren<Text>().text = buttonText;
            }
            else
            {
                next.GetComponentInChildren<Text>().text = "Continue";
            }
        }
        else
        {
            next.GetComponentInChildren<Text>().text = "Next";
        }
    }

    public void nextPage()
    {
        if (page_i < pages.Count-1)
        {
            page_i++;
            setButtonText();
        }
        else
        {
            endDisplay();
        }
    }

    public static void endDisplay()
    {
        Instance.displaying = false;
        Time.timeScale = 1;

        if (Instance.messageFinishedCallback != null)
        {
            Instance.messageFinishedCallback();
        }
    }
}
