using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

namespace MPStudio
{
    [DisallowMultipleComponent]
    [DefaultExecutionOrder(-1)]
    public class TweenManager : MonoBehaviour
    {
        public Text testText;
        public Transform testSlider;

        public bool isTest = false;

        public static TweenManager Inst { get; private set; }

        protected virtual void Awake()
        {
            if (Inst == null)
            {
                Inst = this;
            }
            else if (Inst != this)
            {
                Destroy(gameObject);
            }
        }

        public IEnumerator ByVec3(Vector3 startValue, Vector3 offsetValue, float duration, Action<Vector3> func,
            Action endAction = null)
        {
            float timer = 0f;

            Vector3 targetValue = startValue + offsetValue;

            while (timer < duration)
            {
                float t = timer / duration;
                Vector3 interpolatedValue = Vector3.Lerp(startValue, targetValue, t);
                func.Invoke(interpolatedValue);
                timer += Time.deltaTime;
                yield return null;
            }

            func.Invoke(targetValue);
            endAction?.Invoke();
        }

        public IEnumerator ToVec3(Vector3 startValue, Vector3 targetValue, float duration, Action<Vector3> func,
            Action endAction = null)
        {
            float timer = 0f;
            while (timer < duration)
            {
                float t = timer / duration;
                Vector3 interpolatedValue = Vector3.Lerp(startValue, targetValue, t);
                func.Invoke(interpolatedValue);
                timer += Time.deltaTime;
                yield return null;
            }

            func.Invoke(targetValue);
            endAction?.Invoke();
        }

        public IEnumerator ToFloat(float startValue, float targetValue, float duration, Action<float> func,
            Action endAction = null)
        {
            float timer = 0f;
            while (timer < duration)
            {
                float t = timer / duration;
                float interpolatedValue = Mathf.Lerp(startValue, targetValue, t);
                func.Invoke(interpolatedValue);

                timer += Time.deltaTime;
                yield return null;
                // print("hai zai zhi xing");
            }

            func.Invoke(targetValue); //lerp不会超过1，再invoke一次保险点
            endAction?.Invoke();
        }

        //相对
        public IEnumerator ByFloat(float startValue, float offsetValue, float duration, Action<float> func,
            Action endAction = null)
        {
            float timer = 0f;

            float targetValue = startValue + offsetValue;

            while (timer < duration)
            {
                float t = timer / duration;
                float interpolatedValue = Mathf.Lerp(startValue, targetValue, t);
                func.Invoke(interpolatedValue);
                // print("interpolate : "+interpolatedValue);

                timer += Time.deltaTime;
                yield return null;
            }

            func.Invoke(targetValue);
            endAction?.Invoke();
        }

        public IEnumerator ToFloat(double startValue, double targetValue, double duration, Action<double> func,
            Action endAction = null)
        {
            double timer = 0f;
            while (timer < duration)
            {
                double t = timer / duration;
                double interpolatedValue = Mathf.Lerp((float)startValue, (float)targetValue, (float)t);
                func.Invoke(interpolatedValue);

                timer += Time.deltaTime;
                yield return null;
            }

            func.Invoke(targetValue); //lerp不会超过1，再invoke一次保险点
            endAction?.Invoke();
        }

        public IEnumerator ByFloat(double startValue, double offsetValue, double duration, Action<double> func,
            Action endAction = null)
        {
            double timer = 0f;

            double targetValue = startValue + offsetValue;

            while (timer < duration)
            {
                double t = timer / duration;
                double interpolatedValue = Mathf.Lerp((float)startValue, (float)targetValue, (float)t);
                func.Invoke(interpolatedValue);
                // print("interpolate : "+interpolatedValue);

                timer += Time.deltaTime;
                yield return null;
            }

            func.Invoke(targetValue);
            endAction?.Invoke();
        }

        public void  AnimalCurve(ref float animalVal,float x)
        {
            animalVal = 3f / (x +2f);
            animalVal=  Mathf.Clamp01(animalVal);
        }
        public void  AnimalCurve2(ref float animalVal,float x)
        {
            animalVal = -(x * x) + x;
            animalVal=  Mathf.Clamp01(animalVal);
        }

    public void GrowScale2()
        {
            throw new NotImplementedException();
        }

        //1写死了，如果默认大小不是1会有bug，需要自己修改
        public void GrowScale( Transform trans,float animalCost=1f, float initScale = 0f, float targetScale = 1f, Action endAction = null)
        {
            
            StartCoroutine(ToFloat(initScale, targetScale, animalCost, (f) =>
            {
                //这里保持为one，不然递归太复杂
                trans.localScale = Vector3.one * f;
            }, () =>
            {
                endAction?.Invoke();
            }));
        }
        
