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

public class QuestionCard : MonoBehaviour
{
    public Text questionText;
    public Text answerText;

    public string questionString = "";
    public string answerString = "";
    public string idString = "";
    public string categoryString = "";

    public Color32 unsafeColor;
    public Color32 safeColor;

    [Serializable]
    public class Question {
        public string question;
        public string answer;
        public string id;
        public string category;
    }

    [Serializable]
    public class Questions {
        public List<Question> questionsList = new List<Question>();
    }

    public void PrepareQuestion() {
        questionText = GameObject.Find("QuestionText").GetComponent<Text>();
        answerText = GameObject.Find("AnswerText").GetComponent<Text>();
    }

    public void PrepareQuestion(string questionTextName, string answerTextName)
    {
        questionText = GameObject.Find(questionTextName).GetComponent<Text>();
        answerText = GameObject.Find(answerTextName).GetComponent<Text>();
    }

    private IEnumerator GetQuestionData(bool showAnswer)
    {
        UnityWebRequest www = UnityWebRequest.Get("nordh.xyz/narKampen/dbFiles/dbAccess.php");

        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        } else {
            while (!www.isDone) {
                yield return null;
            }
            // Show result
            string jsonData = www.downloadHandler.text;

            jsonData = "{\"questionsList\" : [ " + jsonData + " ]}";

            Questions qe = new Questions();
            JsonUtility.FromJsonOverwrite(jsonData, qe);
            if (qe.questionsList.Count > 0 && questionText != null )
            {
                if (showAnswer && answerText != null)
                {
                    answerText.text = qe.questionsList[0].answer;
                }
                questionText.text = qe.questionsList[0].question;
            }
            questionString = qe.questionsList[0].question;
            answerString = qe.questionsList[0].answer;
            idString = qe.questionsList[0].id;
            categoryString = qe.questionsList[0].category;
        }
    }

    public void SetQuestionSafe()
    {
        this.GetComponent<Image>().color = safeColor;
    }

    public bool IsQuestionSafe() {
        if (this.GetComponent<Image>().color == safeColor) {
            return true;
        }
        return false;
    }

    public void SetQuestionText(string text) {
        this.questionText.text = text;
    }
    public void SetAnswerText(string text) {
        this.answerText.text = text;
    }

    public Text GetQuestionText() {
        return this.questionText;
    }

    public Text GetAnswerText() {
        return this.answerText;
    }
}
