using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class LoseScreen : MonoBehaviour
{

    public TextMeshProUGUI ScoreLabel = null;

    public TextMeshProUGUI textTopNames = null;
    public TextMeshProUGUI textTopScores = null;
    public TextMeshProUGUI textLocalNames = null;
    public TextMeshProUGUI textLocalScores = null;

    // Start is called before the first frame update
    void Start()
    {
        ScoreLabel.text = PlayerStats.Score.ToString();

        GetComponent<WebServerHighScores>().HighScoresUpdated += HighScoreScreen_HighScoresUpdated;
        GetComponent<WebServerHighScores>().HighScoreSubmitted += HighScoreScreen_HighScoreSubmitted;

        GetComponent<WebServerHighScores>().SubmitHighScore(PlayerStats.Username, PlayerStats.Score);

    }

    private void HighScoreScreen_HighScoresUpdated(WebServerHighScores sender)
    {
        ShowAllScores();
    }

    private void HighScoreScreen_HighScoreSubmitted(WebServerHighScores sender)
    {
        GetComponent<WebServerHighScores>().RefreshTopScores();
    }


    public void ShowAllScores()
    {
        WebServerHighScores wshs = GetComponent<WebServerHighScores>();

        if (wshs.ErrorMessageCount > 0)
        {
            textTopNames.text = wshs.ErrorMessages;
            textTopScores.text = "If you are getting an error, please make sure your url begins with \"http://www.wyattcheng.com\" and not simply \"http://wyattcheng.com\"";
            wshs.ClearErrorMessages();
        }
        HighScoreUtility.Dump20ScoresToTextControls(wshs, textTopNames, textTopScores, PlayerStats.Username, 0);
        HighScoreUtility.Dump20ScoresToTextControls(wshs, textLocalNames, textLocalScores, PlayerStats.Username, HighScoreUtility.RecommendedStartIndexForMoreScores(wshs, PlayerStats.Username));
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnConfirmClicked()
    {
        PlayerStats.Reset();
        UnityEngine.SceneManagement.SceneManager.LoadScene("CombatScene");
    }
}
