using UnityEngine;

using ftt.Core.Managing;
using System.Collections;

public class HidingSpot : MonoBehaviour, IInteractable
{
    [SerializeField] KeyCode exitSpotInteraction;
    [SerializeField] Transform hidePos;

    bool isHidden = false;
    Collider hsCollider;

    private void Start()
    {
        hsCollider = GetComponent<Collider>();
    }

    private void Update()
    {
        if (isHidden && Input.GetKeyDown(exitSpotInteraction))
        {
            InteractWithObject();
        }
    }

    public void InteractWithObject()
    {
        if (!isHidden)
        {
            StartCoroutine(EnterHidingSpot());
            GameManager.GetManager.PlayerInteractions.ForceTurnOffTorch();
        }
        else
        {
            StartCoroutine(ExitHidingSpot());
        }
    }

    IEnumerator EnterHidingSpot()
    {
        hsCollider.isTrigger = true;

        yield return new WaitForEndOfFrame();

        GameManager.GetManager.Player.DeactivateController();
        GameManager.GetManager.Player.LerpPlayerToPosition(hidePos.position);
        GameManager.GetManager.Player.transform.rotation = transform.rotation;

        isHidden = true;
        GameManager.GetManager.IsPlayerHidden = isHidden;
    }

    IEnumerator ExitHidingSpot()
    {
        GameManager.GetManager.Player.ActivateController();
        GameManager.GetManager.Player.LerpPlayerToPosition(transform.position + transform.forward);

        yield return new WaitForEndOfFrame();

        isHidden = false;
        hsCollider.isTrigger = false;

        GameManager.GetManager.IsPlayerHidden = isHidden;
    }
}
