package de.steamwar.tntleague.game

import de.steamwar.tntleague.config.TNTLeagueWorldConfig
import de.steamwar.tntleague.config.targetedBlocks
import de.steamwar.tntleague.plugin
import de.steamwar.tntleague.util.*
import net.kyori.adventure.sound.Sound
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.TextColor
import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.awt.Color.green

data class TNTLeagueTeam(val config: TNTLeagueWorldConfig.TeamConfig, private val team: Team) {

    var leader: Player? = null
        set(player) {
            field = player
            if (player != null) {
                with(player.inventory) {
                    clear()
                    setItem(4, readyItem())
                }
            }
        }

    val members = mutableListOf<Player>()
    val invites = mutableListOf<Player>()

    val name: String
        get() = team.name.lowercase()

    val color: TextColor
        get() = team.color

    var isReady: Boolean = false
        set(value) {
            field = value
            leader?.inventory?.setItem(4, readyItem())
            leader?.playSound(Sound.sound(org.bukkit.Sound.BLOCK_NOTE_BLOCK_PLING.key, Sound.Source.MASTER, 1f, 1f))

            plugin.server.onlinePlayers.forEach { it.sendActionBar(translate(if (value) "isReady" else "isNotReady", translate(this.name).colorByTeam(this)).let { cmp ->
                if (value) {
                    cmp.green()
                } else {
                    cmp.red()
                }
            }) }

            if (value && opposite.isReady) {
                TNTLeagueGame.checkStart()
            }
        }

    var damagedBlocks: Int = 0
        set(value) {
            field = value
            if (value >= targetedBlocks) {
                TNTLeagueGame.win(this, TNTLeagueGame.WinReason.DESTROYED)
            }
        }

    val opposite: TNTLeagueTeam
        get() = when (team) {
            Team.BLUE -> TNTLeagueGame.redTeam
            Team.RED -> TNTLeagueGame.blueTeam
        }

    fun join(player: Player): Boolean {
        members.add(player)

        with(player) {
            teleport(config.spawnLocation)
            gameMode = GameMode.ADVENTURE
            inventory.clear()
            plugin.server.broadcast(translate("joinTeam", name().colorByTeam(this@TNTLeagueTeam), translate(this@TNTLeagueTeam.name).colorByTeam(this@TNTLeagueTeam)).basic())
        }

        if (leader == null) {
            leader = player
        }

        return true
    }

    fun readyItem() = if (isReady) {
        ItemStack.of(Material.LIME_DYE).apply {
            itemMeta = itemMeta.apply {
                displayName(translate("ready").green().translate(leader!!))
            }
        }
    } else {
        ItemStack.of(Material.RED_DYE).apply {
            itemMeta = itemMeta.apply {
                displayName(translate("notReady").red().translate(leader!!))
            }
        }
    }

    fun start() = members.forEach {
        with(it) {
            gameMode = GameMode.SURVIVAL
            inventory.addItem(ItemStack.of(Material.DIAMOND_PICKAXE).apply {
                itemMeta = itemMeta.apply {
                    isUnbreakable = true
                    addEnchant(Enchantment.EFFICIENCY, 1, false)
                }
            })
        }
    }

    fun leave(player: Player) {
        if (TNTLeagueGame.state == TNTLeagueGame.GameState.RUNNING) {
            TNTLeagueGame.playerLeave(player)
        } else {
            members.remove(player)

            if (members.isEmpty()) {
                plugin.server.onlinePlayers.firstOrNull { it != player && TNTLeagueGame.getTeam(it) == null }?.run {
                    members.add(this)
                }
            }
            if (leader == player) {
                leader = members.firstOrNull()
            }
        }
    }

    fun remove(player: Player) {
        leave(player)
        with(player) {
            teleport(TNTLeagueWorldConfig.lobby)
            gameMode = GameMode.SPECTATOR
            inventory.clear()
            plugin.server.broadcast(translate("quitTeam", name().colorByTeam(this@TNTLeagueTeam), translate(this@TNTLeagueTeam.name).colorByTeam(this@TNTLeagueTeam)).basic())
        }
    }

    enum class Team(val color: TextColor) {
        BLUE(NamedTextColor.BLUE),
        RED(NamedTextColor.RED);
    }
}