using System;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    [SerializeField]
    private Camera controlCamera = null; // set from editor
    [SerializeField]
    private ExpandCamera fowController = null; // set from editor

    private Quaternion rotation = Quaternion.identity;

    public Vector3 Position {
        get => transform.position;
        set => transform.position = value;
    }
    public Vector3 Offset
    {
        get => controlCamera.transform.localPosition;
        set => controlCamera.transform.localPosition = value;
    }

    public Quaternion Rotation
    {
        get => rotation;
        set {
            rotation = value;
            transform.rotation = rotation;
        }
    }

    private void Awake()
    {
        rotation = transform.rotation;
    }

    public void SetPosition(Vector3 position) {
        transform.position = position;
    }

    public void SetFieldOfView(float FoW)
    {
        if (fowController != null) {
            fowController.SetFieldOfView(FoW);
        } else {
            controlCamera.fieldOfView = FoW;
        }
    }

    public float GetFieldOfView()
    {
        if (fowController != null)
            return fowController.FieldOfView;
        return controlCamera.fieldOfView;
    }

    public Ray GetRay(Vector3 position)
    {
        return controlCamera.ScreenPointToRay(position);
    }

    public static float RoundAngle(float angle)
    {
        while (angle < 0)
        {
            angle += 360f;
        }
        while (angle >= 360)
        {
            angle -= 360f;
        }
        return angle;
    }

    public static float ClampAngle(float angle, float minAngle, float maxAngle)
    {
        if (angle > 180)
            angle -= 360f;
        return Mathf.Clamp(angle, minAngle, maxAngle);
    }
}
