use pumpkin_data::translation;
use pumpkin_util::PermissionLvl;
use pumpkin_util::permission::{Permission, PermissionDefault, PermissionRegistry};
use pumpkin_util::text::TextComponent;
use pumpkin_util::text::color::NamedColor;

use crate::command::argument_builder::{ArgumentBuilder, command};
use crate::command::context::command_context::CommandContext;
use crate::command::node::dispatcher::CommandDispatcher;
use crate::command::node::{CommandExecutor, CommandExecutorResult};
use crate::stop_server;

const DESCRIPTION: &str = "Stop the server.";

const PERMISSION: &str = "minecraft:command.stop";

struct StopCommandExecutor;

impl CommandExecutor for StopCommandExecutor {
    fn execute<'a>(&'a self, context: &'a CommandContext) -> CommandExecutorResult<'a> {
        Box::pin(async move {
            context
                .source
                .send_feedback(
                    TextComponent::translate_cross(
                        translation::java::COMMANDS_STOP_STOPPING,
                        translation::bedrock::COMMANDS_STOP_START,
                        [],
                    )
                    .color_named(NamedColor::Red),
                    true,
                )
                .await;
            stop_server();
            Ok(1)
        })
    }
}

pub fn register(dispatcher: &mut CommandDispatcher, registry: &mut PermissionRegistry) {
    registry.register_permission_or_panic(Permission::new(
        PERMISSION,
        DESCRIPTION,
        PermissionDefault::Op(PermissionLvl::Four),
    ));

    dispatcher.register(
        command("stop", DESCRIPTION)
            .requires(PERMISSION)
            .executes(StopCommandExecutor),
    );
}
