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

namespace Rokid.MRC
{
    public class TweenPosition : Tweener
    {
        public Vector3 from;

        public Vector3 to;

        public Vector3 tweenDirection = Vector3.one;

        RectTransform mRectTransform = null;
        Transform mTransform = null;

        bool mIs3D = true;
        private bool is3D
        {
            //get
            //{
            //    if (mTransform == null)
            //    {
            //        mTransform = transform;
            //        RectTransform rect = cachedTransform as RectTransform;
            //        mIs3D = (rect != null) ? false : true;
            //    }
            //    return mIs3D;
            //}

            get
            {
                return mIs3D;
            }
        }

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

        RectTransform cachedRectTransform
        {
            get
            {
                if(mRectTransform == null)
                {
                    mRectTransform = cachedTransform as RectTransform;
                }
                return mRectTransform;
            }
        }

        private Vector3 GetCurrentPosition(Vector3 pos)
        {
            Vector3 tmptargetpos = is3D ? cachedTransform.localPosition : cachedRectTransform.anchoredPosition3D;
            Vector3 tmpvalue = pos;
            if(tweenDirection.x == 0)
            {
                tmpvalue.x = tmptargetpos.x;
            }
            if(tweenDirection.y == 0)
            {
                tmpvalue.y = tmptargetpos.y;
            }
            if(tweenDirection.z == 0)
            {
                tmpvalue.z = tmptargetpos.z;
            }
            return tmpvalue;
        }

        public Vector3 value
        {
            get
            {
                if(is3D)
                {
                    return cachedTransform.localPosition;
                }
                else
                {
                    return cachedRectTransform.anchoredPosition;
                }
            }
            set
            {
                if(is3D)
                {
                    cachedTransform.localPosition = GetCurrentPosition(value);
                }
                else
                {
                    cachedRectTransform.anchoredPosition = GetCurrentPosition(value);
                }
            }
        }

        protected override void OnUpdate(float factor, bool isFinished)
        {
            value = from + factor * (to - from);
        }

        //调用该接口，进行缓动
        public static TweenPosition Begin(GameObject go, Vector3 from, Vector3 to, float duration = 1f, float delay = 0f)
        {
            TweenPosition comp = Tweener.Begin<TweenPosition>(go, duration);
            comp.value = from;
            comp.from = from;
            comp.to = to;
            comp.duration = duration;
            comp.delay = delay;
            if(duration <= 0)
            {
                comp.Sample(1, true);
                comp.enabled = false;
            }
            return comp;
        }

        [ContextMenu("Set 'From' to current value")]
        protected override void SetStartToCurrentValue()
        {
            from = value;
        }

        [ContextMenu("Set 'To' to current value")]
        protected override void SetEndToCurrentValue()
        {
            to = value;
        }

        [ContextMenu("Assume value of 'From'")]
        protected override void SetCurrentValueToStart()
        {
            value = from;
        }

        [ContextMenu("Assume value of 'To'")]
        protected override void SetCurrentValueToEnd()
        {
            value = to;
        }

#if UNITY_EDITOR

        [ContextMenu("Horizontal PingPong 6 pixel")]
        protected void HorizontalaPingPong()
        {
            Vector3 pos = cachedRectTransform.anchoredPosition3D;
            from = new Vector3(pos.x + 3f, pos.y, pos.z);
            to = new Vector3(pos.x - 3f, pos.y, pos.z);
            style = iTween.LoopType.pingPong;
            method = iTween.EaseType.linear;
            duration = 1f;
        }

        [ContextMenu("Vertical PingPong 6 pixel")]
        protected void VerticalPingPong()
        {
            Vector3 pos = cachedRectTransform.anchoredPosition3D;
            from = new Vector3(pos.x, pos.y + 3f, pos.z);
            to = new Vector3(pos.x, pos.y - 3f, pos.z);
            style = iTween.LoopType.pingPong;
            method = iTween.EaseType.linear;
            duration = 1f;
        }

#endif

    }
}

