using System;
using VOID.Generic.Objects.Unit.Actions;
using VOID.Generic.Objects.Wearable;
using VOID.Generic.Player.Input;
using VOID.Generic.Player.Util;
using VOID.UserInterface.Game;

namespace VOID.Generic.Player.CursorOperation
{
    public class OnWearableCursorOperation : AbstractCursorOperation
    {
        public override Type ForType() => typeof(WearableObject);
        
        public override bool Check(SelectUtilResult select)
        {
            return select.selectObject is WearableObject;
        }

        public override void DoDefault(SelectUtilResult select, bool isAlternate)
        {
            if (isAlternate) return;
            
            var wearable = (WearableObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var carrier = wearable.itemState.carriedBy;
            var attackAbility = activeUnit.unitAbilityBook.attackAbility;

            activeUnit.OrderStop();
            if (activeUnit.unitData.canAttack && PlayerInputManager.current.isForceAttack)
            {
                // Player forced attack action
                activeUnit.Order(new MoveAction(activeUnit, select.position, attackAbility.range));
                activeUnit.Order(new AbilityAction(activeUnit, attackAbility, select.position));
            }
            else if (activeUnit.unitData.canTake && wearable.itemData.canBePicked && wearable.itemData.canBeMoved && (carrier == null || carrier.basicState.isDead))
            {
                // Default operaion is Take
                activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                activeUnit.Order(new TakeAction(activeUnit, wearable));
            }
            else if (wearable.wearableState.wearerUnit == activeUnit)
            {
                // Try UNEQUIP (if currently wearing)
                activeUnit.Order(new UnEquipAction(activeUnit, wearable));
            }
            else if (wearable.itemState.carriedBy == activeUnit)
            {
                // Try EQUIP (if in backpack)
                activeUnit.Order(new EquipAction(activeUnit, wearable));
            }
        }

        public override void DoContext(SelectUtilResult select)
        {
            var wearable = (WearableObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var attackAbility = activeUnit.unitAbilityBook.attackAbility;
            var carrier = wearable.itemState.carriedBy;
            var wearer = wearable.wearableState.wearerUnit;
            
            ContextMenuUI.current.Prepare();

            ContextMenuUI.current.AddBeforeClick(() => activeUnit.OrderStop());
            
            if (!select.onUI)
            {
                if (activeUnit.unitData.canMove)
                {
                    ContextMenuUI.current.AddOption("Move", () =>
                    {
                        activeUnit.Order(new MoveAction(activeUnit, select.position));
                    });
                }
                if (activeUnit.unitData.canAttack)
                {
                    ContextMenuUI.current.AddOption("Attack", () =>
                    {
                        activeUnit.Order(new MoveAction(activeUnit, select.position, attackAbility.range));
                        activeUnit.Order(new AbilityAction(activeUnit, attackAbility, select.position));
                    });
                }
            }
            if ((carrier == null) && wearer == null && activeUnit.unitData.canTake && wearable.itemData.canBePicked && wearable.itemData.canBeMoved)
            {
                ContextMenuUI.current.AddOption("Equip", () =>
                {
                    activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                    activeUnit.Order(new TakeAction(activeUnit, wearable));
                    activeUnit.Order(new EquipAction(activeUnit, wearable));
                });
            }
            if ((carrier == activeUnit) && wearer == null)
            {
                ContextMenuUI.current.AddOption("Equip", () =>
                {
                    activeUnit.Order(new EquipAction(activeUnit, wearable));
                });
            }
            if (carrier == activeUnit && wearer == activeUnit)
            {
                ContextMenuUI.current.AddOption("UnEquip", () =>
                {
                    activeUnit.Order(new UnEquipAction(activeUnit, wearable));
                });
            }
            if (activeUnit.unitData.canTake && wearable.itemData.canBePicked && wearable.itemData.canBeMoved && (carrier == null || carrier.basicState.isDead))
            {
                ContextMenuUI.current.AddOption("Take", () =>
                {
                    activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                    activeUnit.Order(new TakeAction(activeUnit, wearable));
                });
            }
            if (carrier == activeUnit)
            {
                ContextMenuUI.current.AddOption("Drop", () =>
                {
                    activeUnit.Order(new DropAction(activeUnit, wearable));
                });
            }
            if (activeUnit.unitData.canInspect && wearable.basicData.canBeInspected)
            {
                ContextMenuUI.current.AddOption("Inspect", () =>
                {
                    MoveAction moveAction = new MoveAction(activeUnit, select.position, activeUnit.unitData.inspectRange);
                    moveAction.OnEnd += () => activeUnit.OrderInstantly(new InspectAction(activeUnit, wearable));
                    activeUnit.Order(moveAction);
                });
            }
            ContextMenuUI.current.Open();
        }
    }
}