using System;
using UnityEngine;

namespace EscapeRoomEngine.Desert.Runtime.Puzzle_B
{
    /// <summary>
    /// The rotator component used by the hexagon module.
    /// </summary>
    [RequireComponent(typeof(Emission))]
    public class Rotator : MonoBehaviour
    {
        [Tooltip("The angle this rotator should rotate towards.")]
        [Range(0, 359)]
        public int angle;
        [Tooltip("How quickly to rotate towards the target angle.")]
        [Range(0, 1)]
        public float speed;

        public Emission Emission { get; private set; }

        private void Awake()
        {
            Emission = GetComponent<Emission>();
        }

        private void Update()
        {
            angle %= 360;
            var angleDifference = angle - transform.localEulerAngles.y;
            if (Math.Abs(angleDifference) >= 0.1)
            {
                transform.Rotate(0, angleDifference*Mathf.Pow(speed, 2), 0, Space.Self);
            }
        }
    }
}
