
using UnityEngine;

using System.Collections;
using Base.Common;
using uTools;

public class uTweenPositionTarget : uTweener
{
	public Transform from;
	public Transform to;

	Transform mTransform;

	private Vector3 from_pos;
	private Vector3 to_pos;

	public Transform cachedTransform { get { if (mTransform == null) mTransform = GetComponent<Transform>(); return mTransform; } }
	public Vector3 value
	{
		get
		{
			return cachedTransform.position;
		}
		set
		{
			cachedTransform.position = value;
		}
	}

	public void UpdateTargetPos()
	{
        if (from == null || to == null)
        {
            return;
        }
		from_pos = from.position;
		to_pos = to.position;
	}

	protected override void OnUpdate(float factor, bool isFinished)
	{
		if (factor == 0)
			UpdateTargetPos();
		value = from_pos + factor * (to_pos - from_pos);
	}

	public static uTweenPositionTarget Begin(GameObject go, Transform from, Transform to, float duration = 1f, float delay = 0f)
	{
		uTweenPositionTarget comp = uTweener.Begin<uTweenPositionTarget>(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_pos = value; }

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

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

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

}
