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

import de.steamwar.bausystem.commands.CommandTPSLimiter;
import de.steamwar.bausystem.tracer.record.RecordStateMachine;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.core.TPSWatcher;
import de.steamwar.scoreboard.SWScoreboard;
import de.steamwar.scoreboard.ScoreboardCallback;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class BauScoreboard implements Listener {

    @EventHandler
    public void handlePlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();

        SWScoreboard.impl.createScoreboard(player, new ScoreboardCallback() {
            @Override
            public HashMap<String, Integer> getData() {
                return sidebar(player);
            }

            @Override
            public String getTitle() {
                return "§eSteam§8War";
            }
        });
    }

    private HashMap<String, Integer> sidebar(Player p) {
        List<String> strings = new ArrayList<>();
        strings.add("§1");
        strings.add("§eUhrzeit§8: §7" + new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()));
        strings.add("§2");
        Region region = Region.getRegion(p.getLocation());
        strings.add("§eTNT§8: " + region.getTntMode().getName());
        strings.add("§eFreeze§8: " + (region.isFreeze() ? "§aan" : "§caus"));
        strings.add("§eFire§8: " + (region.isFire() ? "§aaus" : "§can"));
        strings.add("§eTrace§8: " + RecordStateMachine.getRecordStatus().getName());
        strings.add("§eLoader§8: " + (AutoLoader.hasLoader(p) ? "§aan" : "§caus"));
        if (region.hasProtection()) {
            strings.add("§eProtect§8: " + (region.isProtect() ? "§aan" : "§caus"));
        }

        if (RecordStateMachine.getRecordStatus().isTracing()) {
            strings.add("§3");
            strings.add("§eTicks§8: §7" + traceTicks());
            strings.add("§eAnzahl TNT§8: §7" + RecordStateMachine.size());
        }

        strings.add("§4");
        strings.add("§eTPS§8: " + tpsColor() + TPSUtils.getTps(TPSWatcher.TPSType.ONE_SECOND) + tpsLimit());

        int i = strings.size();
        HashMap<String, Integer> result = new HashMap<>();
        for (String s : strings)
            result.put(s, i--);
        return result;
    }

    private long traceTicks() {
        return (System.currentTimeMillis() - RecordStateMachine.getStartTime()) / 50;
    }

    private String tpsColor() {
        double tps = TPSUtils.getTps(TPSWatcher.TPSType.ONE_SECOND);
        if (tps > CommandTPSLimiter.getCurrentTPSLimit() * 0.9) {
            return "§a";
        }
        if (tps > CommandTPSLimiter.getCurrentTPSLimit() * 0.5) {
            return "§e";
        }
        return "§c";
    }

    private String tpsLimit() {
        if (CommandTPSLimiter.getCurrentTPSLimit() == 20) {
            return "";
        }
        return "§8/§7" + CommandTPSLimiter.getCurrentTPSLimit();
    }

}
