using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Assets.Scripts.Network;

public class Camera_Controller : MonoBehaviour
{
    public Camera characterCamera;
    public Vector3 defaultCameraOffset;
    public Vector3 lightHouseOffset;

    public EyesSpawner eyeSpawner;

    public Transform targetTransform;

    public float smoothSpeed = 5f;
    public float zoomTime = 1.5f;

    private Vector3 m_prevCameraPosition;
    private Vector3 m_curOffset;

    private float m_zoomTimer;

    private bool m_inZoom = false;

    private void Awake()
    {
        m_curOffset = defaultCameraOffset;
    }

    private void Start()
    {
        m_prevCameraPosition = characterCamera.transform.position;
        m_zoomTimer = zoomTime;
        SetDefaultOffset();

        eyeSpawner = GetComponentInChildren<EyesSpawner>();
        eyeSpawner.targetPlayer = targetTransform;
    }

    private void LateUpdate()
    {
        if (targetTransform != null)
        {
            characterCamera.transform.position = Vector3.Lerp(m_prevCameraPosition, targetTransform.position + m_curOffset, smoothSpeed * Time.deltaTime);

            m_prevCameraPosition = characterCamera.transform.position;

            if(m_inZoom)
            {
                if (m_zoomTimer < zoomTime)
                {
                    m_curOffset = Vector3.Lerp(lightHouseOffset, defaultCameraOffset, m_zoomTimer / zoomTime);
                    m_zoomTimer += Time.deltaTime;
                }
            }
            else
            {
                if (m_zoomTimer > 0)
                {
                    m_curOffset = Vector3.Lerp(lightHouseOffset, defaultCameraOffset, m_zoomTimer / zoomTime);
                    m_zoomTimer -= Time.deltaTime;
                }
            }
        }
    }

    public void SetDefaultOffset()
    {
        m_inZoom = true;

        if(!eyeSpawner.isAnim)
        {
            eyeSpawner.EnableAnim();
        }
    }

    public void SetLighthouseOffset()
    {
        m_inZoom = false;

        if (eyeSpawner.isAnim)
        {
            eyeSpawner.DisableAnim();
        }
    }
}
