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

namespace uTools
{
    [AddComponentMenu("uTools/Tween/Tween Color(uTools)")]
    public class uTweenColor : uTweener
    {
        Transform mTransform;
        public TweenTarget colorType = TweenTarget.UI;
        public Color from = Color.white;
        public Color to = Color.white;
        public bool includeChilds = false;

        List<SpriteRenderer> spList = new List<SpriteRenderer>();
        List<Graphic> gaList = new List<Graphic>();
        List<TextMesh> textMeshList = new List<TextMesh>();

        Color mColor = Color.white;

        public Transform cachedTransform { get { if (mTransform == null) mTransform = transform; return mTransform; } }

        protected override void Awake()
        {
            GetTweenList();
            base.Awake();
        }

        void GetTweenList()
        {
            spList.Clear();
            gaList.Clear();
            textMeshList.Clear();

            Queue<Transform> childList = new Queue<Transform>();
            childList.Enqueue(cachedTransform);

            while (childList.Count > 0)
            {
                Transform t = childList.Dequeue();
                if (colorType == TweenTarget.SpriteRender)
                {
                    SpriteRenderer m_sp = t.GetComponent<SpriteRenderer>();
                    if (null != m_sp) spList.Add(m_sp);
                }
                else if (colorType == TweenTarget.UI)
                {
                    Graphic m_graphic = t.GetComponent<Graphic>();
                    if (null != m_graphic) gaList.Add(m_graphic);
                }
                else if (colorType == TweenTarget.TextMesh)
                {
                    TextMesh m_textMesh = t.GetComponent<TextMesh>();
                    if (null != m_textMesh) textMeshList.Add(m_textMesh);
                }
                if (includeChilds)
                {
                    for (int i = 0; i < t.childCount; i++)
                    {
                        childList.Enqueue(t.GetChild(i));
                    }
                }
            }
        }

        public Color colorValue
        {
            get
            {
                return mColor;
            }
            set
            {
                SetColor(transform, value);
                mColor = value;
            }
        }

        protected override void OnUpdate(float factor, bool isFinished)
        {
            colorValue = Color.Lerp(from, to, factor);
        }

        public static uTweenColor Begin(GameObject go, float duration, float delay, Color from, Color to)
        {
            uTweenColor comp = uTweener.Begin<uTweenColor>(go, duration);
            comp.from = from;
            comp.to = to;
            comp.delay = delay;

            if (duration <= 0)
            {
                comp.Sample(1, true);
                comp.enabled = false;
            }
            return comp;
        }

        void SetColor(Transform _transform, Color _color)
        {
            if (colorType == TweenTarget.SpriteRender)
            {
                if (spList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < spList.Count; i++)
                {
                    SpriteRenderer m_sp = spList[i];
                    m_sp.color = _color;
                }
            }
            else if (colorType == TweenTarget.UI)
            {
                if (gaList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < gaList.Count; i++)
                {
                    Graphic m_ga = gaList[i];
                    m_ga.color = _color;
                }
            }
            else if (colorType == TweenTarget.TextMesh)
            {
                if (textMeshList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < textMeshList.Count; i++)
                {
                    TextMesh m_textMesh = textMeshList[i];
                    m_textMesh.color = _color;
                }
            }
        }
    }
}
