package pw.hamzantal.mdranks

import hazae41.minecraft.kutils.bukkit.ConfigFile
import hazae41.minecraft.kutils.get

object Messages : ConfigFile(MDRanks.pl.dataFolder["messages.yml"]) {

	override var minDelay: Long
		get() = super.minDelay
		set(value) {}
	private val errorFormat by lazy { config.getString("error") }
	fun error(msg: String, vararg params: Pair<String, String>): String {
		var base = errorFormat.replace("%s", config.getString("error_msgs.$msg", msg))
		params.forEach { (k, v) -> base = base.replace("%$k%", v) }
		return base.c
	}

	private val generalFormat by lazy { config.getString("general") }
	fun general(msg: String, vararg params: Pair<String, String>): String {
		var base = generalFormat.replace("%s", config.getString("general_msgs.$msg", msg))
		params.forEach { (k, v) -> base = base.replace("%$k%", v) }
		return base.c
	}

	private val successFormat by lazy { config.getString("success") }
	fun success(msg: String, vararg params: Pair<String, String>): String {
		var base = successFormat.replace("%s", config.getString("success_msgs.$msg", msg))
		params.forEach { (k, v) -> base = base.replace("%$k%", v) }
		return base.c
	}


	init {
		if (file == null) throw IllegalStateException("File can't be null")
		val file = file!!
		if (!file.exists()) {
			val messages = MDRanks.pl.getResource("messages.yml").bufferedReader().readLines().joinToString("\n")
			file.createNewFile()
			file.writeText(messages)
		}
	}

}
