/*
 *      Script par Tanguy Gimenez      
 *
 */

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.SceneManagement;

public class Dialog : MonoBehaviour
{
    public GameObject dialogBox;
    public Text dialogText;
    public int dialog;
    public bool dialogActive;


    // clé : nom de la scène
    // valeur : tableau de string contenant chaque réplique
    private Dictionary<string, string[]> ListDialogs = new Dictionary<string, string[]>();

    // Start is called before the first frame update
    void Start()
    {
        this.dialog = 0;

        string[] DialogsLvl1 = {
            "Coucou",
            "Je fais un test pour le niveau 1",
            "Ca marche bien nan ?",
            "Melvyn t moch",
            "Valentin aussi"
            };

        this.ListDialogs.Add("sceneTanguy", DialogsLvl1);

        if (this.dialogActive)
            this.dialogText.text = this.ListDialogs[SceneManager.GetActiveScene().name][0];
    }

    // Update is called once per frame
    void Update()
    {
        if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown("joystick button 0")) && dialogActive)
            this.Next();

        // DEBUG
        if ((Input.GetKeyDown(KeyCode.H) || Input.GetKeyDown("joystick button 1")))
            this.dialogActive = !this.dialogActive;

        if (!dialogActive)
        {
            this.dialogBox.SetActive(false);
        }
        else
        {
            this.dialogBox.SetActive(true);
        }
    }

    void Next()
    {
        string sceneActuelle = SceneManager.GetActiveScene().name;

        if (this.dialog >= this.ListDialogs[sceneActuelle].Length)
            this.dialogActive = false;
        else
        {
            this.dialogText.text = this.ListDialogs[sceneActuelle][this.dialog];
            this.dialog++;
        }
    }
}