package pw.hamzantal.vagueenchants

import com.udojava.evalex.Expression
import hazae41.minecraft.kutils.bukkit.ConfigFile
import hazae41.minecraft.kutils.bukkit.keys
import hazae41.minecraft.kutils.get
import org.bukkit.configuration.ConfigurationSection
import kotlin.reflect.KProperty

class EnchantmentInfo(
	val description: String,
	val max: Int,
	val cost: Expression,
	val others: ConfigurationSection
) {
	val cooldown by lazy { Expression(others.getString("cooldown_equation")) }
	val blocks by lazy { Expression(others.getString("destroyed_blocks_equation")) }
	val chance by lazy { Expression(others.getString("chance_equation")) }
	val command by lazy { others.getString("command") ?: "" }
	val keys by lazy {
		val list = mutableListOf<String>()
		others.getConfigurationSection("keys").keys
			.map { others.getConfigurationSection("keys.$it") }.forEach { key ->
				repeat(key.getInt("chance")) { list += key.getString("command") }
			}
		list
	}

	val minTokens by lazy { Expression(others.getString("minTokens"))}
	val maxTokens by lazy { Expression(others.getString("maxTokens"))}

	val all by lazy {
		others.getKeys(false).map { it to others.get(it).toString() }
	}
}

fun info(section: ConfigurationSection) = EnchantmentInfo(
	section.getString("description"),
	section.getInt("max"),
	Expression(section.getString("cost_equation")),
	section
)

data class TokenConfig(
	val host: String,
	val database: String,
	val table: String,
	val user: String,
	val pass: String

)

object Config : ConfigFile(VagueEnchants.pl.dataFolder["config.yml"]) {

	val refundValue by double("refund_value")

	val repairValue by double("repair_value")
	val minRepair by long("min_repair")

	private val tokenPath by Section("tokens")
	val token = TokenConfig(
		tokenPath.getString("host"),
		tokenPath.getString("database"),
		tokenPath.getString("table"),
		tokenPath.getString("username"),
		tokenPath.getString("password")
	)


	private val efficiencyPath by Section("enchantments.efficiency")
	val efficiency = info(efficiencyPath)

	private val fortunePath by Section("enchantments.fortune")
	val fortune = info(fortunePath)

	private val omniPath by Section("enchantments.omnitool")
	val omni = info(omniPath)

	private val speedPath by Section("enchantments.speed")
	val speed = info(speedPath)

	private val hastePath by Section("enchantments.haste")
	val haste = info(hastePath)

	private val explosivePath by Section("enchantments.explosive")
	val explosive = info(explosivePath)

	private val autoSellPath by Section("enchantments.autosell")
	val autoSell = info(autoSellPath)

	private val keyMinePath by Section("enchantments.keymine")
	val keyMine = info(keyMinePath)

	private val tokenMinerPath by Section("enchantments.tokenminer")
	val tokenMiner = info(tokenMinerPath)

	private val slicingPath by Section("enchantments.slicing")
	val slicing = info(slicingPath)


	class Section(private val path: String) {
		operator fun getValue(ref: Any?, prop: KProperty<*>): ConfigurationSection =
			config.getConfigurationSection(path)

		operator fun setValue(ref: Any?, prop: KProperty<*>, value: ConfigurationSection) = set(path, value)
	}
}