using UnityEngine;

public class ChestLogic : MonoBehaviour
{

    private Animator animator;
    private bool isOpen = false;
    private bool isPlayerInRange = false;
    private GameObject canvas;


    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        animator = GetComponent<Animator>();
        canvas = GameObject.Find("UI Canvas");
        if (canvas == null) Debug.LogWarning("ChestUIText GameObject not found in the scene. Please ensure it exists.");

    }

    // Update is called once per frame
    void Update()
    {
        if (!isPlayerInRange)
            return;
        if (Input.GetKeyDown(KeyCode.E)) // Detekce stisku klávesy E
        {
            Debug.Log("E key pressed");
            if (!isOpen)
            {
                animator.SetTrigger("Open");
                isOpen = true;

                // Deactivate the ChestUIText when the chest is opened
                if (canvas != null)
                {
                    Transform chestTextTransform = canvas.transform.Find("ChestUIText");
                    if (chestTextTransform != null)
                    {
                        chestTextTransform.gameObject.SetActive(false);
                    }
                }
            }
            /*else
            {
                animator.SetTrigger("Close");
                isOpen = false;
            }*/
        }
    }

    /*private void OnMouseDown()
    {
        Debug.Log("Chest clicked");
        if (!isOpen)
        {
            animator.SetTrigger("Open");
            isOpen = true;
        }
        else
        {
            animator.SetTrigger("Close");
            isOpen = false;
        }
    }*/
    private void OnTriggerEnter(Collider other)
    {
        // Check if the chest is not already open
        if (!isOpen)
        {
            if (other.CompareTag("Player"))
            {
                isPlayerInRange = true;
                Debug.Log("Player entered chest range");

                // Activate the ChestUIText when the player enters the range
                if (canvas != null)
                {
                    Transform chestTextTransform = canvas.transform.Find("ChestUIText");
                    if (chestTextTransform != null)
                    {
                        chestTextTransform.gameObject.SetActive(true);
                    }
                }
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        // Check if the chest is already open
        if (isOpen)
            return;
            
        if (other.CompareTag("Player"))
        {
            isPlayerInRange = false;
            Debug.Log("Player left chest range");

            // Deaktivate the ChestUIText when the player leaves the range
            if (canvas != null)
            {
                Transform chestTextTransform = canvas.transform.Find("ChestUIText");
                if (chestTextTransform != null)
                {
                    chestTextTransform.gameObject.SetActive(false);
                }
            }
        }
    }

}
