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

[System.Serializable]
public class aQuestion
{
    public string aquestion;
    public string[] answers;
    public int good_answer;
    public int type;
}

[System.Serializable]
public class aQuestions
{
    public aQuestion[] aquestions;
}

public class QsuizzManager : MonoBehaviour
{
    public GameObject LoadingCanvas;
    public GameObject TutorialCanvas;
    public GameObject aQuestions;
    public GameObject Home;
    public TextAsset jsonFile;
    private bool launched;
    private bool started;
    private int answer_button;

    void Start() {
        LoadingCanvas.GetComponent<Canvas>().enabled = true;
        TutorialCanvas.GetComponent<Canvas>().enabled = false;
        aQuestions.GetComponent<Canvas>().enabled = false;
        Home.GetComponent<Canvas>().enabled = false;
        launched = false;
        started = false;
    }

    public void ShowTutorial() {
        if (!launched) {
            LoadingCanvas.GetComponent<Canvas>().enabled = false;
            TutorialCanvas.GetComponent<Canvas>().enabled = true;
            launched = true;
        }
    }

    public void LaunchQuizz() {
        if (!started) {
            TutorialCanvas.GetComponent<Canvas>().enabled = false;
            Home.GetComponent<Canvas>().enabled = true;
            started = true;
        }
    }

    public void LeaveApp() {
        Application.Quit();
    }

    private void YesNoaQuestion(aQuestion aquestion) {
        GameObject.Find("aQuestion").GetComponent<TMP_Text>().text = aquestion.aquestion;
        GameObject.Find("AnswerA").GetComponentInChildren<TMP_Text>().text = aquestion.answers[0];
        GameObject.Find("AnswerB").GetComponentInChildren<TMP_Text>().text = aquestion.answers[1];
        GameObject.Find("HidingCanva").GetComponent<Canvas>().enabled = false;
    }

    private void ClassicaQuestion(aQuestion aquestion) {
        GameObject.Find("aQuestion").GetComponent<TMP_Text>().text = aquestion.aquestion;
        GameObject.Find("HidingCanva").GetComponent<Canvas>().enabled = true;
        GameObject.Find("AnswerA").GetComponentInChildren<TMP_Text>().text = aquestion.answers[0];
        GameObject.Find("AnswerB").GetComponentInChildren<TMP_Text>().text = aquestion.answers[1];
        GameObject.Find("AnswerC").GetComponentInChildren<TMP_Text>().text = aquestion.answers[2];
        GameObject.Find("AnswerD").GetComponentInChildren<TMP_Text>().text = aquestion.answers[3];
    }

    public void StartQuizz() {
        Home.GetComponent<Canvas>().enabled = false;
        aQuestions.GetComponent<Canvas>().enabled = true;    
        aQuestions aquestions = JsonUtility.FromJson<aQuestions>(jsonFile.text);
        for (int i = 0; i < aquestions.aquestions.Length; i++) {
            aQuestion aquestion = aquestions.aquestions[i];
            if (aquestion.type == 0) {
                ClassicaQuestion(aquestion);
            } else {
                YesNoaQuestion(aquestion);
            }
        }
    }
}
