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

public class CameraMovement : MonoBehaviour
{

    public LevelManager Levelmanager;

    public double minSize = 5.0;
    public double defaultSize = 10.0;
    public double maxSize = 50.0;

    Camera thecamera;
    double aspect;
    double size;
    //public double playergamma;
    // Start is called before the first frame update
    void Start()
    {
        //playergamma = 1.0f;
        thecamera = GetComponent<Camera>();
        aspect = 16.0 / 9.0;
        size = defaultSize;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.position = Levelmanager.player.transform.position;

        
        var v = Levelmanager.player.v;
        var vt = v + (float)Constants.c * Vector3.up;

        vt.x = -vt.x;

        vt.z = -vt.z;

        vt.Normalize();

        Vector3 newforward = new Vector3(vt.y * (v.x / v.magnitude), Mathf.Sqrt(vt.x * vt.x + vt.z * vt.z), vt.y * (v.z / v.magnitude));

        transform.rotation = Quaternion.LookRotation(-vt, newforward);

        thecamera.orthographicSize = (float)(size * (Levelmanager.player.gamma)); // number is size of camera

        aspect = 16.0f / (9.0f * (Levelmanager.player.gamma));

        thecamera.aspect = (float)aspect;

        transform.Translate(-Vector3.forward * 50); // number is distance of camera from player

    }

    public double Size
    {
        get
        {
            return size;
        }
        set
        {
            if(value < minSize)
            {
                size = minSize;
            }
            else if (value > maxSize)
            {
                size = maxSize;
            }
            else
            {
                size = value;
            }
        }
    }

    /// <summary>
    /// size/defaultSize
    /// </summary>
    public float SizeScale
    {
        get
        {
            return (float)size / (float)defaultSize;
        }
    }
}