/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024  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.util

import de.steamwar.scoreboard.ScoreboardCallback
import de.steamwar.tntleague.config.targetedBlocks
import de.steamwar.tntleague.game.TNTLeagueGame
import net.kyori.adventure.text.Component
import org.bukkit.entity.Player
import kotlin.collections.HashMap

private val scoreboardTitle by lazy { tntLeaguePrefix.toLegacy() }

data class TNTLeagueScoreboard(val p: Player): ScoreboardCallback {
    override fun getData(): HashMap<String, Int> {
        val lines = mutableListOf<Component>()

        lines.add(Component.space().green())

        val minutes = TNTLeagueGame.gameTimeRemaining.floorDiv(60)
        val seconds = TNTLeagueGame.gameTimeRemaining.rem(60).toString().padStart(2, '0')
        lines.add(translate("scoreboardTime", minutes.toString().yellow(), seconds.yellow()).gray())

        lines.add(Component.space().yellow())

        with(TNTLeagueGame.blueTeam) {
            lines.add(translate("scoreboardTeam", translate(name).colorByTeam(this), (targetedBlocks - damagedBlocks).toString().yellow()).gray())
        }
        with(TNTLeagueGame.redTeam) {
            lines.add(translate("scoreboardTeam", translate(name).colorByTeam(this), (targetedBlocks - damagedBlocks).toString().yellow()).gray())
        }

        lines.add(Component.space().gray())

        return lines
            .foldIndexed(HashMap()) { index, acc, component -> acc.also { it[component.translate(p).toLegacy()] = index } }
    }

    override fun getTitle(): String = scoreboardTitle
}
