/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using UnityEngine;
using Utils;

namespace Utils.Effects {

	/// <summary>
	/// Moves.
	/// </summary>
	public class Move : Effect {
		
		[Header("Function parameters")]
		[Tooltip("Determies whether to use the curve or the ease function.")]
		/// <summary>
		/// Determies whether to use the curve or the ease function.
		/// </summary>
		public bool UseCurve = false;
		[Tooltip("The curve.")]
		/// <summary>
		/// The curve.
		/// </summary>
		public AnimationCurve Curve;
		[Tooltip("The ease function.")]
		/// <summary>
		/// The ease function.
		/// </summary>
		public Ease.EaseFunctions EaseFunction;

		[Header("Effect parameters")]
		[Tooltip("Whether to reset to the original position at the end.")]
		/// <summary>
		/// Whether to reset to the original position at the end.
		/// </summary>
		public bool ResetAtTheEnd = false;
		[Tooltip("The direction.")]
		/// <summary>
		/// The direction.
		/// </summary>
		public Vector3 Direction;
		[Tooltip("The amount to move by.")]
		/// <summary>
		/// The amount to move by.
		/// </summary>
		public float Amount;

		/// <summary>
		/// The original position.
		/// </summary>
		private Vector3 originalPosition;
		/// <summary>
		/// The target position.
		/// </summary>
		private Vector3 targetPosition;
		
		protected override void InitEffect() {
			// Get original position and calculate target position
			originalPosition = transform.localPosition;
			targetPosition = originalPosition + Direction * Amount;
		}
		
		protected override void UpdateEffect() {
			// Update position according to settings
			if (UseCurve) transform.localPosition = Ease.GetEase(Curve, currentTime / Duration, originalPosition, targetPosition);
			else transform.localPosition = Ease.GetEase(EaseFunction, currentTime / Duration, originalPosition, targetPosition);
		}
		
		protected override void CleanUpEffect() {
			// Reset position if necessairy
			if (ResetAtTheEnd) transform.localPosition = originalPosition;
		}
	}
}
