package pw.hamzantal.vagueenchants

import hazae41.minecraft.kutils.bukkit.msg
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import pw.hamzantal.vagueenchants.commands.*


val help = Help()
val subCommands = listOf(
    AddEnch(),
    DelEnch(),
    help,
    Info(),
    Connect(),
    AddTokens(),
    SetTokens(),
    DelTokens(),
    Upgrade(),
    Refund(),
    Repair()
)

fun baseCommand(s: CommandSender, arg: Array<String>) {
    if (arg.isEmpty()) {
        help.execute(s, arg)
        return
    }

    val (subCommandName) = arg
    val cmd = subCommands.firstOrNull { it.firstArg == subCommandName } ?: run {
        s.err("command_not_found", "command" to subCommandName)
        return
    }
    if (cmd.requirePermission && !s.hasPermission(cmd.permission)) {
        s.err("no_access")
        return
    }
    if (cmd.requirePlayer && s !is Player) {
        s.err("require_player")
        return
    }
    if (cmd.minArgs > arg.size && cmd != help) {
        s.general(cmd.usage)
        return
    }

    cmd.execute(s, arg)
}