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

public class CameraTransition : MonoBehaviour
{
    [SerializeField] private Camera _cinematicCamera;
    [SerializeField] private Camera _astronautCamera;
    [SerializeField] private Camera _secretCamera;
    [SerializeField] private Transform _endPoint;
    [SerializeField] private float _duration = 2f;
    [SerializeField] private GameObject _astronautCanvas;
    [SerializeField] private GameObject _planetCanvas;
    [SerializeField] private GameObject _secretCanvas;
    [SerializeField] private UnityEngine.UI.Button _secretButton;
    [SerializeField] private AstronautController _astronautController;
    [SerializeField] private GameObject _secretPlanet;
    [SerializeField] private GameObject _mainPlanet;

    private Vector3 _startPos;
    private Quaternion _startRot;

    void Start()
    {
        _startPos = _cinematicCamera.transform.position;
        _startRot = _cinematicCamera.transform.rotation;

        _cinematicCamera.gameObject.SetActive(true);
        _astronautCamera.gameObject.SetActive(false);
        _secretCamera.gameObject.SetActive(false);
        _astronautController.enabled = false;
        _astronautCanvas.SetActive(false);
        _secretCanvas.SetActive(false);
        _planetCanvas.SetActive(true);
        _secretButton.gameObject.SetActive(false);
        _secretPlanet.SetActive(false);
    }

    void Update()
    {
        if (_astronautCanvas.activeSelf && Input.GetKeyDown(KeyCode.LeftControl))
        {
            _secretButton.gameObject.SetActive(true);
        }
    }

    public void StartTransition()
    {
        _planetCanvas.SetActive(false);
        StartCoroutine(FlyToAstronaut());
    }

    public void ReturnToPlanets()
    {
        _astronautCanvas.SetActive(false);
        _astronautController.enabled = false;
        _astronautCamera.gameObject.SetActive(false);
        _cinematicCamera.gameObject.SetActive(true);
        StartCoroutine(FlyBack());
    }

    public void GoToSecret()
    {
        _astronautCanvas.SetActive(false);
        _astronautController.enabled = false;
        _astronautCamera.gameObject.SetActive(false);
        _cinematicCamera.gameObject.SetActive(true);
        StartCoroutine(FlyToSecret());
    }

    public void ReturnFromSecret()
    {
        _secretCanvas.SetActive(false);
        _secretCamera.gameObject.SetActive(false);
        _cinematicCamera.gameObject.SetActive(true);
        StartCoroutine(FlyFromSecret());
    }

    IEnumerator FlyToAstronaut()
    {
        float t = 0f;
        Vector3 startPos = _cinematicCamera.transform.position;
        Quaternion startRot = _cinematicCamera.transform.rotation;

        while (t < 1f)
        {
            t += Time.deltaTime / _duration;
            _cinematicCamera.transform.position = Vector3.Lerp(startPos, _endPoint.position, Mathf.SmoothStep(0f, 1f, t));
            _cinematicCamera.transform.rotation = Quaternion.Lerp(startRot, _endPoint.rotation, Mathf.SmoothStep(0f, 1f, t));
            yield return null;
        }

        _cinematicCamera.gameObject.SetActive(false);
        _astronautCamera.gameObject.SetActive(true);
        _astronautCanvas.SetActive(true);
        _astronautController.enabled = true;
    }

    IEnumerator FlyBack()
    {
        float t = 0f;
        Vector3 startPos = _cinematicCamera.transform.position;
        Quaternion startRot = _cinematicCamera.transform.rotation;

        while (t < 1f)
        {
            t += Time.deltaTime / _duration;
            _cinematicCamera.transform.position = Vector3.Lerp(startPos, _startPos, Mathf.SmoothStep(0f, 1f, t));
            _cinematicCamera.transform.rotation = Quaternion.Lerp(startRot, _startRot, Mathf.SmoothStep(0f, 1f, t));
            yield return null;
        }

        _planetCanvas.SetActive(true);
    }

    IEnumerator FlyToSecret()
    {
        float t = 0f;
        Vector3 startPos = _cinematicCamera.transform.position;
        Quaternion startRot = _cinematicCamera.transform.rotation;
        Vector3 secretPos = _secretCamera.transform.position;
        Quaternion secretRot = _secretCamera.transform.rotation;

        while (t < 1f)
        {
            t += Time.deltaTime / _duration;
            _cinematicCamera.transform.position = Vector3.Lerp(startPos, secretPos, Mathf.SmoothStep(0f, 1f, t));
            _cinematicCamera.transform.rotation = Quaternion.Lerp(startRot, secretRot, Mathf.SmoothStep(0f, 1f, t));
            yield return null;
        }

        _cinematicCamera.gameObject.SetActive(false);
        _secretCamera.gameObject.SetActive(true);
        _secretCanvas.SetActive(true);
        _secretPlanet.SetActive(true);
        _mainPlanet.SetActive(false);
    }

    IEnumerator FlyFromSecret()
    {
        float t = 0f;
        Vector3 startPos = _cinematicCamera.transform.position;
        Quaternion startRot = _cinematicCamera.transform.rotation;
        Vector3 targetPos = _astronautCamera.transform.position;
        Quaternion targetRot = _astronautCamera.transform.rotation;

        while (t < 1f)
        {
            t += Time.deltaTime / _duration;
            _cinematicCamera.transform.position = Vector3.Lerp(startPos, targetPos, Mathf.SmoothStep(0f, 1f, t));
            _cinematicCamera.transform.rotation = Quaternion.Lerp(startRot, targetRot, Mathf.SmoothStep(0f, 1f, t));
            yield return null;
        }

        _cinematicCamera.gameObject.SetActive(false);
        _astronautCamera.gameObject.SetActive(true);
        _astronautController.enabled = true;
        _astronautCanvas.SetActive(true);
        _mainPlanet.SetActive(true);
        _secretPlanet.SetActive(false);
    }
}
