using System.Collections.Generic;
using UnityEngine;

public class ScoreCard : MonoBehaviour
{
	public List<Transform> DigitPrefabs;
	public float DigitSpacing = 0.375f;

	private int shownScore;

	void Start()
    {
		shownScore = -999;

	}

	void Update()
    {
		if(Score.Total != shownScore)
		{
			ClearScore();
			ShowScore();
		}
    }

	private void ClearScore()
	{
		foreach (Transform child in transform)
		{
			Destroy(child.gameObject);
		}
	}


	private void ShowScore()
	{
		int score = Score.Total;
		string s = score.ToString();

		float spacing = DigitSpacing;
		float leftEdge = -0.5f * (spacing * (s.Length - 1));

		for (int i = 0; i < s.Length; i++)
		{
			Transform digit = Instantiate(DigitPrefabs[int.Parse(s.Substring(i, 1))]);
			float xpos = leftEdge + i * spacing;

			digit.SetParent(transform);
			digit.localScale = new Vector3(0.5f, 0.5f, 0.5f);
			digit.localPosition = new Vector3(xpos, 0f, 0f);
		}

		shownScore = score;
	}
}
