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

public class AnswerQuestion : MonoBehaviour {

    class Question
    {
        public string QuestionContent { get; set; }
        public List<string> Answers { get; set; }

        public Question(string content, List<string> answerList)
        {
            QuestionContent = content;
            Answers = answerList;
        }
    }

    List<Question> questionList = new List<Question>();
    Question currentQuestion = null;
    public Text QuestionText;
    public Text AnswerTextA;
    public Text AnswerTextB;
    public Text AnswerTextC;
    public Text AnswerTextD;
    public Text AnswerTextE;
    public Button AnswerButtonA;
    public Button AnswerButtonB;
    public Button AnswerButtonC;
    public Button AnswerButtonD;
    public Button AnswerButtonE;
    string answer;
    int correct = 0;
    int wrong = 0;
    ColorBlock green = new ColorBlock();
    ColorBlock red = new ColorBlock();
    ColorBlock normal = new ColorBlock();

    public Text FinalText;
    public GameObject MainSet;
    // Use this for initialization
    void Start()
    {
        questionList.Add(new Question("What type of phrase is in the sentence \"I need to sharpen the bread knife.\"?", new List<string> { "Indefinite noun phrase", "Definite noun phrase", "Adjective phrase", "Unmodified noun phrase", "Compound noun phrase" }));
        questionList.Add(new Question("Which of the following sentences does not contain a noun?", new List<string> { "Eat, drink, have fun, laugh, play!", "It is a place where the suns and days are different.", "The months look out from the window in fear at night.", "There are people lying in this place, an empty nothingness.", "There is a festival outside, a celebration." }));
        questionList.Add(new Question("Which of the following sentences contains a dropped noun phrase?", new List<string> { "His uncle will get very angry about this.", "Not everyone can own a place like this.", "The students' situation is heart-warming.", "I couldn't get used to the air in Çukurova.", "It is not advisable to go down a well with a rope that is his." }));
        questionList.Add(new Question("In which of the following lines has the modifier switched places with the modified?", new List<string> { "Absences, the most fearsome of all absenses.", "Fate made us in the phonographs of scorpions.", "You are as bright and beautiful as Turkey.", "What is this hole where my face should be?", "There are green curtains with vines in the room." }));
        questionList.Add(new Question("Which of the star nouns phrases is different from the others?", new List<string> { "These *footsteps* are always roaming outside.", "I'm imagining *underneath the clouds*.", "You are the *reflection of our horizon*, a flag of fire!", "Our family's problems have been resolved.", "The *child's arm* was sticking out of the blanket." }));
        questionList.Add(new Question("Which of the following sentences has the starred word taken a case suffix?", new List<string> { "They've beautifully painted *the house*.", "His mother never leaves his side.", "His anger is like a hay fire.", "His smile gives life to people.", "His face was changing colors." }));

        newQ();

        green = AnswerButtonA.colors;
        red = AnswerButtonA.colors;
        normal = AnswerButtonA.colors;

        green.normalColor = Color.green;
        green.pressedColor = Color.green;
        green.highlightedColor = Color.green;
        green.disabledColor = Color.green;

        red.normalColor = Color.red;
        red.pressedColor = Color.red;
        red.highlightedColor = Color.red;
        red.disabledColor = Color.red;
    }

    float timeToWait = 0;
    bool changeTime = false;
    bool returnTime = false;
    void Update()
    {
        timeToWait -= Time.deltaTime;

        if (timeToWait < 0 && changeTime)
        {
            AnswerButtonA.colors = normal;
            AnswerButtonB.colors = normal;
            AnswerButtonC.colors = normal;
            AnswerButtonD.colors = normal;
            AnswerButtonE.colors = normal;
            newQ();
            changeTime = false;
        }

        if (timeToWait < 0 && returnTime)
        {
            SceneManager.LoadScene("Main");
            returnTime = false;
        }
    }

    void newQ()
    {
        if (questionList.Count != 0)
        {
            currentQuestion = questionList[(new System.Random()).Next(questionList.Count)];
            answer = currentQuestion.Answers[0];
            QuestionText.text = currentQuestion.QuestionContent + " (C:" + correct + " W:" + wrong + ")";
            var availableAnswers = currentQuestion.Answers;

            List<int> notIncluded = new List<int>();
            var curr = Random.Range(0, 5);
            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 5);
            }
            notIncluded.Add(curr);
            AnswerTextA.text = "" + availableAnswers[curr];

            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 5);
            }
            notIncluded.Add(curr);
            AnswerTextB.text = "" + availableAnswers[curr];

            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 5);
            }
            notIncluded.Add(curr);
            AnswerTextC.text = "" + availableAnswers[curr];

            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 5);
            }
            notIncluded.Add(curr);
            AnswerTextD.text = "" + availableAnswers[curr];

            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 5);
            }
            notIncluded.Add(curr);
            AnswerTextE.text = "" + availableAnswers[curr];
        }
        else
        {
            MainSet.SetActive(false);
            FinalText.gameObject.SetActive(true);
            FinalText.text = string.Format("You got {0} correct and {1} wrong answers!", correct, wrong);
            changeTime = false;
            returnTime = true;
            timeToWait = 5;
        }
    }

    void commonStuff(bool isCorrect, Button currentButton)
    {
        questionList.Remove(currentQuestion);

        if (isCorrect) { correct++; currentButton.colors = green; } else
        {
            wrong++; currentButton.colors = red;
            if (AnswerTextA.text == answer)
            {
                AnswerButtonA.colors = green;
            }
            else if (AnswerTextB.text == answer)
            {
                AnswerButtonB.colors = green;
            }
            else if (AnswerTextC.text == answer)
            {
                AnswerButtonC.colors = green;
            }
            else if (AnswerTextD.text == answer)
            {
                AnswerButtonD.colors = green;
            }
            else if (AnswerTextE.text == answer)
            {
                AnswerButtonE.colors = green;
            }
        }
        timeToWait = 1.5f;
        changeTime = true;
    }

    public void AnswerA()
    {
        if (!changeTime)
        {
            commonStuff((AnswerTextA.text == answer), AnswerButtonA);
        }
    }

    public void AnswerB()
    {
        if (!changeTime)
        {
            commonStuff((AnswerTextB.text == answer), AnswerButtonB);
        }
    }

    public void AnswerC()
    {
        if (!changeTime)
        {
            commonStuff((AnswerTextC.text == answer), AnswerButtonC);
        }
    }

    public void AnswerD()
    {
        if (!changeTime)
        {
            commonStuff((AnswerTextD.text == answer), AnswerButtonD);
        }
    }

    public void AnswerE()
    {
        if (!changeTime)
        {
            commonStuff((AnswerTextE.text == answer), AnswerButtonE);
        }
    }
} where are the correct answers in this code?