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

namespace uTools
{
    public enum TweenTarget
    {
        SpriteRender,
        UI,
        TextMesh,
        MeshRenderer,
    }

    [AddComponentMenu("uTools/Tween/Tween Alpha(uTools)")]
    public class uTweenAlpha : uTweenValue
    {
        Transform mTransform;
        public TweenTarget alphaType = TweenTarget.UI;

        public bool includeChilds = false;

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

        float mAlpha = 0f;

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

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

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

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

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

        public float alpha
        {
            get
            {
                return mAlpha;
            }
            set
            {
                SetAlpha(value);
                mAlpha = value;
            }
        }

        protected override void ValueUpdate(float value, bool isFinished)
        {
            alpha = value;
        }

        void SetAlpha(float _alpha)
        {
            Color c = Color.white;
            if (alphaType == TweenTarget.SpriteRender)
            {
                if (spList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < spList.Count; i++)
                {
                    SpriteRenderer m_sp = spList[i];
                    c = m_sp.color;
                    c.a = _alpha;
                    m_sp.color = c;
                }
            }
            else if (alphaType == TweenTarget.UI)
            {
                if (gaList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < gaList.Count; i++)
                {
                    Graphic m_ga = gaList[i];
                    c = m_ga.color;
                    c.a = _alpha;
                    m_ga.color = c;
                }
            }
            else if (alphaType == TweenTarget.TextMesh)
            {
                if (textMeshList.Count == 0)
                {
                    GetTweenList();
                }
                for (int i = 0; i < textMeshList.Count; i++)
                {
                    TextMesh m_textMesh = textMeshList[i];
                    c = m_textMesh.color;
                    c.a = _alpha;
                    m_textMesh.color = c;
                }
            }
            else if (alphaType == TweenTarget.MeshRenderer)
            {
                if(meshRendererList.Count == 0)
                {
                    GetTweenList();
                }
                for(int i = 0; i < meshRendererList.Count; i ++)
                {
                    c = meshRendererList[i].material.GetColor("_TintColor");
                    if(null != c)
                    {
                        c.a = _alpha;
                        meshRendererList[i].material.SetColor("_TintColor", c);
                    }
                }
            }
        }

        public override void SetCurrentValueToEnd()
        {
            base.SetCurrentValueToEnd();
            alpha = to;
        }

        public override void SetCurrentValueToStart()
        {
            base.SetCurrentValueToStart();
            alpha = from;
        }
    }
}