package dev.m1sk9.lunaticChat.paper.command.impl.lc.channel

import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import dev.m1sk9.lunaticChat.engine.command.CommandResult
import dev.m1sk9.lunaticChat.engine.exception.ChannelNoOwnerPermissionException
import dev.m1sk9.lunaticChat.engine.exception.ChannelNotFoundException
import dev.m1sk9.lunaticChat.engine.permission.LunaticChatPermissionNode
import dev.m1sk9.lunaticChat.paper.LunaticChat
import dev.m1sk9.lunaticChat.paper.chat.channel.ChannelManager
import dev.m1sk9.lunaticChat.paper.command.annotation.Permission
import dev.m1sk9.lunaticChat.paper.command.annotation.PlayerOnly
import dev.m1sk9.lunaticChat.paper.command.core.CommandContext
import dev.m1sk9.lunaticChat.paper.command.core.LunaticCommand
import dev.m1sk9.lunaticChat.paper.i18n.LanguageManager
import dev.m1sk9.lunaticChat.paper.i18n.MessageFormatter
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands

@PlayerOnly
class ChannelDeleteCommand(
    plugin: LunaticChat,
    private val channelManager: ChannelManager,
    private val languageManager: LanguageManager,
) : LunaticCommand(plugin) {
    fun buildWithPermissionCheck(): LiteralArgumentBuilder<CommandSourceStack> {
        val builder = build()
        return applyMethodPermission("build", builder)
    }

    fun buildAllWithPermissionCheck(): List<LiteralArgumentBuilder<CommandSourceStack>> =
        withAliases(buildWithPermissionCheck(), listOf("del"))

    @Permission(LunaticChatPermissionNode.ChannelDelete::class)
    fun build(): LiteralArgumentBuilder<CommandSourceStack> =
        Commands
            .literal("delete")
            .then(
                Commands
                    .argument("channelId", StringArgumentType.word())
                    .suggests { ctx, builder ->
                        val player = ctx.source.executor as? org.bukkit.entity.Player
                        if (player != null) {
                            val hasBypass =
                                player.hasPermission(
                                    LunaticChatPermissionNode.ChannelBypass.permissionNode,
                                )

                            val channels =
                                if (hasBypass) {
                                    // Show all channels if has bypass permission
                                    channelManager.getAllChannels().getOrNull() ?: emptyList()
                                } else {
                                    // Show only owned channels
                                    channelManager
                                        .getAllChannels()
                                        .getOrNull()
                                        ?.filter { it.ownerId == player.uniqueId }
                                        ?: emptyList()
                                }

                            channels.forEach { channel ->
                                builder.suggest(channel.id)
                            }
                        }
                        builder.buildFuture()
                    }.executes { ctx ->
                        val context = wrapContext(ctx)
                        checkPlayerOnly(context)?.let { return@executes handleResult(context, it) }

                        val channelId = StringArgumentType.getString(ctx, "channelId")

                        val result = execute(context, channelId)
                        handleResult(context, result)
                    },
            )

    internal fun execute(
        ctx: CommandContext,
        channelId: String,
    ): CommandResult {
        val sender = ctx.requirePlayer()
        val hasBypass = sender.hasPermission(LunaticChatPermissionNode.ChannelBypass.permissionNode)

        val result = channelManager.deleteChannel(channelId, sender.uniqueId, hasBypass)
        return result.fold(
            onSuccess = {
                CommandResult.SuccessWithMessage(
                    MessageFormatter.format(
                        languageManager.getMessage(
                            "channel.delete.success",
                            mapOf("id" to channelId),
                        ),
                    ),
                )
            },
            onFailure = { error ->
                when (error) {
                    is ChannelNotFoundException -> {
                        CommandResult.Failure(
                            MessageFormatter.formatError(
                                languageManager.getMessage(
                                    "channel.delete.notFound",
                                    mapOf("id" to channelId),
                                ),
                            ),
                        )
                    }
                    is ChannelNoOwnerPermissionException -> {
                        CommandResult.Failure(
                            MessageFormatter.formatError(
                                languageManager.getMessage("channel.delete.noPermission"),
                            ),
                        )
                    }
                    else -> {
                        CommandResult.Failure(
                            MessageFormatter.formatError(
                                languageManager.getMessage("channel.delete.error"),
                            ),
                        )
                    }
                }
            },
        )
    }

    override fun buildCommand(): LiteralArgumentBuilder<CommandSourceStack> =
        throw UnsupportedOperationException(
            "ChannelDeleteCommand should use build() method instead of buildCommand()",
        )
}
