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


public class NewQuestionsPanel : MonoBehaviour
{
    public GameObject QuestionAnswerLine;

    public Text AnswerText;
    public Text QuestionText;
    public Text backCategoryText;
    public GameObject QuestioncardBackside;

    [SerializeField] Image FrontQuestionCategoryImage;
    [SerializeField] Text FrontQuestionCategoryText;

    private NewQuestionData questionData;
    private GameManagerScript gameManagerScript;
    public NewQuestionData QuestionData { get => questionData; set => questionData = value; }

    [SerializeField] GameObject gameManager;
    // Start is called before the first frame update
    void Start()
    {
        gameManagerScript = gameManager.GetComponent<GameManagerScript>();
        int gameId = gameManagerScript.GameId;
        string gameMode = gameManagerScript.GameMode;
        //string gameMode = Database.Instance.GetGameMode(gameId);
        string currentPlayer = Database.Instance.GetCurrentPlayer(gameId, gameMode);

        if (gameMode.Equals(Constants.ONLINE) && 
                currentPlayer.Equals(Database.Instance.GetSignedInUser().Value, StringComparison.InvariantCultureIgnoreCase ) && 
                Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode) <= 1 ||
            (gameMode.Equals(Constants.LOCAL) && 
                Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode) <= 1)) {
            List<NewQuestionData> usersQuestions = Database.Instance.GetPlayerQuestions(gameId, currentPlayer, gameManagerScript.GameMode);
            NewQuestionData qd = usersQuestions[0];
            AnswerText.text = qd.Answer;
            QuestionText.text = qd.Question;
            backCategoryText.text = qd.CategoryName;
            FrontQuestionCategoryText.text = qd.CategoryName;
            FrontQuestionCategoryImage.color = qd.CategoryColor;
            QuestioncardBackside.GetComponent<Image>().color = qd.CategoryColor;

            QuestionData = qd;
        } else if (gameMode.Equals(Constants.ONLINE) && 
                !currentPlayer.Equals(Database.Instance.GetSignedInUser().Value, StringComparison.InvariantCultureIgnoreCase)) {
            SetNotYourTurn();
        }
    }

    private void Awake() {
        if (gameManagerScript == null) {
            gameManagerScript = gameManager.GetComponent<GameManagerScript>();
        }
        if (Database.Instance.GetRoundValue(gameManagerScript.GameId, gameManagerScript.GameMode) > 1) {
            generateNewQuestion(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (gameManagerScript.GameMode.Equals(Constants.ONLINE) && !GameManagerScript.GetCurrentPlayer().Equals(Database.Instance.GetSignedInUser().Value, StringComparison.InvariantCultureIgnoreCase)) {
            SetNotYourTurn();
        }
    }

    public void generateNewQuestion(bool setQuestionActive) {
        if (setQuestionActive) {
            AnswerLineQuestionCard[] answerLineQuestions = QuestionAnswerLine.GetComponentsInChildren<AnswerLineQuestionCard>();
            List<int> answerIds = new List<int>();
            foreach (var aq in answerLineQuestions) {
                answerIds.Add(aq.QuestionId);
            }

            KeyValuePair<int, String> user = Database.Instance.GetSignedInUser();
            QuestionData = Database.Instance.GetNewQuestion(answerIds, user, gameManager.GetComponent<GameManagerScript>().GameMode);

            AnswerText.text = "???? - ????";
            QuestionText.text = QuestionData.Question;
            backCategoryText.text = QuestionData.CategoryName;
            QuestioncardBackside.GetComponent<Image>().color = QuestionData.CategoryColor;
            FrontQuestionCategoryImage.color = QuestionData.CategoryColor;
            FrontQuestionCategoryText.text = QuestionData.CategoryName;
            GetComponentInChildren<Button>().interactable = true;
        } else {
            SetNotYourTurn();
        }
    }

    private void SetNotYourTurn()
    {
        backCategoryText.text = LocalizationManager.Instance.GetText("NOT_YOUR_TURN");
        QuestioncardBackside.GetComponent<Image>().color = new Color(130f, 130f, 130f, 255f);
        GetComponentInChildren<Button>().interactable = false;
        GetComponentInChildren<NewQuestionCardController>().BackClickable = false;
    }
}
