using System.Collections;
using UnityEngine;
using System;
using Sirenix.Utilities;

public class EffectTime : MonoBehaviour
{
    [Tooltip("持续时间(-1=无限)")]
    public float duration = -1;

    [Tooltip("延迟")]
    public float delay;

    [Tooltip("播放速度")]
    public float speed = 1;

    private Transform mTransform;
    private ParticleSystem[] childParticle;
    private Animation childAnimation;

    private void Awake()
    {
        this.mTransform = this.transform;
        this.childParticle = GetComponentsInChildren<ParticleSystem>();
        this.childAnimation = GetComponentInChildren<Animation>();
    }

    private void OnEnable()
    {
        if(speed != 1)
        {
            SetPlaySpeed(speed);
        }

        if(delay > 0)
        {
            ShowChildren(false);
            StartCoroutine(DelayShow());
        }
        else
        {
            ShowChildren(true);
        }

        if (childAnimation != null)
        {
            foreach (var ob in childAnimation)
            {
                var state = ob as AnimationState;
                if (state != null)
                {
                    Debug.Log($"state name:{state.name}");
                }
            }
        }
    }

    private IEnumerator DelayShow()
    {
        yield return new WaitForSeconds(delay);
        ShowChildren(true);
    }

    //保存动画和特效的原始播放速度
    private Hashtable mAniSpeeds = new Hashtable();
    private ArrayList mEffSpeeds = new ArrayList();
    public void SetPlaySpeed(float _speed, string aniName = "")
    {
        if (_speed < 0f) _speed = 1f;
        speed = _speed;

        if (!aniName.IsNullOrWhitespace() && childAnimation != null)
        {
            var state = childAnimation[aniName];
            if (state != null)
            {
                float orginSpeed = 1.0f;
                if (mAniSpeeds.ContainsKey(aniName))
                {
                    orginSpeed = (float)mAniSpeeds[aniName];
                }
                else
                {
                    orginSpeed = state.speed;
                    mAniSpeeds[aniName] = orginSpeed;
                }
                state.speed = orginSpeed * speed;
            }
        }

        if (childParticle != null)
        {
            for (int i = 0; i < childParticle.Length; i++)
            {
                var main = childParticle[i].main;
                float orginSpeed;
                if (mEffSpeeds.Count > i)
                {
                    orginSpeed = (float)mEffSpeeds[i];
                }
                else
                {
                    orginSpeed = main.simulationSpeed;
                    mEffSpeeds.Add(orginSpeed);
                }
                main.simulationSpeed = orginSpeed * speed;
            }
        }
    }

    void ShowChildren(bool show)
    {
        var num = mTransform.childCount;
        for (int i = num - 1; i >= 0; i--)
        {
            mTransform.GetChild(i).gameObject.SetActive(show);
        }
    }
}