using System;
using VOID.Generic.Objects.Container;
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 OnContainerCursorOperation : AbstractCursorOperation
    {
        public override Type ForType() => typeof(ContainerObject);
        
        public override bool Check(SelectUtilResult select)
        {
            return select.selectObject is ContainerObject;
        }

        public override void DoDefault(SelectUtilResult select, bool isAlternate)
        {
            if (isAlternate) return;
            
            var container = (ContainerObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            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
            {
                // Default operation is OPEN (+ move closer if needed)
                activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                activeUnit.Order(new OpenContainerAction(activeUnit, container));
            }
        }

        public override void DoContext(SelectUtilResult select)
        {
            var container = (ContainerObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var attackAbility = activeUnit.unitAbilityBook.attackAbility;
            var carrier = container.itemState.carriedBy;
            
            ContextMenuUI.current.Prepare();
            
            ContextMenuUI.current.AddBeforeClick(() => activeUnit.OrderStop());
            
            ContextMenuUI.current.AddOption("Open", () =>
            {
                activeUnit.Order(new MoveAction(activeUnit, select.position, activeUnit.unitData.takeRange));
                activeUnit.Order(new OpenContainerAction(activeUnit, container));
            });
            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 && container.itemData.canBePicked && container.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, container));
                });
            }
            if (carrier == activeUnit)
            {
                ContextMenuUI.current.AddOption("Drop", () =>
                {
                    activeUnit.Order(new DropAction(activeUnit, container));
                });
            }
            if (activeUnit.unitData.canInspect && container.basicData.canBeInspected) 
            {
                ContextMenuUI.current.AddOption("Inspect", () =>
                {
                    MoveAction moveAction = new MoveAction(activeUnit, select.position, activeUnit.unitData.inspectRange);
                    moveAction.OnEnd += () => activeUnit.OrderInstantly(new InspectAction(activeUnit, container));
                    activeUnit.Order(moveAction);
                });
            }
            ContextMenuUI.current.Open();
        }
    }
}