using UnityEngine;
using System.Collections;
using TMPro;

public class UI_GameOver : TM_UI_Base
{
    public GameManager theGame;

    public TextMeshProUGUI textPlayAgain;
    public TextMeshProUGUI textScore;

    public float fDelayBeforePushButtonToPlayAgain;

    void Awake()
    {
        theGame = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

    public override void TurnOn()
    {
        base.TurnOn();
        fDelayBeforePushButtonToPlayAgain = 1.0f;
        //textPlayAgain.enabled = false;
    }

    // Use this for initialization
    public override void Start()
    {

    }

    // Update is called once per frame
    public override void Update()
    {
        if (bFirstUpdate)
        {
            bFirstUpdate = false;
            TurnOff();
            return;
        }

        if (fDelayBeforePushButtonToPlayAgain >= 0.0f)
        {
            fDelayBeforePushButtonToPlayAgain -= Time.deltaTime;
        }

        textScore.text = theGame.Score().ToString();
        textScore.color = Random.ColorHSV();
    }

    public void OnEndEdit_EnterName(string strEditValue)
    {
        if (fDelayBeforePushButtonToPlayAgain <= 0.0f && 
            (Input.GetKey(KeyCode.Return) || Input.GetKey(KeyCode.KeypadEnter)) )
        {
            TM_LeaderboardManager.SendUpLeaderboardData(strEditValue, theGame.Score());

            //blow up all projectiles
            TM_Projectile[] slowAssFindCall = FindObjectsOfType<TM_Projectile>();
            for (int t = 0; t < slowAssFindCall.Length; t++)
            {
                TM_Projectile p = slowAssFindCall[t];
                Destroy(p.gameObject);
            }
            theGame.strPlayerHighScoreName = strEditValue;
            theGame.ChangeGameState(EGameState.leaderboards);
        } 
        
        

    }
}