using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Thousandto.Plugins.Common;

using System;
using Thousandto.Core.Base;

namespace Thousandto.Plugins.Common
{
    /// <summary>
    /// 星星特效管理类
    /// </summary>
    public class StarMoveManager
    {
        #region//私有变量
        private float _clearTime = 0f;
        private StarEffectAttr _fristFreeObj = null;
        private StarMoveManager _manager = null;
        private List<StarEffectAttr> _freeList = new List<StarEffectAttr>();
        private List<StarEffectAttr> _runList = new List<StarEffectAttr>();
        #endregion

        #region//公共变量
        public StarMoveManager Manager
        {
            get { if (_manager == null)
                {
                    _manager = new StarMoveManager();
                }
                return _manager;
            }
        }
        public List<StarEffectAttr> FreeList
        {
            get
            {
                return _freeList;
            }

            set
            {
                _freeList = value;
            }
        }
        public List<StarEffectAttr> RunList
        {
            get
            {
                return _runList;
            }

            set
            {
                _runList = value;
            }
        }
        #endregion

        #region//公共接口
        public void Push(GameObject go,GameObject endGo,Vector3 startPos,Vector3 endPos,int count = 0,MyAction func = null )
        {
            if (_freeList != null)
            {
                StarEffectAttr tempObj = null;
                if (_freeList.Count == 0)
                {
                    //创建一个节点
                    GameObject cloneGo = UIUtility.Clone(go);
                    if (cloneGo != null)
                    {
                        tempObj = new StarEffectAttr(cloneGo, endGo, startPos, endPos, func);
                        tempObj.Index = count;
                    }
                }
                else
                {
                    tempObj = _freeList[0];
                    tempObj.EndGo = endGo;
                    tempObj.EndPos = endPos;
                    tempObj.Index = count;
                    if (tempObj.PlayStarVfxCallBack == null)
                    {
                        tempObj.PlayStarVfxCallBack = func;
                    }
                    _freeList.Remove(_freeList[0]);
                    if (_freeList.Count > 0 && _clearTime == 0f)
                    {
                        _fristFreeObj = _freeList[0];
                    }
                }
                _runList.Add(tempObj);
                tempObj.SetManager(this);
                tempObj.PlayeVfxAction();
            }
        }

        //插入等待释放列表
        public void Pop(StarEffectAttr data)
        {
            if (_runList != null && _runList.Contains(data))
            {
                if (_freeList != null)
                {
                    _freeList.Add(data);
                }
                _runList.Remove(data);
            }
        }

        //移除和销毁所有特效
        public void Destory()
        {
            Clear(_freeList);
            Clear(_runList);
            _fristFreeObj = null;
        }

        private void Clear(List<StarEffectAttr> list)
        {
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i] != null)
                    {
                        list[i].Destory();
                    }
                }
                list.Clear();
            }
        }

        //清理缓存处理
        private void ClearCache()
        {
            if (_fristFreeObj != null)
            {
                if (_clearTime < 10f)
                {
                    _clearTime += Time.deltaTime;
                }
                else
                {
                    _clearTime = 0f;
                    if (_freeList != null && _freeList.Count > 0)
                    {
                        if (_fristFreeObj == _freeList[0])
                        {
                            //外部操作已经结束或者停止太久 释放缓存
                            Destory();
                        }
                    }
                }
            }
        }

        private void UpdateStarEffect()
        {
            for (int i = 0; i < _runList.Count; i++)
            {
                if (_runList[i] != null)
                {
                    _runList[i].Update();
                }
            }
        }

        public void Update()
        {
            ClearCache();
            UpdateStarEffect();
        }
        #endregion
    }
}