using VOID.Generic.Dict;
using VOID.Generic.Objects.Unit;
using VOID.Generic.Objects.Unit.Actions;
using VOID.Generic.Objects.Wearable;
using VOID.Generic.Player.Util;

namespace VOID.Generic.Player.DraggingOperation
{
    public class ContainerToEquipmentDraggingOperation : AbstractDraggingOperation
    {
        public override bool Check(SelectUtilResult sourceSelect, SelectUtilResult targetSelect)
        {
            return sourceSelect.slotUI?.slotType == SlotType.ContainerSlot
                   && targetSelect.slotUI?.slotType == SlotType.EquipmentSlot;
        }

        public override void Perform(UnitObject unit, SelectUtilResult sourceSelect, SelectUtilResult targetSelect)
        {
            if (sourceSelect.selectObject is not WearableObject wearable) return;
            unit.OrderStop();
            var container = sourceSelect.slotUI.item.itemState.inContainer;
            if (container.itemState.carriedBy == unit)
            {
                var equipAction = new EquipAction(unit, wearable);
                equipAction.OnBeforePerform += () => container.containerContent.MoveOut(wearable, unit);
                unit.Order(equipAction);
            }
            else
            {
                unit.Order(new MoveAction(unit, sourceSelect.position, unit.unitData.takeRange));
                var takeAction = new TakeAction(unit, wearable);
                takeAction.OnBeforePerform += () => container.containerContent.MoveOut(wearable, unit);
                var equipAction = new EquipAction(unit, wearable);
                equipAction.runAfterAction = takeAction;
                unit.Order(takeAction);
                unit.Order(equipAction);
            }
        }
    }
}