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);
    }

}
