using System.Collections;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using UnityEngine.UI;

// Handle room and scene transitions
public class TeleportHandler : MonoBehaviour
{
    PlayerController playerController;

    private void Start()
    {
        playerController = GetComponent<PlayerController>();
    }

    /// <summary>
    /// Triggers the fast travel coroutine
    /// </summary>
    public void FastTravel(Transform position, bool isPaused)
    {
        if (isPaused)
        {
            GameManager.Instance.PlayerController.TogglePause();
        }

        StartCoroutine(FastTravelCoroutine(position));
    }

    /// <summary>
    /// Triggers the room entrance coroutine, only works if the character is not controllable (keeps from double transitioning)
    /// </summary>
    public void EnterRoom(RoomDoor door)
    {
        if (playerController.GetCharacterHasControl())
            StartCoroutine(EnterRoomCoroutine(door));
    }

    /// <summary>
    /// Handle teleportation and timing for entering rooms within a scene
    /// </summary>
    IEnumerator EnterRoomCoroutine(RoomDoor door)
    {
        float fadeDuration = 0.2f; // how long to fade to/from black
        float moveDuration = 0.4f; // how long to auto walk into room from the door
        float fadeTime = 0;
        Color blackScreenColor = Color.black;

        playerController.SetCharacterControl(false);

        // Play door open animation
        if (door.requiresInteraction)
            yield return new WaitForSeconds(0.2f);

        while (fadeTime < fadeDuration)
        {
            blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
            GameManager.Instance.BlackScreen.color = blackScreenColor;
            fadeTime += Time.deltaTime;
            yield return null;
        }
        fadeTime = 0;

        playerController.CharacterControllerMove(door.linkedDoor.transform.position - transform.position);
        playerController.DougBody.transform.rotation = door.linkedDoor.transform.rotation;

        while (fadeTime < fadeDuration)
        {
            blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
            GameManager.Instance.BlackScreen.color = blackScreenColor;
            playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized);

            fadeTime += Time.deltaTime;
            yield return null;
        }
        // move character a little more
        while (fadeTime < moveDuration)
        {
            playerController.WalkInDirection((door.linkedDoor.WalkDirection.position - playerController.transform.position).normalized);
            fadeTime += Time.deltaTime;
            yield return null;
        }
        fadeTime = 0;

        // Play exit animation for linked door (if it exists)
        door.linkedDoor.CloseDoor();
        yield return new WaitForSeconds(0.2f);
        playerController.SetCharacterControl(true);
    }

    /// <summary>
    /// Handle teleportation and timing for fast travel
    /// </summary>
    IEnumerator FastTravelCoroutine(Transform location)
    {
        float fadeDuration = 0.2f; // how long to fade to/from black
        float fadeTime = 0;
        Color blackScreenColor = Color.black;

        playerController.SetCharacterControl(false);

        while (fadeTime < fadeDuration)
        {
            blackScreenColor.a = Mathf.Lerp(0, 1, fadeTime / fadeDuration);
            GameManager.Instance.BlackScreen.color = blackScreenColor;
            fadeTime += Time.deltaTime;
            yield return null;
        }
        fadeTime = 0;

        playerController.CharacterControllerSetPosition(location.position);

        while (fadeTime < fadeDuration)
        {
            blackScreenColor.a = Mathf.Lerp(1, 0, fadeTime / fadeDuration);
            GameManager.Instance.BlackScreen.color = blackScreenColor;

            fadeTime += Time.deltaTime;
            yield return null;
        }

        fadeTime = 0;

        yield return new WaitForSeconds(0.2f);
        playerController.SetCharacterControl(true);
    }

}
