﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace MPStudio
{
    
    //这个类只针对Text,对象池的东西一定要写死，要设计到回收的。
    public class DelayFadeOut:MonoBehaviour
    {
        public bool isText = true;
        
        [SerializeField] private float delayTime = 1f;

        [SerializeField] private float animalCost = 1f;
        protected virtual void OnEnable()
        {
         //怎么都不正常 算了不搞了
         /*
            CancelInvoke("Animal");
            StopAllCoroutines();
           */ 
            //Invoke("Animal", delayTime);
            
           Animal();
        }

        protected virtual void ActionRun()
        {
            //Destroy(this.gameObject);
            
           // PoolManager.Inst.RecycleGo(this.gameObject);
        }

        protected void OnDisable()
        {
            //如果提前回收,停止所有携程，然后做数据结尾化
            StopAllCoroutines();
            TextColor(1);
            transform.localScale = Vector3.one * 1f;
        }

        protected virtual void TextColor(float f)
        {
            if (isText)
            {
                var sr = transform.GetComponent<Text>();
                sr.color =new Color(sr.color.r,sr.color.g,sr.color.b,f);
            }
            else
            {
                var sr = transform.GetComponent<Text>();
                sr.color =new Color(sr.color.r,sr.color.g,sr.color.b,f);
            }
        }

        void Animal()
        {
            StartCoroutine(TweenManager.Inst.ToFloat(1f, 1.5f,this.delayTime, (f) =>
            {
                transform.localScale = Vector3.one * f;
                //什么都不做
            }, () =>
            {
    
                StartCoroutine(TweenManager.Inst.ToFloat(1f, 0f, animalCost, (f) =>
                {
                    //这里保持为oen，不然递归太复杂
                    TextColor(f);
                }, () =>
                {
                    ActionRun();
                }));
                
            }));
            

        }
    }
}