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

public class LocalGameScript : MonoBehaviour
{

    public Transform content;
    public GameObject playerInfoPrefab;

    public Button startButton;
    public Button deleteButton;

    public Text numberOfPlayersText;
    public Text neededToWinText;
    public Text roundText;
    /*public Text dateStartedText;
    public Text lastPlayedText;
    public Text finishedDateText;
    */

    private string startDate = "";
    private string lastPlayedDate = "";
    private string finishedDate = "";

    private int answerTimer;
    private string gameMode;
    private string currentPlayer;
    private string currentPlayerName;
    private int gameId;
    private List<KeyValuePair<string, int>> players;

    private LocalizationManager LANG = LocalizationManager.Instance;

    public string CurrentPlayerName { get => currentPlayerName; set => currentPlayerName = value; }
    public string CurrentPlayer { get => currentPlayer; set => currentPlayer = value; }
    public string NumberOfPlayers { get => numberOfPlayersText.text; set => numberOfPlayersText.text = value; }
    public int AnswerTimer { get => answerTimer; set => answerTimer = value; }
    public string QuestionsNeededToWin { get => neededToWinText.text; set => neededToWinText.text = value; }
    public string GameMode { get => gameMode; set => gameMode = value; }
    public int GameId { get => gameId; set => gameId = value; }
    public string Round { get => roundText.text; set => roundText.text = value; }
    public string StartDate { get => startDate; set => startDate = value; }
    public string LastPlayedDate { get => lastPlayedDate; set => lastPlayedDate = value; }
    public string FinishedDate { get => finishedDate; set => finishedDate = value; }
  
    public List<KeyValuePair<string, int>> Players { get => players; set => players = value; }

    private void Start() {
        startButton.onClick.AddListener(StartThisGame);
        deleteButton.onClick.AddListener(DeleteThisGame);
    }

    public void AddPlayer(string name, int points) {
        if (players == null) {
            players = new List<KeyValuePair<string, int>>();
        }
        KeyValuePair<string, int> p = new KeyValuePair<string, int>(name, points);
        Players.Add(p);

        GameObject go = Instantiate(playerInfoPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
        PlayerInfo playerInfo = go.GetComponent<PlayerInfo>();

        playerInfo.SetName(name);
        playerInfo.SetPoints(points);

        playerInfo.transform.SetParent(content);
        playerInfo.transform.SetSiblingIndex(0);
    }

    public void StartThisGame() {
        PlayerPrefs.SetInt("GameId", GameId);
        PlayerPrefs.SetString("GameMode", "Local");
        PlayerPrefs.SetInt("QuestionTimer", AnswerTimer);
        SceneManager.LoadSceneAsync("narKampen");
    }

    public void DeleteThisGame() {
        GenericDialog dialog = GenericDialog.Instance();
        dialog.SetTitle(LANG.GetText("REMOVE_GAME_TITLE_KEY"));
        dialog.SetMessage(LANG.GetText("REMOVE_GAME_MESSAGE_KEY"));
        dialog.SetOnAccept(LANG.GetText("YES"),() => {
            Database.Instance.RemoveGame(gameId);
            Destroy(this);
            Destroy(this.gameObject);
            dialog.Hide();
        });
        dialog.SetOnDecline(LANG.GetText("NO"), () => { dialog.Hide(); });

        dialog.Show();
    }

    public void DisableStartButton() {
        Destroy(startButton.gameObject);
    }

}
