using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Assets.Scripts;
using Assets.Scripts.Utils;
using UnityEngine.UI;

public class UIController : MonoBehaviour
{
    public Text Distance;
    public Text Score;
    public Text HighScore;
    public Text NbVials;
    public Image Battery;
    public Image Progression;
    public Sprite[] ProgressionSprites;
    public List<Sprite> BatteryImages;

    private Player _player;
    private int _oldHs;

	void Start ()
	{
		GameController.Speed = GameController.InitialSpeed;
	    _player = GameObject.FindWithTag("Player").GetComponent<Player>();
	    HighScore.text = " "; // Keep position of elements, even when not displaying this one
	    _oldHs = PlayerPrefs.GetInt("highscore");
	}
	
	void Update ()
	{
	    Distance.text = (int.Parse(Distance.text) + 1).ToString();

		_player.Score += Mathf.RoundToInt(
			1f * (GameController.Speed / 4f));
	    Score.text = _player.Score.ToString();
	    NbVials.text = _player.NbVials.ToString();

	    DisplayBattery();
	    HandlePowerUps();
	    HandleScore();
		BackButtonHelper.menuOnBack();
	}

    private void HandleScore()
    {
        if (_oldHs < _player.Score)
			HighScore.text = "New high score!";
    }

    private void HandlePowerUps()
    {
    }

    private void DisplayBattery()
    {
        Battery.sprite = BatteryImages[_player.Battery/8];
    }
}
