using Conjure.Interraction;
using UnityEngine;

namespace Conjure.Items
{
    public class Item : InterractableElement
    {
        private bool _isHolded = false;
        public override void InterractEnter(PlayerInterraction playerInterraction)
        {
            if (_isHolded)
            {
                return;
            }
            playerInterraction.SetInterractable(this);
        }

        public override void Interract(PlayerInterraction playerInterraction)
        {
            if (_isHolded)
            {
                AskToBeReleased(playerInterraction);
                return;
            }

            AskToBeGrabbed(playerInterraction);
        }
        
        public override void InterractLeave(PlayerInterraction playerInterraction)
        {
            if (_isHolded)
            {
                return;
            }
            playerInterraction.UnsetInterractable(this);
        }
        
        private void AskToBeGrabbed(PlayerInterraction playerInterraction)
        {
            bool isActionSuccess = playerInterraction.Grab(gameObject);
            if (isActionSuccess)
            {
                _isHolded = true;
            }
        }
        
        private void AskToBeReleased(PlayerInterraction playerInterraction)
        {
            bool isActionSuccess = playerInterraction.Drop(gameObject);
            if (isActionSuccess)
            {
                _isHolded = false;
            }
        }
    }
}