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

public class GameInfoScript : MonoBehaviour {

    public Button cancelButton;
    public Button acceptButton;

    public GameObject playerInfoPrefab;

    public Transform playersPanel;
    public Transform gameSettingsPanel;

    private int gameId;
    private int questionTimer;
    private int winNumber;
    [SerializeField] Text answerTimeValueText;
    [SerializeField] Text questionsToWinValueText;
    [SerializeField] Text DaysToStartRoundValueText;

    [SerializeField] Text GameIdText;
    // Start is called before the first frame update
    void Start() {
        cancelButton.onClick.AddListener(Hide);
        acceptButton.onClick.AddListener(OpenGame);
    }


    // Update is called once per frame
    void Update() {

    }

    internal void SetOnlineData(OnlineGameScript onlineGameScript) {
        String currentPlayer = Database.Instance.GetSignedInUser().Value;
        gameId = onlineGameScript.GetId();
        questionTimer = onlineGameScript.AnswerTimer;
        winNumber = onlineGameScript.WinNumber;

        answerTimeValueText.text = onlineGameScript.AnswerTimer.ToString();
        questionsToWinValueText.text = onlineGameScript.WinNumber.ToString();
        DaysToStartRoundValueText.text = onlineGameScript.RoundTimeLimit.ToString();

        foreach (KeyValuePair<string, string> player in onlineGameScript.PlayerInfos) {
            GameObject slp = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
            OnlinePlayerInfoScript opi = slp.GetComponent<OnlinePlayerInfoScript>();
            opi.GameId = gameId;
            opi.SetPlayerName(player.Key);
            if (currentPlayer.Equals(player.Key, StringComparison.InvariantCultureIgnoreCase) && player.Value.Equals("WAITING")) {
                opi.ShowDecitionButtons();
            } else {
                opi.SetPlayerStatus(player.Value);
            }
            opi.transform.SetParent(playersPanel, false);
        }

        GameIdText.text = onlineGameScript.GetId().ToString();

        acceptButton.enabled = false;
        acceptButton.interactable = false;

        if (onlineGameScript.GameStatus.Equals("ACTIVE") || onlineGameScript.GameStatus.Equals(OnlineGameScript.GAME_STATUS_YOUR_TURN) || onlineGameScript.GameStatus.Equals(OnlineGameScript.GAME_STATUS_OTHERS_TURN)) {
            acceptButton.enabled = true;
            acceptButton.interactable = true;
        }
    }

    private void OpenGame() {
        PlayerPrefs.SetString(Constants.GAME_MODE, Constants.ONLINE);
        PlayerPrefs.SetInt(Constants.GAME_ID, gameId);
        PlayerPrefs.SetInt(Constants.GAME_QUESTION_TIMER, questionTimer);
        PlayerPrefs.SetInt(Constants.GAME_TO_WIN, winNumber);
        SceneManager.LoadSceneAsync(Constants.SCENE_NAR_KAMPEN);
    }

    public void Show() {
        CanvasGroup cg = this.GetComponent<CanvasGroup>();
        cg.alpha = 1;
        cg.interactable = true;
        cg.blocksRaycasts = true;
    }

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

        foreach (OnlinePlayerInfoScript opis in playersPanel.GetComponentsInChildren<OnlinePlayerInfoScript>()) {
            Destroy(opis);
            Destroy(opis.gameObject);
        }

        GameObject.Find("Main Panel").GetComponent<MainMenu>().UpdateOnlineGamesLists();
    }
}
