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

public class RoundButtonsScript : MonoBehaviour
{
    public Button newQuestionButton;
    public Button nextPlayerButton;
    private ScrollViewScript svs;
    private TimerScript ts;

    // Start is called before the first frame update
    void Start()
    {
        newQuestionButton.onClick.AddListener(NewQuestion);
        nextPlayerButton.onClick.AddListener(NextPlayer);
        svs = GameObject.Find("Scroll View").GetComponent<ScrollViewScript>();
        ts = GameObject.Find("TimerCircle").GetComponent<TimerScript>();
    }


    void NewQuestion() {
        HidePanel();
        ts.ResetTimer();
        ts.StartTimer();
        svs.HideRoundButtons();
    }

    void NextPlayer() {
        svs.SetAllQuestionsLocked(true);
        svs.NextPlayer();
        svs.CheckActiveUserLoggedIn();
        HidePanel();
    }

    public void HidePanel() {
        CanvasGroup cg = this.GetComponent<CanvasGroup>();
        cg.alpha = 0F;
        cg.blocksRaycasts = false;
        cg.interactable = false;
    }

    public void ShowPanel() {

        if (svs == null) {
            svs = GameObject.Find("Scroll View").GetComponent<ScrollViewScript>();
        }
        NewQuestion q = Database.Instance.GetNewQuestion(svs.GetQuestionIdsInAnswerLine(), svs.currentPlayer, svs.GetGameMode());
        q.SetAnswerText("???? - ????");
        svs.SetNewQuestion(q);

        Text nqct = GameObject.Find("NextQuestionCategoryText").GetComponent<Text>();
        CategoryPanel cp = GameObject.Find("Categories").GetComponent<CategoryPanel>();
        CategoryPanel.Category cat = cp.GetCategoryById(q.GetCategoryId());
        nqct.text = cat.name;

        CanvasGroup cg = this.GetComponent<CanvasGroup>();
        cg.alpha = 1f;
        cg.interactable = true;
        cg.blocksRaycasts = true;
    }

    internal void DeactivateNextPlayer() {
        nextPlayerButton.interactable = false;
        newQuestionButton.GetComponentInChildren<Text>().text = LocalizationManager.Instance.GetText("START_ROUND_TEXT");
    }

    internal void ActivateNextPlayer() {
        nextPlayerButton.interactable = true;
        newQuestionButton.GetComponentInChildren<Text>().text = LocalizationManager.Instance.GetText("NEW_QUESTION");
    }
    
    public void SetGameOver() {
        nextPlayerButton.interactable = false;
        newQuestionButton.interactable = false;
    }

}
