using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreManager : MonoBehaviour {
	
	int currentCombo;

	int maxCombo;

	[SerializeField]
	Text currentScoreText;

	[SerializeField]
	Text highScoreText;

	[SerializeField]
	string keyToSave;

	[SerializeField]
	bool forMultiplayer;

    public static ScoreManager Instance;

	void Awake()
	{
		bool isInMultiplayer = false;

		if((Network.isServer || Network.isClient) && Network.connections.Length > 0)
		{
			isInMultiplayer = true;
		}

		gameObject.SetActive(isInMultiplayer == forMultiplayer);
	}

	public void Start()
	{
		Instance = this;
		BallManager.onEndOfGame += Reset;

		if(PlayerPrefs.HasKey(keyToSave))
		{
			maxCombo = PlayerPrefs.GetInt(keyToSave, maxCombo);
			highScoreText.text = maxCombo.ToString();
		}
	}

	void OnDestroy()
	{
		BallManager.onEndOfGame -= Reset;
	}

	public void Reset()
	{
		currentCombo = 0;
	}

	public void OnBallHit()
	{
		currentCombo++;
		currentScoreText.text = currentCombo.ToString();

		if(currentCombo > maxCombo)
		{
			maxCombo = currentCombo;
			highScoreText.text = maxCombo.ToString();
			PlayerPrefs.SetInt(keyToSave, maxCombo);

			if(!forMultiplayer)
			{
				PlayerPrefs.Save();
				Debug.Log(PlayerPrefs.GetInt(keyToSave, maxCombo));
			}

		}
	}

}
