using VOID.Generic.Objects.Container;
using VOID.Generic.Objects.Container.Events;
using VOID.Generic.Objects.Item.Events;
using VOID.Generic.Objects.Unit;

namespace VOID.Generic.Objects.Item
{
    [System.Serializable]
    public class ItemObjectState : ObjectComponent<ItemObject>
    {
        #region Initialize

        protected override void EventInitialize()
        {
            parent.itemEvents.onPickedAfter.AddListener(OnPicked);
            parent.itemEvents.onDroppedAfter.AddListener(OnDropped);
            parent.itemEvents.onMovedIntoContainer.AddListener(OnMovedIntoContainer);
            parent.itemEvents.onMovedOutOfContainer.AddListener(OnMovedOutOfContainer);
        }

        #endregion
        
        public UnitObject carriedBy { get; private set; }
        public ContainerObject inContainer { get; private set; }
        
        private void OnPicked(PickEvent pickEvent)
        {
            carriedBy = pickEvent.unit;
            parent.gameObject.SetActive(!carriedBy && !inContainer);
        }

        private void OnDropped(DropEvent dropEvent)
        {
            carriedBy = null;
            parent.gameObject.SetActive(!carriedBy && !inContainer);
        }
        
        private void OnMovedIntoContainer(ContainerMoveInEvent containerMoveInEvent)
        {
            inContainer = containerMoveInEvent.container;
            parent.gameObject.SetActive(!carriedBy && !inContainer);
        }
        
        private void OnMovedOutOfContainer(ContainerMoveOutEvent containerMoveOutEvent)
        {
            inContainer = null;
            parent.gameObject.SetActive(!carriedBy && !inContainer);
        }
    }
}
