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

		[Header("Effect parameters")]
		[Tooltip("How many times to flicker.")]
		/// <summary>
		/// How many times to flicker.
		/// </summary>
		public int Times;
		[Tooltip("Determies wheter to fade out at the end or to return to normal.")]
		/// <summary>
		/// Determies wheter to fade out at the end or to return to normal.
		/// </summary>
		public bool FadeOut;

		/// <summary>
		/// Reference to the renderer.
		/// </summary>
		private Renderer r;
		/// <summary>
		/// The amount it will flicker (used for fadeout).
		/// </summary>
		private float amount;
		/// <summary>
		/// All colors of the materials.
		/// </summary>
		private Color[] colors;

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

			// Calculate amount
			amount = Times;
			if (FadeOut) amount += 0.5f;

			// Get all colors
			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() {
			// Calculate alpha value
			float a = 1f - Mathf.Abs(Mathf.Sin(currentTime / Duration * amount * Mathf.PI)); // TODO: Use start alpha instead of 1f!

			// Apply alpha value to all colors
			for (int i = 0; i < r.materials.Length; i++) {
				colors[i].a = a;
				r.materials[i].color = colors[i];
			}
		}
		
		protected override void CleanUpEffect() {
			// Apply final alpha value to all colors
			for (int i = 0; i < r.materials.Length; i++) {
				colors[i].a = (FadeOut) ? 0f : 1f;
				r.materials[i].color = colors[i];
			}
		}
	}
}