using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerScore : MonoBehaviour {

    public int Score;
    public Text ScoreText;
    public Text GameOverScoreText;
    public float Timer;
    public Text TimerText;
    public Text HighscoreText;
    public float wave;
    public Text WaveText;
    public GameObject spawnManager;

    public int highScore = 0;
    string highScoreKey = "HighScore";
    // Use this for initialization
    void Start()
    {

        highScore = PlayerPrefs.GetInt(highScoreKey, 0);

        spawnManager = GameObject.FindGameObjectWithTag("Spawnmanager");
        wave = spawnManager.GetComponent<SpawnManager>().waveCount;

        GameOverScoreText.text = Score.ToString();
        Score = 0;
        UpdateScore();
        ScoreText.text = Score.ToString();
        TimerText.text = Timer.ToString("f0");

    }

    // Update is called once per frame
    void Update()
    {
       
        WaveText.text = spawnManager.GetComponent<SpawnManager>().waveCount.ToString();
        UpdateScore();
        TimeDiversifier();
        TimerText.text = Timer.ToString("f0");
    }

    void OnDisable()
    {

        //If our scoree is greter than highscore, set new higscore and save.
        if (Score > highScore)
        {
            PlayerPrefs.SetInt(highScoreKey, Score);
            PlayerPrefs.Save();
        }
    }

    public void AddScore(int scoreValue)
    {
        Score = Score += scoreValue;

    }

    void UpdateScore()
    {
        ScoreText.text = Score.ToString();
        GameOverScoreText.text = Score.ToString();

    }
    void TimeDiversifier()
    {
        Timer += Time.deltaTime;
    }

    
}
