package pw.hamzantal.mdranks

import hazae41.minecraft.kutils.bukkit.msg
import me.clip.placeholderapi.PlaceholderAPI
import org.bukkit.Bukkit
import org.bukkit.command.CommandSender
import org.bukkit.entity.Player
import pw.hamzantal.mdranks.api.RankType
import pw.hamzantal.mdranks.api.Vault

fun CommandSender.send(message: String) {
	msg(message)
}

val isPapi by lazy { Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null }

fun String.placeholders(p: Player, data: RankData, cmdPrefix: Boolean = false): String {
	val baseReplacement = replace("%player%", p.name)
		.replace("%rankup%", "${data.level}")
		.replace("%display%", data.display)
		.run {
			if (cmdPrefix) replaceFirst("[console] ", "")
				.replaceFirst("[op] ", "")
				.replaceFirst("[player] ", "")
			else this
		}
	return if (isPapi) PlaceholderAPI.setPlaceholders(p, baseReplacement) else baseReplacement
}

fun CommandSender.err(msg: String, vararg placeholder: Pair<String, String>) {
	msg(Messages.error(msg, *placeholder))
}

fun CommandSender.general(msg: String, vararg placeholder: Pair<String, String>) {
	msg(Messages.general(msg, *placeholder))
}

fun CommandSender.success(msg: String, vararg placeholder: Pair<String, String>) {
	msg(Messages.success(msg, *placeholder))
}
