package de.steamwar.tntleague.events

import de.steamwar.tntleague.config.TNTLeagueWorldConfig
import de.steamwar.tntleague.game.TNTLeagueGame
import de.steamwar.tntleague.game.TNTLeagueTeam
import de.steamwar.tntleague.inventory.SWInventoryHolder
import de.steamwar.tntleague.plugin
import de.steamwar.tntleague.util.*
import io.papermc.paper.event.player.AsyncChatEvent
import org.bukkit.GameMode
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerMoveEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.event.player.PlayerRespawnEvent

object GlobalListener: Listener {

    @EventHandler(priority = EventPriority.LOW)
    fun onPlayerJoin(e: PlayerJoinEvent) {
        e.joinMessage(null)
        with(e.player) {
            teleport(TNTLeagueWorldConfig.lobby)
            inventory.clear()
            plugin.server.broadcast(translate("join", name.bold()).basic())
            isOp = false
            gameMode = GameMode.SPECTATOR
            respawnLocation = TNTLeagueWorldConfig.lobby
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    fun onPlayerQuit(e: PlayerQuitEvent) {
        e.quitMessage(null)
        plugin.server.broadcast(translate("quit", e.player.name.bold().colorByTeam(TNTLeagueGame.getTeam(e.player))).basic())
        TNTLeagueGame.playerLeave(e.player)
    }

    @EventHandler(priority = EventPriority.LOWEST)
    fun onPlayerClick(e: InventoryClickEvent) {
        val holder = e.inventory.getHolder(false)
        if (holder is SWInventoryHolder && e.clickedInventory == holder._inventory) {
            e.isCancelled = true
            holder.handleInventoryClick(e)
        }
    }

    @EventHandler(priority = EventPriority.LOWEST)
    fun onPlayerMove(e: PlayerMoveEvent) {
        if (e.to.blockY < TNTLeagueWorldConfig.minHeight) {
            when (val team = TNTLeagueGame.getTeam(e.player)) {
                is TNTLeagueTeam -> e.player.teleport(team.config.spawnLocation)
                null -> e.player.teleport(TNTLeagueWorldConfig.blueTeam.spawnLocation)
            }
        }

        e.player.foodLevel = 20
        e.player.saturation = 20f
    }

    @EventHandler
    fun onPlayerDeath(e: PlayerDeathEvent) {
        e.deathMessage(null)
        e.drops.clear()

        e.itemsToKeep.removeIf { it.type != Material.DIAMOND_PICKAXE }
    }

    @EventHandler
    fun onPlayerRespawn(e: PlayerRespawnEvent) {
        when (val team = TNTLeagueGame.getTeam(e.player)) {
            is TNTLeagueTeam -> e.respawnLocation = team.config.spawnLocation
            null -> e.respawnLocation = TNTLeagueWorldConfig.lobby
        }
    }

    @EventHandler
    fun onChat(e: AsyncChatEvent) {
        e.renderer { source, sourceDisplayName, message, _ ->
            translate("chat", sourceDisplayName.colorByTeam(TNTLeagueGame.getTeam(source)), message).basic()
        }
    }
}