/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.tntleague.config

import de.steamwar.tntleague.plugin
import de.steamwar.kotlin.util.Area
import net.kyori.adventure.text.Component
import org.bukkit.Bukkit
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.configuration.file.YamlConfiguration
import org.bukkit.entity.WanderingTrader
import java.io.File
import kotlin.math.abs
import kotlin.properties.Delegates

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"
            )
        )
    }

    lateinit var blueTeam: TeamWorldConfig
    lateinit var redTeam: TeamWorldConfig
    lateinit var lobby: Location
    var teamsOnSameLine by Delegates.notNull<Boolean>()
    lateinit var targetMaterial: Material
    var minHeight by Delegates.notNull<Int>()
    var target by Delegates.notNull<Int>()

    init {
        try {
            blueTeam = TeamWorldConfig.fromConfig(config.getConfigurationSection("blueTeam")!!)
            redTeam = TeamWorldConfig.fromConfig(config.getConfigurationSection("redTeam")!!)
            teamsOnSameLine = abs(blueTeam.spawnLocation.blockX - redTeam.spawnLocation.blockX) < 20
            lobby = config.getWorldLocation("lobby", blueTeam.spawnLocation.clone().add(redTeam.spawnLocation).multiply(0.5))
            targetMaterial = Material.matchMaterial(config.getString("targetMaterial", "IRON_BLOCK")!!)!!
            minHeight = config.getInt("minHeight", 0)
            target = config.getInt("target", -1)
        } catch (e: NullPointerException) {
            e.printStackTrace()
            Bukkit.shutdown()
        }
    }
}

fun ConfigurationSection.getWorldLocation(s: String, default: Location? = null): Location {
    val section = getConfigurationSection(s) ?: return default!!

    val x = section.getDouble("x")
    val y = section.getDouble("y")
    val z = section.getDouble("z")
    val pitch = section.getDouble("pitch")
    val yaw = section.getDouble("yaw")

    return Location(
        plugin.server.worlds.first(),
        x, y, z,
        yaw.toFloat(), pitch.toFloat()
    )
}
