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

public class OnlineGameScript : MonoBehaviour {

    // Status color Pending, not your turn, your turn, declined
    List<Color32> statusColors = new List<Color32>() { new Color32(234, 164, 17, 100), new Color32(255, 255, 24, 175), new Color32(0, 255, 0, 100), new Color32(219, 12, 65, 100), new Color32(12,12,219,150) };
    public Text gameStatusText;
    public Text gameTitleText;

    private int id;
    private int winNumber;
    private int answerTimer;
    private int roundTimeLimit;
    private string currentPlayer;
    private int round;
    private string startDate;
    private string LastPlayedDate;
    private string finishedDate;

    private List<KeyValuePair<string, string>> playerInfos;

    public int GetId() {
        return id;
    }

    public void SetId(int value) {
        id = value;
    }

    public int WinNumber { get => winNumber; set => winNumber = value; }
    public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
    public int RoundTimeLimit { get => roundTimeLimit; set => roundTimeLimit = value; }
    public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
    public int Round { get => round; set => round = value; }
    public string StartDate { get => startDate; set => startDate = value; }
    public string LastPlayedDate1 { get => LastPlayedDate; set => LastPlayedDate = value; }
    public string FinishedDate { get => finishedDate; set => finishedDate = value; }
    public string GameStatus {
        get { return gameStatus; }
        set {
            gameStatus = value;
            SetColor(gameStatus);

            //SetGameStatusText();
        }
    }

    public List<KeyValuePair<string, string>> PlayerInfos { get => playerInfos; set => playerInfos = value; }

    private string PENDING_TITLE = "ONLINE_GAME_STATUS_TITLE_VALUE_PENDING";
    private string ACTIVE_TITLE = "ONLINE_GAME_STATUS_TITLE_ACTIVE";

    private string PENDING = "ONLINE_GAME_STATUS_PENDING";
    private string DECLINED = "ONLINE_GAME_STATUS_DECLINED";
    private string ACTIVE = "ONLINE_GAME_STATUS_ACTIVE";
    private string INVITED = "ONLINE_GAME_STATUS_INVITED";

    private string OTHERS_TURN = "ONLINE_GAME_STATUS_OTHERS_TURN";
    private string YOUR_TURN = "ONLINE_GAME_STATUS_YOUR_TURN";

    // behövs nog en partialy declined status också, om en av fyra tackar nej kanske man viss spela ändå.
    // behövs också en med dina inbjudningar.

    private Button gameInfoButton;

    private void Start() {
        gameInfoButton = this.GetComponent<Button>();
        gameInfoButton.onClick.AddListener(ShowInfoPanel);
    }

    private void ShowInfoPanel() {
        GameObject gameInfoPanel = GameObject.Find("GameInfoPanel");
        GameInfoScript gameInfoScript = gameInfoPanel.GetComponent<GameInfoScript>();
        gameInfoScript.SetOnlineData(this);
        gameInfoScript.Show();
    }

    public string gameStatus;

    private void SetColor(string status) {
        Image image = this.GetComponent<Image>();
        if ("PENDING".Equals(status)) {
            image.color = statusColors[0];
        } else if ("YOUR_TURN".Equals(status)) {
            image.color = statusColors[1];
        } else if ("OTHERS_TURN".Equals(status)) {
            image.color = statusColors[2];
        } else if ("DECLINED".Equals(status)) {
            image.color = statusColors[3];
        } else if ("INVITED".Equals(status)) {
            image.color = statusColors[4];
        } else if ("ACTIVE".Equals(status)) {
            if (CurrentPlayer.Equals(Database.Instance.GetSignedInUser().Value, StringComparison.InvariantCultureIgnoreCase)) {
                image.color = statusColors[2];
            } else {
                image.color = statusColors[1];
            }
        } else {
            image.color = new Color32(100, 100, 100, 100);
        }
    }

    public void SetTitleText(string text) {
        gameTitleText.text = text;
    }

    public void SetGameStatusText() {
        SetGameStatusText("");
    }

    public void SetGameStatusText(string extraInfo) {
        if ("PENDING".Equals(GameStatus)) {
            gameStatusText.gameObject.GetComponent<TextLocalization>().key = PENDING;
            gameStatusText.text = LocalizationManager.Instance.GetText(PENDING);
            SetTitleText(LocalizationManager.Instance.GetText(PENDING_TITLE));
        } else if ("YOUR_TURN".Equals(GameStatus)) {
            gameStatusText.gameObject.GetComponent<TextLocalization>().key = YOUR_TURN;
            gameStatusText.text = LocalizationManager.Instance.GetText(YOUR_TURN);
        } else if ("OTHERS_TURN".Equals(GameStatus)) {
            gameStatusText.gameObject.GetComponent<TextLocalization>().key = OTHERS_TURN;
            string message = LocalizationManager.Instance.GetText(OTHERS_TURN);
            gameStatusText.text = String.Format(message, extraInfo);
        } else if ("DECLINED".Equals(GameStatus)) {
            gameStatusText.gameObject.GetComponent<TextLocalization>().key = DECLINED;
            gameStatusText.text = LocalizationManager.Instance.GetText(DECLINED);
        } else if ("INVITED".Equals(GameStatus)) {
            gameStatusText.gameObject.GetComponent<TextLocalization>().key = INVITED;
            gameStatusText.text = LocalizationManager.Instance.GetText(INVITED);
        } else if ("ACTIVE".Equals(GameStatus)) {
            TextLocalization textLocalization = gameStatusText.gameObject.GetComponent<TextLocalization>();
            textLocalization.key = ACTIVE;
            textLocalization.SetReplaceText(extraInfo);
            gameTitleText.gameObject.GetComponent<TextLocalization>().key = ACTIVE_TITLE;
            gameTitleText.text = LocalizationManager.Instance.GetText(ACTIVE_TITLE);
        } else { 
            gameStatusText.text = "SOMETHING WRONG WITH STATUS TITLE FROM TEXT " + GameStatus;
        }
    }

    public void SetId(string id) {
        Int32.TryParse(id, out int intId);
        SetId(intId);
    }

    public void SetWinNumber(string winNumber) {
        Int32.TryParse(winNumber, out int intWinNumber);
        WinNumber = intWinNumber;
    }

    public void SetAnswerTimer(string answerTimer) {
        Int32.TryParse(answerTimer, out int intAnswerTimer);
        AnswerTimer = intAnswerTimer;
    }

    public void SetRoundTimeLimit(string roundTimeLimit) {
        Int32.TryParse(roundTimeLimit, out int intRoundTimelimit);
        RoundTimeLimit = intRoundTimelimit;
    }

    public void SetRound(string round) {
        Int32.TryParse(round, out int intRound);
        Round = intRound;
    }

    public void SetGameStatus(string status) {
        GameStatus = status;
    }
}
