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

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