
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UI;

public class QuizController : MonoBehaviour {

    public GameObject[] answers;

    public Text counterText;

    public Text scoreText;

    public Text questionText;

    public Button btnNextQuestion;

    private float counterValue = 90;

    private const int scoreIncreaseValue = 10;

    private const int scoreDecreaseValue = 15;

    private int scoreValue = 0;

    private int questionIdx = 0;

    private List<int> selectedAnswers;

    List<Question> questions = new List<Question>() {
            new Question("Q1", new int[] {1, 3}), 
            new Question("Q2", new int[] {2}),
    };

    // Use this for initialization
    void Start () {
        selectedAnswers = new List<int>();
        UpdateScene();
        UpdateCounter();
        UpdateScore();
        btnNextQuestion.GetComponent<Button>().onClick.AddListener(LoadNextQuestion);
    }

    // Update is called once per frame
    void Update()
    {
        if (counterValue > 0)
        {
            counterValue -= Time.deltaTime;
            UpdateCounter();
        }

        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 100))
            {
                Debug.Log(hit.transform.gameObject.name);
                int answerIdx = GetAnswerIdx(hit.transform.gameObject.name) - 4 * questionIdx;
                if (!selectedAnswers.Contains(answerIdx))
                    selectedAnswers.Add(answerIdx);
            }
        }
    }

    public void IncreaseScore()
    {
        Debug.Log("Clicked on cube!");
        scoreValue += scoreIncreaseValue;
        UpdateScore();
    }

    void LoadNextQuestion()
    {
        if (IsAnswerCorrect())
            IncreaseScore();
        else
            DecreaseScore();
        questionIdx = (questionIdx + 1) % 2;
        selectedAnswers.Clear();
        Debug.Log("" + questionIdx);
        UpdateScene();
    }

    public void DecreaseScore()
    {
        Debug.Log("Clicked on sphere!");
        scoreValue -= scoreDecreaseValue;
        UpdateScore();
    }

    private void UpdateScore()
    {
        scoreText.text = "Score: " + scoreValue.ToString();
    }

    private void UpdateCounter()
    {
        counterText.text = "Counter: " + counterValue.ToString("f0");
    }

    private void UpdateScene()
    {
        // Set new question
        questionText.text = questions[questionIdx].getQuestionText();
        // Set right answers
        for(int i = 0; i < answers.Length; i++)
        {
            if (i >= 4 * questionIdx && i < 4 * (questionIdx + 1))
            {
                Debug.Log("Set active: " + i);
                answers[i].SetActive(true);
            }
            else
                answers[i].SetActive(false);
        }
    }

    private int GetAnswerIdx(string gameObjName)
    {
        string resultString = Regex.Match(gameObjName, @"\d+").Value;
        return System.Int32.Parse(resultString);
    }

    private bool IsAnswerCorrect() {
        return selectedAnswers.SequenceEqual(new List<int>(questions[questionIdx].getCorrectAnswers()));
    }
}
