/*
 *   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.misslewars;

import de.steamwar.linkage.Linked;
import de.steamwar.scoreboard.SWScoreboard;
import de.steamwar.scoreboard.ScoreboardCallback;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scoreboard.Scoreboard;

import java.util.HashMap;
import java.util.Objects;

/**
 * Modified Version of the Fight-System Scoreboard
 */
@Linked
public class FightScoreboard implements Listener {

    public FightScoreboard() {
        Bukkit.getPluginManager().registerEvents(this, MissileWars.getPlugin());
    }

    private static final Scoreboard scoreboard = Objects.requireNonNull(Bukkit.getScoreboardManager()).getMainScoreboard();

    private static long startTime = 0;

    public static long getStartTime() {
        return startTime;
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        SWScoreboard.impl.createScoreboard(event.getPlayer(), new ScoreboardCallback() {
            @Override
            public String getTitle() {
                return "§eMissileWars";
            }

            @Override
            public HashMap<String, Integer> getData() {
                HashMap<String, Integer> data = new HashMap<>();
                data.put("§eSpielzeit", 5);
                if (startTime == 0) {
                    data.put("§7??:??:??", 4);
                } else {
                    long current_time = System.currentTimeMillis() - startTime;
                    data.put(String.format("§7%02d:%02d", current_time / 60000, (current_time / 1000) % 60), 4);
                }

                data.put("", 3);
                data.put("§eSpieler", 2);
                MissileWars.getBlueTeam().teamScoreboard(data);
                MissileWars.getRedTeam().teamScoreboard(data);
                return data;
            }
        });
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        SWScoreboard.impl.removeScoreboard(event.getPlayer());
    }

    static Scoreboard getScoreboard() {
        return scoreboard;
    }

    static void startTime() {
        startTime = System.currentTimeMillis();
    }
}
