using System.Collections.Generic;
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 ContainerToOtherUnitBackpackDraggingOperation : AbstractDraggingOperation
    {
        public override bool Check(SelectUtilResult sourceSelect, SelectUtilResult targetSelect)
        {
            return sourceSelect.slotUI?.slotType == SlotType.ContainerSlot
                   && targetSelect.slotUI?.slotType == SlotType.OtherUnitBackpackSlot;
        }

        public override void Perform(UnitObject unit, SelectUtilResult sourceSelect, SelectUtilResult targetSelect)
        {
            if (sourceSelect.selectObject is not ItemObject item) return;
            var fromContainer = sourceSelect.slotUI.item.itemState.inContainer;
            var carriedBy = sourceSelect.slotUI.item.itemState.carriedBy;
            var fromIndex = sourceSelect.slotUI.slotIndex;
            var toIndex = targetSelect.slotUI.slotIndex;
            var otherUnit = GameUI.current.PickComponentUI<OtherUnitBackpackUI>().unit;
            var otherItem = otherUnit.unitBackpack.items[toIndex];
            
            if (carriedBy != null)
            {
                unit.OrderStop();
                var orderChain = new List<AbstractAction>();
                orderChain.Add(new MoveAction(unit, otherUnit, unit.unitData.takeRange));
                var dropAction = new DropAction(unit, item);
                dropAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                dropAction.OnBeforePerform += () => unit.unitBackpack.Take(item);
                orderChain.Add(dropAction);
                if (otherItem)
                {
                    var takeAction = new TakeAction(unit, otherItem, sourceSelect.slotUI.slotIndex);
                    takeAction.OnBeforePerform += () => new ForcedDropAction(otherUnit, otherItem).DoIt();
                    takeAction.OnAfterPerform += () => unit.unitBackpack.Drop(otherItem);
                    takeAction.OnAfterPerform += () => fromContainer.containerContent.MoveIn(fromIndex, otherItem, unit);
                    takeAction.OnAfterPerform += () => new ForcedTakeAction(otherUnit, item, toIndex).DoIt();
                    orderChain.Add(takeAction);
                }
                else
                {
                    dropAction.OnAfterPerform += () => new ForcedTakeAction(otherUnit, item, toIndex).DoIt();
                }
                unit.OrderChain(orderChain);
            }
            else if (carriedBy == null)
            {
                unit.OrderStop();
                var moveAction = new MoveAction(unit, sourceSelect.position, unit.unitData.takeRange);
                var orderChain = new List<AbstractAction> { moveAction };
                if (otherItem)
                {
                    var moveItemAction = new MoveItemAction(unit, item, otherUnit.transform.position);
                    moveItemAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                    moveItemAction.OnBeforePerform += () => new ForcedDropAction(otherUnit, otherItem).DoIt();
                    moveItemAction.OnAfterPerform += () => new ForcedTakeAction(otherUnit, item, toIndex).DoIt();
                    moveItemAction.OnAfterPerform += () => fromContainer.containerContent.MoveIn(fromIndex, otherItem, unit);
                    orderChain.Add(moveItemAction);
                }
                else
                {
                    var moveItemAction = new MoveItemAction(unit, item, otherUnit.transform.position);
                    moveItemAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                    moveItemAction.OnAfterPerform += () => new ForcedTakeAction(otherUnit, item, toIndex).DoIt();
                    orderChain.Add(moveItemAction);
                }
                unit.OrderChain(orderChain);
            }
        }
    }
}