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 Button AnswerButtonA;
    public Button AnswerButtonB;
    public Button AnswerButtonC;
    public Button AnswerButtonD;
    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("Кто главный герой мультсериала \"Губка Боб Квадратные штаны\"?", new List<string> { "Губка Боб", "Сквидвард", "Патрик", "Планктон"}));
        questionList.Add(new Question("Как зовут собаку Микки Мауса в мультфильме \"Клуб Микки Мауса\"?", new List<string> { "Плуто", "Гуфи", "Минни Маус", "Дональд Дак"}));
        questionList.Add(new Question("Какой мультяшный супергерой носит красную накидку и умеет летать?", new List<string> { "Супермен", "Бэтмен", "Человек-паук", "Железный человек"}));
        questionList.Add(new Question("В мультсериале \"Том и Джерри\" кто постоянно пытается поймать мышонка Джерри?", new List<string> { "Кот Том", "Кузен Джерри", "Кот Бутч", "Бульдог Спайк"}));

        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;
            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, 4);
            while (notIncluded.Contains(curr))
            {
                curr = Random.Range(0, 4);
            }
            notIncluded.Add(curr);
            AnswerTextA.text = "" + availableAnswers[curr];

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

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

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

        }
        else
        {
            MainSet.SetActive(false);
            FinalText.gameObject.SetActive(true);
            FinalText.text = string.Format("Ты правильно ответил(а) {0} и ошибся(лась) {1} раз!", 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;
            }
           
        }
        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);
        }
    }

}