using System;
using System.Collections.Generic;
using Fort23.Core;
using UnityEngine;
using Utility.UITool;
using Object = UnityEngine.Object;

namespace Utility.CTween
{
    public static class CustomTweenManager
    {
        public static AnimationCurveLibrary AnimationCurveLibrary
        {
            get { return AnimationCurveManager.Instance.AnimationCurveLibrary; }
        }

        private static int FPS = 30;

        public static List<CustomVectorTween> RuningVectorTweens = new List<CustomVectorTween>();

        public static Queue<CustomVectorTween> VectorTweenPool = new Queue<CustomVectorTween>();


        public static List<CustomFloatTween> RuningFloatTweens = new List<CustomFloatTween>();

        public static Queue<CustomFloatTween> FloatTweenPool = new Queue<CustomFloatTween>();

        public static void Init()
        {
            StaticUpdater.Instance.AddRenderUpdateCallBack(Update);
        }


        /// <summary>
        /// 创建一个tween
        /// </summary>
        /// <param name="getter"></param>
        /// <param name="setter"></param>
        /// <param name="endValue"></param>
        /// <param name="duration"></param>
        /// <param name="target">作用的目标 当目标不存在时tween会被自动回收</param>
        /// <returns></returns>
        public static CustomVectorTween To(CustomGetter<Vector2> getter, CustomSetter<Vector2> setter, Vector2 endValue, float duration, GameObject target)
        {
            CustomVectorTween customVectorTween;
            if (VectorTweenPool.Count > 0)
            {
                customVectorTween = VectorTweenPool.Dequeue();
            }
            else
            {
                customVectorTween = new CustomVectorTween();
            }

            customVectorTween.CustomInit(getter, setter, endValue, duration, target);
            RuningVectorTweens.Add(customVectorTween);
            return customVectorTween;
        }

        public static CustomFloatTween To(CustomGetter<float> getter, CustomSetter<float> setter, float endValue, float duration, GameObject target)
        {
            CustomFloatTween customFloatTween;
            if (FloatTweenPool.Count > 0)
            {
                customFloatTween = FloatTweenPool.Dequeue();
            }
            else
            {
                customFloatTween = new CustomFloatTween();
            }

            customFloatTween.CustomInit(getter, setter, endValue, duration, target);
            RuningFloatTweens.Add(customFloatTween);
            return customFloatTween;
        }

        public static void Update()
        {
            for (var i = 0; i < RuningVectorTweens.Count; i++)
            {
                RuningVectorTweens[i].Update();
            }

            for (var i = 0; i < RuningFloatTweens.Count; i++)
            {
                RuningFloatTweens[i].Update();
            }
        }

        public static void VectorRecycle(CustomVectorTween customVectorTween)
        {
            RuningVectorTweens.Remove(customVectorTween);
            
            //VectorTweenPool.Enqueue(customVectorTween);
        }

        public static void FloatRecycle(CustomFloatTween customFloatTween)
        {
            RuningFloatTweens.Remove(customFloatTween);
           // FloatTweenPool.Enqueue(customFloatTween);
        }
    }
}