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

namespace uTools
{
    [AddComponentMenu("uTools/Tween/Tween Rotation(uTools)")]
    public class uTweenRotation : uTweener
    {
        public Vector3 from;
        public Vector3 to;

        public bool isLocalRotation = true;

        Transform mTransfrom;

        public Transform cacheTransfrom
        {
            get
            {
                if (mTransfrom == null)
                {
                    mTransfrom = transform;
                }
                return mTransfrom;
            }
        }

        public Quaternion value
        {
            get
            {
                if (isLocalRotation)
                {
                    return cacheTransfrom.localRotation;
                }
                else
                {
                    return cacheTransfrom.rotation;
                }
            }
            set
            {
                if (isLocalRotation)
                {
                    cacheTransfrom.localRotation = value;
                }
                else
                {
                    cacheTransfrom.rotation = value;
                }
            }
        }

        protected override void OnUpdate(float _factor, bool _isFinished)
        {
            value = Quaternion.Euler(Vector3.LerpUnclamped(from, to, _factor));
        }

        public static uTweenRotation Begin(GameObject go, Vector3 from, Vector3 to, float duration = 1f, float delay = 0f)
        {
            uTweenRotation comp = uTweener.Begin<uTweenRotation>(go, duration);
            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")]
        public override void SetStartToCurrentValue()
        {
            from = value.eulerAngles;
        }

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

        [ContextMenu("Assume value of 'From'")]
        public override void SetCurrentValueToEnd()
        {
            value = Quaternion.Euler(to);
        }

        [ContextMenu("Assume value of 'To'")]
        public override void SetCurrentValueToStart()
        {
            value = Quaternion.Euler(from);
        }
    }
}