package pw.hamzantal.mdranks.commands

import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import pw.hamzantal.mdranks.*
import pw.hamzantal.mdranks.api.Perms
import pw.hamzantal.mdranks.api.RankType

fun onRebirth(p: CommandSender) {
	if (p !is Player) {
		p.err("You can only rebirth up as a player")
		return
	}
	val regMax = RegularConfig.max()
	if (regMax != Perms.reg(p)) {
		p.err(
			"rebirth_not_max_rankup",
			"rankup_display" to RegularConfig(regMax).display,
			"rankup" to "$regMax"
		)
		return
	}

	val prMax = PrestigeConfig.max()
	if (prMax != Perms.prestige(p)) {
		p.err(
			"rebirth_not_max_prestige",
			"prestige_display" to PrestigeConfig(prMax).display,
			"prestige" to "$prMax"
		)
		return
	}

	val currentLevel = Perms.rebirth(p)
	if (currentLevel != 0) {
		val current = RebirthConfig(currentLevel)

		if (current.isLast) {
			p.err("rebirth_max")
			return
		}
	}

	val next = RebirthConfig(currentLevel + 1)
	if (!rankUpgradePrerequisites(p, next, RankType.REBIRTH)) {
		p.err("rebirth_not_enough_money", "cost" to Config.formatC(next.cost(p)))
	}
}
