package pw.hamzantal.mdranks.api

import net.luckperms.api.LuckPerms
import net.luckperms.api.model.user.User
import org.bukkit.entity.Player
import pw.hamzantal.mdranks.*

enum class RankType(val rankName: String, val alias: List<String> = listOf(), val config: PrisonConfig) {
	REG("regular", listOf("reg", "regular", "rg"), RegularConfig),
	PRESTIGE("prestige", listOf("pr", "prestige"), PrestigeConfig),
	REBIRTH("rebirth", listOf("rb", "rebirth", "birth"), RebirthConfig);

	companion object {
		fun byArg(arg: String) = values().firstOrNull { arg in it.alias }
	}
}

object Perms {
	fun rankFor(p: Player, type: RankType): Int {
		return p.effectivePermissions
			.filter { it.permission.startsWith("mdranks.${type.rankName}.") }
			.map { (it.permission.removePrefix("mdranks.${type.rankName}.").toIntOrNull()) ?: 0 }
			.max() ?: 0
	}

	fun reg(p: Player) = rankFor(p, RankType.REG)
	fun prestige(p: Player) = rankFor(p, RankType.PRESTIGE)
	fun rebirth(p: Player) = rankFor(p, RankType.REBIRTH)

}