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

        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 fromContainerCarrier = fromContainer.itemState.carriedBy;
            var fromIndex = sourceSelect.slotUI.slotIndex;
            var toContainer = GameUI.current.PickComponentUI<ContainerUI>().container;
            var toContainerCarrier = toContainer.itemState.carriedBy;
            var toIndex = targetSelect.slotUI.slotIndex;
            if (fromContainer == toContainer)
            {
                fromContainer.containerContent.Swap(fromIndex, toIndex);
            }
            else if (fromContainerCarrier == toContainerCarrier)
            {
                var otherItem = toContainer.containerContent.items[toIndex];
                fromContainer.containerContent.MoveOut(item, unit);
                if (otherItem) toContainer.containerContent.MoveOut(otherItem, unit);
                toContainer.containerContent.MoveIn(toIndex, item, unit);
                if (otherItem) fromContainer.containerContent.MoveIn(fromIndex, otherItem, unit);
            }
            else if (fromContainerCarrier == unit && toContainerCarrier == null)
            {
                unit.OrderStop();
                var moveAction = new MoveAction(unit, toContainer, unit.unitData.takeRange);
                var dropAction = new DropAction(unit, item);
                dropAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                dropAction.OnAfterPerform += () => toContainer.containerContent.MoveIn(toIndex, item, unit);
                var orderChain = new List<AbstractAction>{ moveAction, dropAction };
                var otherItem = toContainer.containerContent.items[toIndex];
                if (otherItem)
                {
                    var takeAction = new TakeAction(unit, otherItem, sourceSelect.slotUI.slotIndex);
                    takeAction.OnAfterPerform += () => unit.unitBackpack.Drop(otherItem);
                    takeAction.OnAfterPerform += () => fromContainer.containerContent.MoveIn(fromIndex, otherItem, unit);
                    orderChain.Add(takeAction);
                }
                unit.OrderChain(orderChain);
            }
            else if (fromContainerCarrier == null && toContainerCarrier == unit)
            {
                unit.OrderStop();
                var otherItem = unit.unitBackpack.items[toIndex];
                var moveAction = new MoveAction(unit, sourceSelect.position, unit.unitData.takeRange);
                var orderChain = new List<AbstractAction> { moveAction };
                if (otherItem)
                {
                    var dropAction = new DropAction(unit, otherItem);
                    dropAction.OnBeforePerform += () => toContainer.containerContent.MoveOut(otherItem, unit);
                    var takeAction = new TakeAction(unit, item);
                    takeAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                    takeAction.OnAfterPerform += () => unit.unitBackpack.Drop(item);
                    takeAction.OnAfterPerform += () => toContainer.containerContent.MoveIn(toIndex, item, unit);
                    takeAction.OnAfterPerform += () => fromContainer.containerContent.MoveIn(fromIndex, otherItem, unit);
                    orderChain.Add(dropAction);
                    orderChain.Add(takeAction);
                }
                else
                {
                    var takeAction = new TakeAction(unit, item, toIndex);
                    takeAction.OnBeforePerform += () => fromContainer.containerContent.MoveOut(item, unit);
                    takeAction.OnAfterPerform += () => unit.unitBackpack.Drop(item);
                    takeAction.OnAfterPerform += () => fromContainer.containerContent.MoveOut(item, unit);
                    orderChain.Add(takeAction);
                }
                unit.OrderChain(orderChain);
            }
            else
            {
                var fromName = fromContainer.basicData.finalName;
                var toName = toContainer.basicData.finalName;
                Debug.LogWarning($"Dragging between {fromName} -> {toName} not supported!", unit);
            }
        }
    }
}