using VOID.Generic.Dict;
using VOID.Generic.Objects.Item;
using VOID.Generic.Objects.Unit;
using VOID.Generic.Objects.Unit.Actions;
using VOID.Generic.Player.Util;
using VOID.UserInterface;
using VOID.UserInterface.Game;

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

        public override void Perform(UnitObject unit, SelectUtilResult sourceSelect, SelectUtilResult targetSelect)
        {
            if (sourceSelect.selectObject is not ItemObject item) return;
            unit.OrderStop();
            unit.Order(new MoveAction(unit, sourceSelect.position, unit.unitData.takeRange));
            var container = GameUI.current.PickComponentUI<ContainerUI>().container;
            if (container.itemState.carriedBy == unit)
            {
                var takeAction = new TakeAction(unit, item);
                takeAction.OnAfterPerform += () => unit.unitBackpack.Drop(item);
                takeAction.OnAfterPerform += () =>
                    container.containerContent.MoveIn(targetSelect.slotUI.slotIndex, item, unit);
                unit.Order(takeAction);
            }
            else
            {
                var moveAction = new MoveItemAction(unit, item, container.transform.position);
                moveAction.OnAfterPerform += () =>
                    container.containerContent.MoveIn(targetSelect.slotUI.slotIndex, item, unit);
                unit.Order(moveAction);
            }
        }
    }
}