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

public class CameraScript : MonoBehaviour
{
    //Public


    //Private
    private Transform cameraTransform;
    private Transform focus = null;
    private bool newfocus = false;
    [SerializeField] private float focusTime = 0.5f;

    [SerializeField] private float cameraMoveSpeed = 10;
    [Range(0.01f, 0.4f)][SerializeField] private float edgeAreaPercentage = 0.2f;
    private int screenWidth = Screen.width;
    private int screenHeight = Screen.height;
    private int edgePixels;

    [SerializeField] float startZoom = 12.5f;
    [SerializeField] float maxZoom = 20;
    [SerializeField] float minZoom = 5;
    [SerializeField] float zoomSpeed = 5;


    //Methods
    public void SetFocus(Transform t)
    {
        focus = t;
        newfocus = true;
    }

    private void Awake()
    {
        edgePixels = (int)(screenHeight * edgeAreaPercentage);

        cameraTransform = transform.GetChild(0).GetComponent<Transform>();
        if (cameraTransform == null) {Debug.LogError("NO CAMERATRANSFORM");}
        else
        {
            cameraTransform.localPosition = new Vector3(cameraTransform.localPosition.x, startZoom, cameraTransform.localPosition.z);
            cameraTransform.LookAt(transform.position);
        }
        
        
        //focus = null; //Set focus to first controlable dude.
        //newfocus = true;
    }

    private void Update()
    {
        Focus();
        MoveCamera();
        Zoom();
    }

    private void Focus()
    {
        if (newfocus)
        {
            Mathf.Lerp(transform.position.x, focus.position.x, focusTime);
            Mathf.Lerp(transform.position.z, focus.position.z, focusTime);

            if (transform.position.x == focus.position.x && transform.position.z == focus.position.z)
            {
                newfocus = false;
            }
        }
    }

    private void MoveCamera()
    {
        if (newfocus)
            return;

        Vector3 movement = Vector3.zero;

        //Move Right
        if (Input.mousePosition.x > screenWidth - edgePixels)
            movement.x += 1;
        //Move Left
        else if (Input.mousePosition.x < edgePixels)
            movement.x -= 1;

        //Move Up
        if (Input.mousePosition.y > screenHeight - edgePixels)
            movement.z += 1;
        //Move Down
        else if (Input.mousePosition.y < edgePixels)
            movement.z -= 1;

        movement.Normalize();
        transform.position += movement * cameraMoveSpeed * Time.deltaTime;
    }

    private void Zoom()
    {
        float delta = Input.mouseScrollDelta.y;
        if (delta == 0)
            return;

        float zoom = 0;

        //Zoom In
        if (delta > 0)
           zoom = cameraTransform.localPosition.y - zoomSpeed * Time.deltaTime;
        
        //Zoom Out
        else if (delta < 0)
            zoom = cameraTransform.localPosition.y + zoomSpeed * Time.deltaTime;

        //Clamp
        if (zoom < minZoom)
            zoom = minZoom;
        else if (zoom > maxZoom)
            zoom = maxZoom;

        //Set camera y
        cameraTransform.localPosition = new Vector3(cameraTransform.localPosition.x, zoom, cameraTransform.localPosition.z);

        cameraTransform.LookAt(transform.position);
    }

    private void Rotate()
    {

    }
}
