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

public class CameraController : MonoBehaviour
{
    public float offsetX = 5, offsetY = 3, offsetZ = -16;
    public float lerpValue = 3.5f;

    [HideInInspector]
    public Vector3 targetPos;
    [HideInInspector]
    public Vector3 leftTargetPos;
    [HideInInspector]
    public Vector3 rightTargetPos;

    public Camera mainCamera;
    public Camera leftCamera;
    public Camera rightCamera;

    public float minField = 55;
    public float maxField = 75;
    [HideInInspector]
    public float field;
    public float minDistance = 20;
    public float splitDistance = 30;
    public float mergeDistance = 16;
    [HideInInspector]
    public float distance;
    [HideInInspector]
    public float ratio;

    public float lineMaxScale = 0.5f;

    public GameObject lineInCamera;


    private void Start()
    {
        ratio = (maxField - minField) / (splitDistance - minDistance);
    }
    private void Update()
    {
        if (PlayersInput.instance[0] == null)
        {
            targetPos = Vector3.zero + new Vector3(0, offsetY, offsetZ);
        }
        else if (PlayersInput.instance[1] == null)
        {
            targetPos = CameraTargetMove(PlayersInput.instance[0]);
        }
        else
        {
            Vector3 player0Pos = PlayersInput.instance[0].transform.position;
            Vector3 player1Pos = PlayersInput.instance[1].transform.position;
            distance = Vector3.Distance(player0Pos, player1Pos);

            if (player0Pos.x < player1Pos.x)
            {
                leftTargetPos = CameraTargetMove(PlayersInput.instance[0]);
                rightTargetPos = CameraTargetMove(PlayersInput.instance[1]);
            }
            else
            {
                leftTargetPos = CameraTargetMove(PlayersInput.instance[1]);
                rightTargetPos = CameraTargetMove(PlayersInput.instance[0]);

            }
            targetPos = Vector3.Lerp(leftCamera.transform.position, rightCamera.transform.position, 0.5f);
            field = Mathf.Clamp(minField + (distance - minDistance) * ratio, minField, maxField);
            if (distance < mergeDistance)
            {
                mainCamera.enabled = true;
                leftCamera.enabled = false;
                rightCamera.enabled = false;
                lineInCamera.SetActive(false);
            }
            if (distance > splitDistance)
            {
                mainCamera.enabled = false;
                leftCamera.enabled = true;
                rightCamera.enabled = true;
                lineInCamera.SetActive(true);
                lineInCamera.transform.localScale = new Vector3(Mathf.Clamp(
                    (distance - mergeDistance) * 0.01f, 0, lineMaxScale), 10, 1);
            }

        }
    }
    private void FixedUpdate()
    {
        if (PlayersInput.instance[1] == null)
        {
            mainCamera.transform.position =
                Vector3.Lerp(mainCamera.transform.position, targetPos, lerpValue * Time.deltaTime);
        }
        else
        {
            mainCamera.transform.position = targetPos;
            leftCamera.transform.position = Vector3.Lerp(leftCamera.transform.position,
                leftTargetPos, lerpValue * Time.deltaTime);
            rightCamera.transform.position = Vector3.Lerp(rightCamera.transform.position,
                rightTargetPos, lerpValue * Time.deltaTime);
            mainCamera.fieldOfView = field;

        }

    }

    Vector3 CameraTargetMove(PlayerController player)
    {
        Vector3 skewPos;
        if (player.bodyTrans.localScale.x > 0)
        {
            skewPos = player.transform.position + new Vector3(-offsetX, offsetY, offsetZ);
        }
        else
        {
            skewPos = player.transform.position + new Vector3(offsetX, offsetY, offsetZ);
        }
        return skewPos;
    }
}
