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

public class CameraLook : MonoBehaviour {
    
    public AnimationCurve resistanceDropOffCurveHorizontal;
    public AnimationCurve resistanceDropOffCurveVertical;

    public float horizontalAngleBoundsLimit;
    public float verticalAngleBounds;
    #region Variables

    public float rotateSpeed;

	#endregion
    
	#region Unity Methods
	
	
	void LateUpdate () 
	{
        float h = Input.GetAxis("Horizontal");
        float currentY = transform.rotation.eulerAngles.y;

        float modifiedHorzontal = rotateSpeed * h;
        bool shouldRotate = true;
        if (currentY > 180)
            currentY -= 360;

        //Horizontal
        if (h == 0 && Mathf.Abs(currentY) < 2f)
        {
            if (currentY != 0)
            {
                transform.Rotate(0, -currentY, 0);
            }
            shouldRotate = false;
        }
        if (currentY > 0)
        {
            if (h > 0)
            {
                if (currentY < horizontalAngleBoundsLimit)
                {
                    modifiedHorzontal = resistanceDropOffCurveHorizontal.Evaluate(horizontalAngleBoundsLimit / currentY) * rotateSpeed;
                }
                else
                {
                    shouldRotate = false;
                }
            }
            else
            {
                modifiedHorzontal = ( resistanceDropOffCurveHorizontal.Evaluate(horizontalAngleBoundsLimit / currentY) ) * -rotateSpeed;
            }
        }
        else if (currentY < 0)
        {
            if (h < 0)
            {
                if (currentY > -horizontalAngleBoundsLimit)
                {
                    modifiedHorzontal = resistanceDropOffCurveHorizontal.Evaluate(horizontalAngleBoundsLimit / Mathf.Abs(currentY)) *  -rotateSpeed;
                }
                else
                {
                    shouldRotate = false;
                }
            }
            else
            {
                modifiedHorzontal = ( resistanceDropOffCurveHorizontal.Evaluate(horizontalAngleBoundsLimit / Mathf.Abs(currentY)) ) * rotateSpeed;
            }
        }

        if (shouldRotate)
        {
            
                transform.Rotate(0, modifiedHorzontal, 0);
            
        }


        float v = Input.GetAxis("Vertical");

        float currentX = transform.rotation.eulerAngles.x;

        float modifiedVertical = rotateSpeed * v;
        bool shouldRotateV = true;

        currentX -= 15f;
        if (currentX > 180)
            currentX -= 360;

        //Vertical
        if (v == 0 && Mathf.Abs(currentX) < 2f)
        {
            if (currentX != 0)
            {
                transform.Rotate(-currentX, 0, 0);
            }
            shouldRotateV = false;
        }
        if (currentX > 0)
        {
            if (v < 0)
            {
                if (currentX < verticalAngleBounds)
                {
                    modifiedVertical = resistanceDropOffCurveVertical.Evaluate(verticalAngleBounds / currentX) * rotateSpeed;
                }
                else
                {
                    shouldRotateV = false;
                }
            }
            else
            {
                modifiedVertical = ( resistanceDropOffCurveVertical.Evaluate(verticalAngleBounds / currentX) ) * -rotateSpeed;
            }
        }
        else if (currentX < 0)
        {
            if (v > 0)
            {
                if (currentX > -verticalAngleBounds)
                {
                    modifiedVertical = resistanceDropOffCurveVertical.Evaluate(verticalAngleBounds / Mathf.Abs(currentX)) * -rotateSpeed;
                }
                else
                {
                    shouldRotateV = false;
                }
            }
            else
            {
                modifiedVertical = ( resistanceDropOffCurveVertical.Evaluate(verticalAngleBounds / Mathf.Abs(currentX)) ) * rotateSpeed;
            }
        }

        if (shouldRotateV)
        {
            transform.Rotate(modifiedVertical, 0, 0);
        }

        float currentZ = transform.rotation.eulerAngles.z;
        if (currentZ != 0)
        {
            transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
        }

    }

	#endregion
}
