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

public class GameManagerScript : MonoBehaviour {

    private int playerCount;
    private int questionTimer;
    private string gameMode;

    public Database db;
    public OnlineDatabase odb;
    StatsScript statsScript;
     
    public string GameMode { get => gameMode; set => gameMode = value; }
    public int GameId { get; internal set; }

    public int QuestionTimer { get => questionTimer; set => questionTimer = value; }

    List<KeyValuePair<string, int>> players;
    // Start is called before the first frame update
    void Start() {
        GameId = PlayerPrefs.GetInt("GameId");
        GameMode = PlayerPrefs.GetString("GameMode");
        QuestionTimer = PlayerPrefs.GetInt("QuestionTimer");

        if (GameMode.Equals("Local")) {
            db = Database.Instance;
            db.SetLocalOrOnline(GameMode);
            db.SetLastPlayedDate(GameId);
        } else if (GameMode.Equals("Online")) {
            odb = OnlineDatabase.Instance;
            odb.SetLastPlayedDate(GameId);
        }
        statsScript = GameObject.Find("StatsPanel").GetComponent<StatsScript>();

    }

    private List<KeyValuePair<string,int>> GetPlayersForGame() {
        players = db.GetPlayersForGame(GameId, GameMode);
        return players;
    }

    public void UpdateQuestiosLost(int questionsLost, string playerName) {
        db.SetQuestionsLost(GameId, playerName, questionsLost);
    }

    public void UpdateQuestionsInAnswerLine(string playerName, int count) {
        
    }

    public List<KeyValuePair<string, int>> GetPlayers() {
        if (players == null) {
            players = GetPlayersForGame();
        }
        return players;
    }

}
