package de.steamwar.tntleague.config

import de.steamwar.tntleague.plugin
import de.steamwar.tntleague.util.Area
import de.steamwar.tntleague.util.translate
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.Villager
import org.bukkit.entity.WanderingTrader
import java.io.File

val world by lazy { plugin.server.worlds.first()!! }

private val targetedBlocksRed by lazy { TNTLeagueWorldConfig.redTeam.target.blocks.count { block -> block.type == TNTLeagueWorldConfig.targetMaterial } }

private val targetedBlocksBlue by lazy { TNTLeagueWorldConfig.blueTeam.target.blocks.count { block -> block.type == TNTLeagueWorldConfig.targetMaterial } }

private val targetedBlocksAll: Int
    get() = if (targetedBlocksBlue == targetedBlocksRed) targetedBlocksBlue else error("Targeted blocks are not equal")

val targetedBlocks: Int
    get() = if (TNTLeagueWorldConfig.target != -1) TNTLeagueWorldConfig.target else targetedBlocksAll

object TNTLeagueWorldConfig {
    private val config: YamlConfiguration by lazy {
        YamlConfiguration.loadConfiguration(
            File(
                plugin.server.worlds.first().worldFolder,
                "tntleague.yml"
            )
        )
    }

    val blueTeam: TeamConfig = TeamConfig.fromConfig(config.getConfigurationSection("blueTeam")!!)
    val redTeam: TeamConfig = TeamConfig.fromConfig(config.getConfigurationSection("redTeam")!!)
    val lobby: Location = config.getLocation("lobby", blueTeam.spawnLocation.clone().add(redTeam.spawnLocation).multiply(0.5))!!
    val targetMaterial: Material = Material.matchMaterial(config.getString("targetMaterial", "IRON_BLOCK")!!)!!
    val minHeight: Int = config.getInt("minHeight", 0)
    val target: Int = config.getInt("target", -1)

    @JvmRecord
    data class TeamConfig(
        val spawnLocation: Location,
        val dealerSpawn: Location,
        val itemSpawn: Location,
        val target: Area
    ) {
        companion object {
            fun fromConfig(config: ConfigurationSection): TeamConfig {
                val spawnLocation = config.getLocation("spawn")!!
                val dealerSpawn = config.getLocation("dealerSpawn")!!
                val itemSpawn = config.getLocation("itemSpawn")!!
                val targetPos1 = config.getLocation("targetMin")!!
                val targetPos2 = config.getLocation("targetMax")!!

                spawnDealer(dealerSpawn)

                return TeamConfig(spawnLocation, dealerSpawn, itemSpawn, Area(targetPos1, targetPos2))
            }

            private fun spawnDealer(loc: Location) = world.spawn(loc, WanderingTrader::class.java)
                .apply {
                    customName(translate("dealer"))
                    isCustomNameVisible = false
                    isInvulnerable = true
                    isSilent = true
                    isCollidable = false
                    isAware = false
                    setAI(false)
                }
        }
    }
}
