using UnityEngine;
using VOID.Generic.Dict;
using VOID.Generic.Objects.Item;
using VOID.Generic.Objects.Item.Events;
using VOID.Generic.Objects.Unit.Actions.Exceptions;
using VOID.Generic.Objects.Wearable;
using VOID.Generic.Objects.Wearable.Events;
using VOID.Generic.Util;

namespace VOID.Generic.Objects.Unit.Actions
{
    public class DropAction : AbstractAction
    {
        private readonly UnitObject _unit;
        private readonly ItemObject _item;
        private readonly Vector3 _dropPosition;

        public DropAction(UnitObject unit, ItemObject item)
        {
            _unit = unit;
            _item = item;
            _dropPosition = unit.transform.position;
        }

        public DropAction(UnitObject unit, ItemObject item, Vector3 dropPosition)
        {
            _unit = unit;
            _item = item;
            _dropPosition = dropPosition;
        }

        public override bool IsInRange()
        {
            return RangeUtil.IsInRange(_unit, _item, _unit.unitData.takeRange);
        }

        protected override void OnEndIt() {}
        protected override void OnCancelIt() {}

        public override void CanDoIt()
        {
            Check(
                state == ActionState.Ready,
                UnitActionErrorType.ActionStateIsNotReady);
            if (_unit.basicState.turnManager)
            {
                Check(
                    _unit.unitState.isSelfTurn,
                    UnitActionErrorType.UnitSelfTurnNotActive);
                Check(
                    _unit.unitData.currentResources.actionPoints >= 1,
                    UnitActionErrorType.UnitNotEnoughActionPoints);
            }
            Check(
                _unit.unitState.isControllable,
                UnitActionErrorType.UnitStateIsNotControllable);
            Check(
                _item.itemState.carriedBy == _unit,
                UnitActionErrorType.UnitDontCarryThatItem);
            Check(
                IsInRange(),
                UnitActionErrorType.NotInRange);
        }

        protected override void OnDoIt()
        {
            if (_unit.basicState.turnManager)
                _unit.unitData.UseResource(UnitResourceType.ActionPoints, 1);

            TryToUnEquip();

            var dropEvent = new DropEvent
            {
                item = _item,
                unit = _unit
            };
            
            _item.itemEvents.onDroppedBefore.Invoke(dropEvent);
            _unit.unitEvents.onDropBefore.Invoke(dropEvent);

            Check(!dropEvent.prevented, UnitActionErrorType.DropIsPrevented);
                
            _unit.OrderInstantly(new RotateAction(_unit, _dropPosition));
            BeforePerform();
            _unit.unitBackpack.Drop(_item);
            _item.transform.position = _dropPosition;
            _item.itemEvents.onDroppedAfter.Invoke(dropEvent);
            _unit.unitEvents.onDropAfter.Invoke(dropEvent);
            AfterPerform();
            EndIt();
        }

        private void TryToUnEquip()
        {
            if (_item is not WearableObject item) return;
            if (!item.wearableState.wearerUnit) return;

            var unEquipEvent = new UnEquipEvent
            {
                wearable = item,
                unit = _unit
            };
            
            item.wearableEvents.onUnEquippedBefore.Invoke(unEquipEvent);
            _unit.unitEvents.onUnEquipBefore.Invoke(unEquipEvent);

            Check(!unEquipEvent.prevented, UnitActionErrorType.UnEquipIsPrevented); 
                
            _unit.unitEquipment.UnEquip(item);
            _unit.unitBackpack.Take(item); 
            
            item.wearableEvents.onUnEquippedAfter.Invoke(unEquipEvent);
            _unit.unitEvents.onUnEquipAfter.Invoke(unEquipEvent);
        }
    }
}