using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScoreUI : MonoBehaviour
{
    public class ScoreData
    {
        public string Text;
        public Vector2 Position;
        public float Time;
        public Color CharacterColour;
    }

    public Font ScoreFont;
    public float MaxTime = 1;
    public Vector2 MoveSpeed = new Vector2(0, 0.1f);
    public float WidthScaledFontSize = 14;
    public Color ScoreShadowColour = Color.black;
    public Vector2 ShadowOffset = new Vector2(0.01f, 0.01f);
    private static List<ScoreData> Scores = new List<ScoreData>();

    public static void AddScore(Vector3 Position, float Score, Color CharacterColour)
    {
        Scores.Add(new ScoreData()
        {
            Text = "$" + Score.ToString("F0"),
            Position = Camera.main.WorldToScreenPoint(Position),
            Time = 0,
            CharacterColour = CharacterColour
        });
    }

    public void Update()
    {
        for(int i = 0; i < Scores.Count; i++)
        {
            Scores[i].Time += Time.deltaTime;
            Scores[i].Position += new Vector2(Screen.width, Screen.height) * MoveSpeed * Time.deltaTime;

            if(Scores[i].Time > MaxTime)
            {
                Scores.RemoveAt(i);
                i--;
            }
        }
    }

    public void OnGUI()
    {
        if (MasterUI.IsBusy)
            return;

        GUIStyle ScoreStyle = new GUIStyle(GUI.skin.label);
        ScoreStyle.alignment = TextAnchor.MiddleCenter;
        ScoreStyle.font = ScoreFont;
        ScoreStyle.fontSize = (int)(((float)WidthScaledFontSize / 1080.0f) * (float)Screen.width);
        Rect R = new Rect(Vector2.zero, new Vector2(Screen.width, Screen.height));

        foreach (ScoreData Score in Scores)
        {
            R.center = new Vector2(Score.Position.x, Screen.height - Score.Position.y);
            GUI.color = ScoreShadowColour;
            GUI.Box(R, Score.Text, ScoreStyle);

            R.center += new Vector2(Screen.width, Screen.height) * ShadowOffset;
            GUI.color = Score.CharacterColour;
            GUI.Box(R, Score.Text, ScoreStyle);
        }
    }
}
