using UnityEngine;
using UnityEngine.UI;

using System;
using System.Collections;

public class HUD : ObjectPoolRoot 
{
    #region 变量

    private float StayTimer;
    private Text Text;

    [NonSerialized] public float Speed;
    [NonSerialized] public float _StayTime;
    [NonSerialized] public bool Permanent;

    #endregion

    private void Awake()
    {
        Text = GetComponent<Text>();
        ObjectPoolType = ObjectPoolType.HUD;
    }

    private void OnEnable()
    {
        Text.color = new Color(Text.color.r, Text.color.g, Text.color.b, 1);
        StayTimer = _StayTime;
    }

    private void FixedUpdate()
    {
        if (Permanent)
        {
            return;
        }

        StayTimer -= Time.fixedDeltaTime;

        if (StayTimer <= 0)
        {
            transform.Translate(Vector3.up*Speed, Space.World);

            Color color = Text.color;
            color.a = Mathf.Lerp(color.a, 0, Time.fixedDeltaTime * 2);

            if (color.a <= 0.05f)
            {
                ManagerResource.Ins.Save(gameObject);
            }

            Text.color = color;
        }
    }


    public void Set(Color color, bool isPermanent, string message, float speed, float stayTime)
    {
        Text.text = message;
        Text.color = color;
        Speed = speed;
        Permanent = isPermanent;

        _StayTime = stayTime;
    }
}