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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.tracer.TNTPosition;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.util.Map;

public class TraceShowManager implements Listener {
    private TraceShowManager() {
    }

    private static final Map<Player, ShowMode> showModes = new HashMap<>();

    public static void show(Player player, ShowMode showMode) {
        hide(player);
        showModes.put(player, showMode);
        StoredRecords.showAll(showMode);
    }

    public static void hide(Player player) {
        ShowMode showMode = showModes.remove(player);
        if (showMode == null)
            return;
        showMode.hide();
    }

    /* Only to be called by record */
    static void show(TNTPosition tnt) {
        for (ShowMode mode : showModes.values())
            mode.show(tnt);
    }

    /* Only to be called by StoredRecords */
    static void clear() {
        for (ShowMode mode : showModes.values())
            mode.hide();
    }

    /* Internal if player leaves*/
    static {
        Bukkit.getPluginManager().registerEvents(new TraceShowManager(), BauSystem.getPlugin());
    }

    @EventHandler
    public void onLeave(PlayerQuitEvent event) {
        showModes.remove(event.getPlayer());
    }

    public static boolean hasActiveShow(Player player) {
        return showModes.containsKey(player);
    }

}
