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

public class StatsScript : MonoBehaviour {
    public GameObject statsNames;
    public GameObject statsValues;

    public GameObject statsLinePrefab;

    private List<StatsLine> statLines = new List<StatsLine>();

    private string roundText;
    private string questionsLostText;
    GameManagerScript gms;
    private string gameMode;

    private void Start() {
        gms = GameObject.Find("GameManager").GetComponent<GameManagerScript>();
        roundText = LocalizationManager.Instance.GetText("ROUND");
        questionsLostText = LocalizationManager.Instance.GetText("QUESTIONS_LOST");
        CreateStandardStats();
        AddPlayersToStats(Database.Instance.GetPlayersForGame(gms.GameId, GetGameMode()));
    }

    private string GetGameMode() {
        if (gameMode == null) {
            gameMode = PlayerPrefs.GetString("GameMode");
        }
        return gameMode;
    }

    private void CreateStandardStats() {
        StatsLine round = CreateStatLine();
        round.SetStatName(roundText);
        int roundValue = Database.Instance.GetRoundValue(gms.GameId, GetGameMode());

        round.SetLocalizationKey("ROUND");

        round.SetStatValue(roundValue);
        round.name = "roundStat";

        StatsLine lostQuestions = CreateStatLine();
        lostQuestions.SetStatName(questionsLostText);
        lostQuestions.SetStatValue(0);
        lostQuestions.name = "questionsLost";
        lostQuestions.SetLocalizationKey("QUESTIONS_LOST");

        StatsLine playerTitle = CreateStatLine();
        playerTitle.SetStatName(LocalizationManager.Instance.GetText("PLAYERS"));
        playerTitle.SetStatValue("");
        playerTitle.MakeBold();
        playerTitle.SetLocalizationKey("PLAYERS");

        statLines.Add(round);
        statLines.Add(lostQuestions);
        statLines.Add(playerTitle);
    }

    private StatsLine CreateStatLine() {
        GameObject slp = Instantiate(statsLinePrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
        StatsLine statLine = slp.GetComponent<StatsLine>();

        statLine.transform.SetParent(this.transform, false);

        return statLine;
    }

    public void AddPlayersToStats(List<KeyValuePair<string, int>> players) {

        foreach (KeyValuePair<string, int> player in players) {
            StatsLine p = CreateStatLine();
            p.RemoveLocalization();
            p.SetStatName(player.Key);
            p.SetStatValue(player.Value);

            statLines.Add(p);
        }
    }

    public void IncreaseRoundValue() {
        foreach (StatsLine sl in statLines) {
            if (sl.GetName().Equals(roundText)) {
                Int32.TryParse(sl.GetValue(), out int round);
                round++;
                sl.SetStatValue(round);
                Database.Instance.SetRoundValue(gms.GameId, round, GetGameMode());
                break;
            }
        }
    }

    public int GetRound() {
        return statLines.Find(s => s.GetName().Equals(roundText)).GetIntValue();
    }

    public void MakeBold(string playerName) {
        foreach (StatsLine sl in statLines) {
            if (sl.GetName().Equals(playerName)) {
                sl.MakeBold();
            } else {
                sl.UnBold();
            }
        }
    }

    public void SetQuestionsInAnswerLine(string playerName, int count) {
        foreach (StatsLine sl in statLines) {
            if (sl.GetName().Equals(playerName)) {
                sl.SetStatValue(count) ;
                break;
            } 
        }
    }

    public void SetQuestionsLost(int value) {
        StatsLine sl =  statLines.Find(s => s.GetName().Equals(questionsLostText));
        sl.SetStatValue(value);
    }

    public int GetQuestionsLost() {
        return statLines.Find(s => s.GetName().Equals(questionsLostText)).GetIntValue();
    }
}
