/*
 *   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.events

import de.steamwar.message.SubMessage
import de.steamwar.tntleague.config.TNTLeagueWorldConfig
import de.steamwar.tntleague.game.TNTLeagueGame
import de.steamwar.tntleague.game.TNTLeagueTeam
import de.steamwar.tntleague.message
import de.steamwar.tntleague.plugin
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.CraftItemEvent
import org.bukkit.event.player.*

object GlobalListener: Listener {

    @EventHandler(priority = EventPriority.LOW)
    fun onPlayerJoin(e: PlayerJoinEvent) {
        e.joinMessage(null)
        with(e.player) {
            teleport(TNTLeagueWorldConfig.lobby)
            inventory.clear()
            gameMode = GameMode.SPECTATOR
            respawnLocation = TNTLeagueWorldConfig.lobby
        }
    }

    @EventHandler(priority = EventPriority.HIGHEST)
    fun onPlayerQuit(e: PlayerQuitEvent) {
        e.quitMessage(null)
        TNTLeagueGame.playerLeave(e.player)

        if (plugin.server.onlinePlayers.size == 1 && plugin.server.onlinePlayers.first() == e.player) {
            plugin.server.shutdown()
        }
    }

    @EventHandler
    fun onPlayerCraft(e: CraftItemEvent) {
        e.isCancelled = true
    }

    @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.worldConfig.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.keepInventory = true

        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.worldConfig.spawnLocation
            null -> e.respawnLocation = TNTLeagueWorldConfig.lobby
        }
    }


    @EventHandler
    fun handlePlayerChat(event: AsyncPlayerChatEvent) {
        val player = event.player
        val msg = event.message

        val fightTeam = TNTLeagueGame.getTeam(player)
        if (fightTeam != null) {
            message.broadcastPrefixless("PARTICIPANT_CHAT", fightTeam.name, player.name, msg)
        } else {
            message.broadcastPrefixless("SPECTATOR_CHAT", player.name, msg)
        }

        event.isCancelled = true
    }
}