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

import de.steamwar.message.Message
import de.steamwar.tntleague.command.*
import de.steamwar.tntleague.events.GlobalListener
import de.steamwar.tntleague.events.LobbyListener
import de.steamwar.tntleague.game.TNTLeagueTeam
import org.bukkit.Bukkit
import org.bukkit.plugin.java.JavaPlugin

lateinit var plugin: TNTLeague
lateinit var message: Message

class TNTLeague : JavaPlugin() {
    init {
        plugin = this
    }

    override fun onEnable() {
        message = Message("de.steamwar.tntleague.TNTLeague", classLoader)

        server.pluginManager.registerEvents(LobbyListener, this)
        server.pluginManager.registerEvents(GlobalListener, this)

        logger.info("TNTLeague enabled")

        ReadyCommand.register()
        InviteCommand.register()
        AcceptCommand.register()
        RemoveCommand.register()
        LeaveCommand.register()

        plugin.server.scheduler.runTaskLater(plugin, Runnable {
            if (server.onlinePlayers.size >= 2) return@Runnable
            Bukkit.shutdown()
        }, 20 * 60 * 5)
    }
}

fun String.colorByTeam(team: TNTLeagueTeam?) =
    when (team) {
        null -> "§7${this}"
        else -> "§${team.color}${this}"
    }
