/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "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>
	/// Fades the alpha values of all materials.
	/// </summary>
	public class FadeAlpha : Effect {

		[Header("Effect parameters")]
		[Tooltip("The start alpha value.")]
		/// <summary>
		/// The start alpha value.
		/// </summary>
		public float Start = 0f;
		[Tooltip("The end alpha value.")]
		/// <summary>
		/// The end alpha value.
		/// </summary>
		public float End = 1f;
		[Tooltip("The ease function to use.")]
		/// <summary>
		/// The ease function to use.
		/// </summary>
		public Ease.EaseFunctions EaseFunction = Ease.EaseFunctions.Linear;

		/// <summary>
		/// Reference to the renderer.
		/// </summary>
		private Renderer r;
		/// <summary>
		/// The colors of the materials.
		/// </summary>
		private Color[] colors;

		protected override void InitEffect() {
			// Get renderer
			r = GetComponent<Renderer>();

			// Get all colors of the materials
			colors = new Color[r.materials.Length];
			for (int i = 0; i < r.materials.Length; i++) {
				colors[i] = r.materials[i].color;
			}
		}

		protected override void UpdateEffect() {
			// Get the current alpha value
			float a;
			if (Start > End) a = Start - Ease.GetEase(EaseFunction, currentTime / Duration, End, Start);
			else a = Ease.GetEase(EaseFunction, currentTime / Duration, Start, End);

			// Apply the alpha value
			for (int i = 0; i < r.materials.Length; i++) {
				colors[i].a = a;
				r.materials[i].color = colors[i];
			}
		}

		protected override void CleanUpEffect() {
			// Set all alpha values to the end value
			for (int i = 0; i < r.materials.Length; i++) {
				colors[i].a = End;
				r.materials[i].color = colors[i];
			}
		}
	}
}
