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

public class spotlightHandler : MonoBehaviour {

    //How far along the rotation arc the spotlight has travelled
    private float horizontalDistRotated = 0;
    private float verticalDistRotated = 0;

    //Randomly generated rotation speed
    private float horizontalRotationSpeed = 0;
    private float verticalRotationSpeed = 0;

    private bool rotatingRight = true;
    private bool rotatingUp = true;

	// Use this for initialization
	void Start () {

        //Set rotation to the default 'facing stage' value
        transform.eulerAngles = new Vector3(45, 180, 0);

        horizontalRotationSpeed = Random.value + 0.1f;
        verticalRotationSpeed = Random.value;

        //Give a 50% chance that the spotlight will begin
        //rotating in the opposite direction.
        float rotDirection = Mathf.Round(Random.value);
        if(rotDirection == 0)
        {
            rotatingRight = false;
        }

        //Give a 50% chance that the spotlight will begin
        //rotating in the opposite direction.
        rotDirection = Mathf.Round(Random.value);
        if (rotDirection == 0)
        {
            rotatingUp = false;
        }
    }
	
	// Update is called once per frame
	void Update () {
        //Horizontal rotation.
		if(rotatingRight)
        {
            transform.Rotate(new Vector3(0, horizontalRotationSpeed * Time.deltaTime * 10, 0));
            horizontalDistRotated += horizontalRotationSpeed * Time.deltaTime * 10;
            if(horizontalDistRotated > 30)
            {
                rotatingRight = false;
            }
        } else
        {
            transform.Rotate(new Vector3(0, horizontalRotationSpeed * Time.deltaTime * -10, 0));
            horizontalDistRotated += horizontalRotationSpeed * Time.deltaTime * -10;
            if (horizontalDistRotated < -30)
            {
                rotatingRight = true;
            }
        }

        //Vertical rotation.
        if (rotatingUp)
        {
            transform.Rotate(new Vector3(verticalRotationSpeed * Time.deltaTime * 10, 0, 0));
            verticalDistRotated += verticalRotationSpeed * Time.deltaTime * 10;
            if (verticalDistRotated > 10)
            {
                rotatingUp = false;
            }
        }
        else
        {
            transform.Rotate(new Vector3(verticalRotationSpeed * Time.deltaTime * -10, 0, 0));
            verticalDistRotated += verticalRotationSpeed * Time.deltaTime * -10;
            if (verticalDistRotated < -20)
            {
                rotatingUp = true;
            }
        }
    }
}
