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

public class UI_EnterName : TM_UI_Base
{
    private GameManager theGame;
    private string strPlayerName;

    public TextMeshProUGUI txtNewName;

	// Use this for initialization
	void Start () 
    {
		theGame = GameManager.GetGame();
	}

    public override void TurnOn()
    {
        base.TurnOn();
        strPlayerName = theGame.GetHighScoreName();
        txtNewName.text = strPlayerName;
    }

    public override void TurnOff()
    {
        base.TurnOff();
        if (theGame != null && !string.IsNullOrEmpty(strPlayerName))
        {
            theGame.SetHighScoreName(strPlayerName);            
        }
    }

	void Update ()
    {
        if (bFirstUpdate)
        {
            bFirstUpdate = false;
            TurnOff();
        }
        
	}

    void UpdateName(string s)
    {
        if (!string.IsNullOrEmpty(s))
        {
            s = s.Replace("*", "");
            strPlayerName = s;
            txtNewName.text = strPlayerName;
            theGame.SetHighScoreName(strPlayerName);     
        }
    }

    public void OnChanged(string s)
    {
        Debug.Log("OnChanged: " + s);
        
    }
    public void OnEndEdit(string s)
    {
        UpdateName(s);
    }

    public void OnDeselect(string s)
    {
        UpdateName(s);
    }

    public void OnClick_Submit()
    {
        theGame.SetHighScoreName(strPlayerName);
        TurnOff();
        theGame.uiLeaderboards.TurnOn();
        theGame.uiLeaderboards.SubmitHighScore(strPlayerName, theGame.GetScore());
    }
}
