using UnityEngine;
using System.Collections;

namespace uTools
{
    public class uTweenPosition_Axis : uTweenValue
    {
        public enum Axis
        {
            x,
            y,
            z,
        }

        public Axis axis;
        public bool isRect = true;
        public bool isWorldPos = false;
        private Transform origin;
        private Transform target;
        public Transform Origin
        {
            get
            {
                return origin;
            }
            set
            {
                origin = value;
                switch (axis)
                {
                    case Axis.x:
                        from = origin.position.x;
                        break;
                    case Axis.y:
                        from = origin.position.y;
                        break;
                    case Axis.z:
                        from = origin.position.z;
                        break;
                    default:
                        break;
                }
            }
        }
        public Transform Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
                switch (axis)
                {
                    case Axis.x:
                        to = target.position.x;
                        break;
                    case Axis.y:
                        to = target.position.y;
                        break;
                    case Axis.z:
                        to = target.position.z;
                        break;
                    default:
                        break;
                }
            }
        }

        RectTransform mRectTransform;

        Transform mTransform;
        public RectTransform cachedRectTransform { get { if (mRectTransform == null) mRectTransform = GetComponent<RectTransform>(); return mRectTransform; } }

        public Transform cachedTransform { get { if (mTransform == null) mTransform = GetComponent<Transform>(); return mTransform; } }
        public Vector3 value
        {
            get
            {
                if (isWorldPos)
                    return cachedTransform.position;
                else if (isRect)
                    return cachedRectTransform.anchoredPosition;
                else
                    return cachedTransform.localPosition;
            }
            set
            {
                if (isWorldPos)
                    cachedTransform.position = value;
                else if (isRect)
                    cachedRectTransform.anchoredPosition = value;
                else
                    cachedTransform.localPosition = value;
            }
        }

        protected override void ValueUpdate(float value, bool isFinished)
        {
            base.ValueUpdate(value, isFinished);
            if (origin)
            {
                switch (axis)
                {
                    case Axis.x:
                        from = origin.position.x;
                        break;
                    case Axis.y:
                        from = origin.position.y;
                        break;
                    case Axis.z:
                        from = origin.position.z;
                        break;
                    default:
                        break;
                }
            }
            if (target)
            {
                switch (axis)
                {
                    case Axis.x:
                        to = target.position.x;
                        break;
                    case Axis.y:
                        to = target.position.y;
                        break;
                    case Axis.z:
                        to = target.position.z;
                        break;
                    default:
                        break;
                }
            }
            switch (axis)
            {
                case Axis.x:
                    this.value = new Vector3(value, this.value.y, this.value.z);
                    break;
                case Axis.y:
                    this.value = new Vector3(this.value.x, value, this.value.z);
                    break;
                case Axis.z:
                    this.value = new Vector3(this.value.x, this.value.y, value);
                    break;
                default:
                    break;
            }
        }
    }
}
