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

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

        public override void DoDefault(SelectUtilResult select, bool isAlternate)
        {
            if (isAlternate) return;
            
            var item = (ItemObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var carrier = item.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 && item.itemData.canBePicked && item.itemData.canBeMoved && (carrier == null || carrier.basicState.isDead))
            {
                // Default operation is TAKE (+ move closer if needed)
                activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                activeUnit.Order(new TakeAction(activeUnit, item));
            }
        }

        public override void DoContext(SelectUtilResult select)
        {
            var item = (ItemObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var carrier = item.itemState.carriedBy;
            var attackAbility = activeUnit.unitAbilityBook.attackAbility;

            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 (activeUnit.unitData.canTake && item.itemData.canBePicked && item.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, item));
                });
            }
            if (carrier == activeUnit)
            {
                ContextMenuUI.current.AddOption("Drop", () =>
                {
                    activeUnit.Order(new DropAction(activeUnit, item));
                });
            }
            if (activeUnit.unitData.canInspect && item.basicData.canBeInspected) 
            {
                ContextMenuUI.current.AddOption("Inspect", () =>
                {
                    MoveAction moveAction = new MoveAction(activeUnit, select.position, activeUnit.unitData.inspectRange);
                    moveAction.OnEnd += () => activeUnit.OrderInstantly(new InspectAction(activeUnit, item));
                    activeUnit.Order(moveAction);
                });
            }
            ContextMenuUI.current.Open();
        }
    }
}