using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spinner : MonoBehaviour {

    // PUBLIC FIELDS //
    #region
    [Header("Spin Settings")]

	[Tooltip("X = Pitch, Y = Yaw, Z = Roll")]
	public Vector3 spinSpeeds;

	[Header("Randomization")]


	public bool randomizeStartRotation;

	public float speedVariation;

	[Range(0, 10)]
	public float sizeVariation;
    #endregion

    // PRIVATE FIELDS

    private Quaternion initialRotation; 
	private float pitch = 0, yaw = 0, roll = 0;


    // DEFAULT METHODS //

	void Start()
    {
        ProcessRandomization();
    }

    void Update ()
    {
        UpdatePersistantValues();
        ApplyRotations();
    }

    // PRIVATE METHODS //

	/// <summary>
	/// Sets randomization parameters on spawn
	/// </summary>
    private void ProcessRandomization()
    {
        if (randomizeStartRotation)
            initialRotation = transform.localRotation = Quaternion.Euler(Random.Range(0, 360), Random.Range(0, 360), Random.Range(0, 360));
        else
            initialRotation = transform.localRotation;

        if (sizeVariation != 0)
            transform.localScale *= Random.Range(sizeVariation / 2, sizeVariation);

        if (speedVariation != 0)
            spinSpeeds *= Random.Range(-speedVariation, speedVariation);
    }

    /// <summary>
    /// Updates our persistant rotation values based on our public spinSpeeds field
    /// </summary>
    private void UpdatePersistantValues()
    {
        if (spinSpeeds.x != 0)
        {
            pitch += spinSpeeds.x * Time.deltaTime;
            pitch = pitch > 360 ? pitch - 360 : pitch;
            pitch = pitch < 0 ? pitch + 360 : pitch;
        }

        if (spinSpeeds.y != 0)
        {
            yaw += spinSpeeds.y * Time.deltaTime;
            yaw = yaw > 360 ? yaw - 360 : yaw;
            yaw = yaw < 0 ? yaw + 360 : yaw;
        }

        if (spinSpeeds.z != 0)
        {
            roll += spinSpeeds.z * Time.deltaTime;
            roll = roll > 360 ? roll - 360 : roll;
            roll = roll < 0 ? roll + 360 : roll;
        }
    }

    /// <summary>
    /// Applies rotations based on our initial rotation and the updated persistant values
    /// </summary>
    private void ApplyRotations()
    {
        // Reset our transform to the initial rotation
        transform.localRotation = initialRotation;
        
        // Apply rotation around the local Y axis first
        transform.localRotation *= Quaternion.Euler(0, -yaw, 0);

        // Apply rotation around the local X axis next
        transform.localRotation *= Quaternion.Euler(pitch, 0, 0);

        // Apply rotation around the local Z axis last
        transform.localRotation *= Quaternion.Euler(0, 0, roll);

        //transform.Rotate(initialForward, spinSpeed * Time.deltaTime);
    }
}
