/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "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>
	/// Scales.
	/// </summary>
	public class Scale : 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 axis influence.")]
		/// <summary>
		/// The axis influence.
		/// </summary>
		public Vector3 Influence;
		[Tooltip("The amount to scale by.")]
		/// <summary>
		/// The amount to scale by.
		/// </summary>
		public float Amount;

		/// <summary>
		/// The original scale.
		/// </summary>
		private Vector3 originalScale;
		/// <summary>
		/// The target scale.
		/// </summary>
		private Vector3 targetScale;

		protected override void InitEffect() {
			// Get original scale and calculate target scale
			originalScale = transform.localScale;
			targetScale = originalScale + Influence * Amount;
		}
		
		protected override void UpdateEffect() {
			// Update scale according to settings
			if (UseCurve) transform.localScale = Ease.GetEase(Curve, currentTime / Duration, originalScale, targetScale);
			else transform.localScale = Ease.GetEase(EaseFunction, currentTime / Duration, originalScale, targetScale);
		}
		
		protected override void CleanUpEffect() {
			// Reset scale if necessairy
			if (ResetAtTheEnd) transform.localScale = originalScale;
		}
	}
}
