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

public class CameraController : MonoBehaviour {

    [Range(5, 100)]
    public float CloseDistance = 20.0f;
    [Range(5,100)]
    public float FarDistance = 80.0f;

    public float CloseAngle = 45.0f;
    public float FarAngle = 45.0f;

    public float xMin = -10.0f;
    public float xMax = 10.0f;
    public float zMin = -10.0f;
    public float zMax = 10.0f;

    public Vector3 CameraFocalOffset = Vector3.zero;
    public Vector3 ZoomOffset = new Vector3(80.0f, 80.0f);

    public Vector3 DesiredCameraPan = new Vector3();

    public int ZoomLevel = 0;
    public int DesiredZoomLevel = 0;

    public float CameraScrollSpeed = 1.0f;

    private void Start()
    {
        SetZoomOffsetForZoomLevel();
    }

    private void SetZoomOffsetForZoomLevel()
    {
        if (ZoomLevel == 0)
        {
            ZoomOffset = new Vector3(0.0f, FarDistance, -FarDistance);
        }
        else if (ZoomLevel == 1)
        {
            ZoomOffset = new Vector3(0.0f, CloseDistance, -CloseDistance);
            CameraFocalOffset = Vector3.zero;
            GetComponent<Minimap>().ViewportMarkerLookAt(CameraFocalOffset);
        }
    }

    // Update is called once per frame
    void Update () {
		if (ZoomLevel != DesiredZoomLevel)
        {
            ZoomLevel = DesiredZoomLevel;
            SetZoomOffsetForZoomLevel();
        }
        if (DesiredCameraPan != Vector3.zero)
        {
            CameraFocalOffset += DesiredCameraPan * Time.deltaTime * CameraScrollSpeed;
            DesiredCameraPan = Vector3.zero;
            GetComponent<Minimap>().ViewportMarkerLookAt(CameraFocalOffset);
        }

        CameraFocalOffset.x = Mathf.Clamp(CameraFocalOffset.x, xMin, xMax);
        CameraFocalOffset.z = Mathf.Clamp(CameraFocalOffset.z, zMin, zMax);

        Camera.main.transform.position = CameraFocalOffset + ZoomOffset;
	}
}
