﻿using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// Score processing
/// </summary>
public class Score : MonoBehaviour
{
	/// <summary>
	/// UIText to display the score
	/// </summary>
	public Text scoreText = null;

	/// <summary>
	/// UIText displaying a high score
	/// </summary>
	public Text highScoreText = null;

	/// <summary>
	/// Current score
	/// </summary>
	private int score = 0;

	/// <summary>
	/// High score
	/// </summary>
	private int highScore = 0;

	// High score save key
	private string highScoreKey = "highScore";

	void Start()
	{
		// Initialize at start.
		Initialize();
	}

	void Update()
	{
		// High score updates
		if (highScore < score)
		{
			highScore = score;
		}

		// Show score/high score
		scoreText.text = score.ToString();
		highScoreText.text = "HighScore : " + highScore.ToString();
	}

	/// <summary>
	/// Return to the state before the game started
	/// </summary>
	private void Initialize()
	{
		// Return score to 0
		score = 0;

		// Get a high score. If not saved, 0 is acquired.
		highScore = PlayerPrefs.GetInt(highScoreKey, 0);
	}

	/// <summary>
	/// Add points
	/// </summary>
	/// <param name="point">Point</param>
	public void AddPoint(int point)
	{
		score += point;
	}

	/// <summary>
	/// Save high score
	/// </summary>
	public void Save()
	{
		// Save high score
		PlayerPrefs.SetInt(highScoreKey, highScore);
		PlayerPrefs.Save();

		// Return to the state before the game started
		Initialize();
	}
}