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

        public override void DoDefault(SelectUtilResult select, bool isAlternate)
        {
            if (isAlternate) return;
            
            var door = (DoorObject)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 if (activeUnit.unitData.canUse)
            {
                // Default operation is USE (+ move closer if needed)
                activeUnit.Order(new MoveAction(activeUnit, door, activeUnit.unitData.takeRange));
                activeUnit.Order(new UseAction(activeUnit, door));
            }
        }

        public override void DoContext(SelectUtilResult select)
        {
            var door = (DoorObject)select.selectObject;
            var activeUnit = PlayerController.current.activeUnit;
            var attackAbility = activeUnit.unitAbilityBook.attackAbility;

            ContextMenuUI.current.Prepare();
            
            ContextMenuUI.current.AddBeforeClick(() => activeUnit.OrderStop());
            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.canUse)
            {
                ContextMenuUI.current.AddOption(door.doorState.isOpen ? "Close" : "Open", () =>
                {
                    activeUnit.Order(new MoveAction(activeUnit, door, activeUnit.unitData.useRange));
                    activeUnit.Order(new UseAction(activeUnit, door));
                });
            }
            if (activeUnit.unitData.canInspect && door.basicData.canBeInspected) 
            {
                ContextMenuUI.current.AddOption("Inspect", () =>
                {
                    MoveAction moveAction = new MoveAction(activeUnit, door, activeUnit.unitData.inspectRange);
                    moveAction.OnEnd += () => activeUnit.OrderInstantly(new InspectAction(activeUnit, door));
                    activeUnit.Order(moveAction);
                });
            }
            ContextMenuUI.current.Open();
        }
    }
}