        public void GrowAlpha( Transform trans,bool isSp=true,float animalCost=1f, float initScale = 1f, float targetScale = 0f, Action endAction = null)
        {
            
            StartCoroutine(ToFloat(initScale, targetScale, animalCost, (f) =>
            {
                //这里保持为oen，不然递归太复杂
                if (isSp)
                {
                    var sr = trans.GetComponent<SpriteRenderer>();
                    sr.color =new Color(sr.color.r,sr.color.g,sr.color.b,f);
                }
                else
                {
                    var img = trans.GetComponent<Image>();
                    img.color = new Color(img.color.r,img.color.g,img.color.b,f);
                }
            }, () =>
            {
                endAction?.Invoke();
            }));
        }
        
        public void FadeIn( Transform trans,bool isSp=true,float animalCost=1f, Action endAction = null)
        {
            var go = trans.gameObject;
            go.SetActive(true);
            
            StartCoroutine(ToFloat(0, 1, animalCost, (f) =>
            {
                //这里保持为oen，不然递归太复杂
                if (isSp)
                {
                    var sr = trans.GetComponent<SpriteRenderer>();
                    sr.color =new Color(sr.color.r,sr.color.g,sr.color.b,f);
                }
                else
                {
                    var img = trans.GetComponent<CanvasGroup>();
                    if (img == null)
                    {
                        img = trans.AddComponent<CanvasGroup>();
                    }
                    img.alpha = f;
                }
            }, () =>
            {
                endAction?.Invoke();
            }));
        }
        
        public void FadeOut( Transform trans,bool isSp=true,float animalCost=1f, Action endAction = null)
        {
            var go = trans.gameObject;
            go.SetActive(true);
            
            StartCoroutine(ToFloat(1, 0, animalCost, (f) =>
            {
                //这里保持为oen，不然递归太复杂
                if (isSp)
                {
                    var sr = trans.GetComponent<SpriteRenderer>();
                    sr.color =new Color(sr.color.r,sr.color.g,sr.color.b,f);
                }
                else
                {
                    var img = trans.GetComponent<CanvasGroup>();
                    if (img == null)
                    {
                        img = trans.AddComponent<CanvasGroup>();
                    }
                    img.alpha = f;
                }
            }, () =>
            {
                go.SetActive(false);
                endAction?.Invoke();
            }));
        }
        
      public  IEnumerator TypeText(Text textComponent,string fullText="",float delay=0.05f, Action endAction=null)
        {
            for (int i = 0; i <= fullText.Length; i++)
            {
               string currentText = fullText.Substring(0, i);
                textComponent.text = currentText;
                yield return new WaitForSeconds(delay);
                endAction?.Invoke();
            }
        }
        

        public void CurtainChange(RectTransform curtainPanel,float animalCost =0.5f,Action overAction =null)
        {
            var go = curtainPanel.gameObject;
            var img = go.GetComponent<Image>();
            go.SetActive(true);
            var logoImg = curtainPanel.gameObject.transform.Find("loadImage").GetComponent<Image>();

            StartCoroutine(ToFloat(0, 1, animalCost, (f) =>
            {
                img.color = new Color(0,0,0,f);
            }, () =>
            {
                logoImg.enabled = true;
                StartCoroutine(ToFloat(1, 1, animalCost, (f) =>
                {
                    img.color = new Color(0,0,0,f);
                }, () =>
                {
                    logoImg.enabled = false;
                    StartCoroutine(ToFloat(1, 0, animalCost, (f) =>
                    {
                        img.color = new Color(0,0,0,f);
                    }, () =>
                    {
                        overAction?.Invoke();
                        go.SetActive(false);
                    }));
                }));
            }));
        }
        
        protected virtual void Start()
        {
        }

        private void Update()
        {
            if (isTest)
            {
                if (Input.GetKeyDown(KeyCode.F))
                {
                   // print("fff");
                    var trans = GameObject.Find("白环2").transform;
                    GrowScale(trans);
                    GrowAlpha(trans);
                }else if (Input.GetKeyDown(KeyCode.G))
                {
                    var trans = GameObject.Find("Canvas/CurtainPanel").GetComponent<RectTransform>();
                    CurtainChange(trans,2f);
                }else if (Input.GetKeyDown(KeyCode.V))
                {
                    if (testText)
                    {
                        StartCoroutine(TweenManager.Inst.TypeText(testText, "你好我是这个时间上最好的人，感谢你们的支持呢。"));
                    }
                    
                    if(testSlider)
                    FadeIn(testSlider,false,2);
                }else if (Input.GetKeyDown(KeyCode.C))
                {
                  if(testSlider)
                    FadeOut(testSlider,false,2);
                }else if (Input.GetKeyDown(KeyCode.Y))
                {
                    GameObject.Find("白环5").SendMessage("Hide");
                }
            }
        }

        /*
        void Start()
        {
            StartCoroutine(ByFloat(-1f, 2f, 3.2f,(float f)=> {
                transform.position = new Vector3(0, f, 0);
            },()=> {
                Debug.Log("is over");
            }));
        }
        */
    }
}