/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "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;

namespace Utils.Effects {

	/// <summary>
	/// Wiggles (2d).
	/// </summary>
	public class Wiggle2D : Effect {

		[Header("Effect parameters")]
		[Tooltip("The amount to wiggle.")]
		/// <summary>
		/// The amount to wiggle.
		/// </summary>
		public float Amount;
		[Tooltip("How many times to wiggle.")]
		/// <summary>
		/// How many times to wiggle.
		/// </summary>
		public int Times;
		[Tooltip("The ease function for interpolation.")]
		/// <summary>
		/// The ease function for interpolation.
		/// </summary>
		public Ease.EaseFunctions EaseFunction;

		/// <summary>
		/// The original position.
		/// </summary>
		private Vector2 originalPosition;
		/// <summary>
		/// The start position.
		/// </summary>
		private Vector2 startPosition;
		/// <summary>
		/// The target position.
		/// </summary>
		private Vector2 targetPosition;
		/// <summary>
		/// The iterations. Used to end on the original position.
		/// </summary>
		private float iterations;
		/// <summary>
		/// The current iteration. Used to end on the original position.
		/// </summary>
		private int currentIteration;
		
		protected override void InitEffect() {
			// Get inital positions
			originalPosition = transform.localPosition;
			startPosition = originalPosition;
			targetPosition = originalPosition + Random.insideUnitCircle * (Amount / 2f);

			// Get iterations
			iterations = Duration / (Times + 1);
			currentIteration = 0;
		}
		
		protected override void UpdateEffect() {
			// Wiggle like a motherfucker
			float t = (currentTime - currentIteration * iterations) / iterations;
			
			transform.localPosition = Ease.GetEase(EaseFunction, t, startPosition, targetPosition);
			
			if (t >= 1f) {
				currentIteration++;
				startPosition = targetPosition;
				if (currentIteration < Times) targetPosition = originalPosition + Random.insideUnitCircle * (Amount / 2f);
				else targetPosition = originalPosition;
			}
		}
		
		protected override void CleanUpEffect() {
			// Reset position
			transform.localPosition = originalPosition;
		}
	}
}
