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

import de.steamwar.scoreboard.ScoreboardCallback
import de.steamwar.tntleague.config.targetedBlocks
import de.steamwar.tntleague.game.TNTLeagueGame
import de.steamwar.tntleague.game.TNTLeagueTeam
import de.steamwar.tntleague.message
import org.bukkit.entity.Player

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

        lines.add("§1")

        val minutes = TNTLeagueGame.gameTimeRemaining.floorDiv(60)
        val seconds = TNTLeagueGame.gameTimeRemaining.rem(60).toString().padStart(2, '0')
        lines.add(message.parse("SCOREBOARD_TIME", p, minutes, seconds))

        lines.add("§2")

        when (val team = TNTLeagueGame.getTeam(p)) {
            is TNTLeagueTeam -> lines.add(message.parse("SCOREBOARD_COINS", p, team.coins))
            else -> lines.add(
                message.parse(
                    "SCOREBOARD_COINS", p,
                    "§${TNTLeagueGame.blueTeam.color}${TNTLeagueGame.blueTeam.coins}§8:§${TNTLeagueGame.redTeam.color}${TNTLeagueGame.redTeam.coins}"
                )
            )
        }

        lines.add("§3")

        with(TNTLeagueGame.redTeam) {
            lines.add(message.parse("SCOREBOARD_TEAM", p, name, targetedBlocks - damagedBlocks))
        }
        with(TNTLeagueGame.blueTeam) {
            lines.add(message.parse("SCOREBOARD_TEAM", p, name, targetedBlocks - damagedBlocks))
        }

        lines.add("§4")

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

    override fun getTitle(): String =
        message.parse("PREFIX", p).dropLast(1)
}
