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;
    private string gameMode;
    private string currenPlayer;
    [SerializeField] GameObject gameManager;
    [SerializeField] GameObject answerLine;
    [SerializeField] GameObject answerLineInfo;
    GameManagerScript gameManagerScript;
    AnswerLineInfoScript answerLineInfoScript;
    ScrollViewScript svc;

    private void Start() {
        gameManagerScript = gameManager.GetComponent<GameManagerScript>();
        answerLineInfoScript = answerLineInfo.GetComponent<AnswerLineInfoScript>();
        svc = answerLine.transform.parent.parent.GetComponent<ScrollViewScript>();
        roundText = LocalizationManager.Instance.GetText("ROUND");
        questionsLostText = LocalizationManager.Instance.GetText("QUESTIONS_LOST");
        currenPlayer = GameManagerScript.GetCurrentPlayer();
        CreateStandardStats();
        AddPlayersToStats(Database.Instance.GetPlayersForGame(gameManagerScript.GameId, gameManagerScript.GameMode));
    }

    void Update()
    {
        UpdatePlayerAnswer();
    }

    private void UpdatePlayerAnswer() {
        String currentPlayerShown = answerLineInfoScript.getCurrentPlayerShown();
        int answerCount = svc.GetQuestionIdsInAnswerLine().Count;
        SetQuestionsInAnswerLine(currentPlayerShown, answerCount);
    }

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

        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.GetComponent<RectTransform>().sizeDelta = new Vector2(playerTitle.GetComponent<RectTransform>().rect.width,50f);
        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(gameManagerScript.GameId, round, gameManagerScript.GameMode);
                break;
            }
        }
    }

    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(sl.GetIntValue() + value);
    }
}
