package dev._2lstudios.exploitfixer.bukkit.managers;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import dev._2lstudios.exploitfixer.bukkit.instanceables.ExploitPlayer;

public class ExploitPlayerManager {
	private final Plugin plugin;
	private final Server server;
	private final ModuleManager moduleManager;
	private final Map<UUID, ExploitPlayer> exploitPlayers = new HashMap<>();
	private int punishments;

	ExploitPlayerManager(final Plugin plugin, final Server server, final ModuleManager moduleManager) {
		this.plugin = plugin;
		this.server = server;
		this.moduleManager = moduleManager;
		this.punishments = 0;

		reload();
	}

	public ExploitPlayer get(final Player player) {
		final UUID uuid = player.getUniqueId();
		final ExploitPlayer exploitPlayer;

		if (exploitPlayers.containsKey(uuid)) {
			exploitPlayer = exploitPlayers.get(uuid);
		} else {
			exploitPlayer = new ExploitPlayer(this.plugin, player.getName(), moduleManager);
			exploitPlayers.put(uuid, exploitPlayer);
		}

		return exploitPlayer;
	}

	public void clear() {
		final Iterator<UUID> iterator = exploitPlayers.keySet().iterator();

		while (iterator.hasNext()) {
			final UUID uuid = iterator.next();

			if (server.getPlayer(uuid) == null) {
				iterator.remove();
			}
		}
	}

	public void reload() {
		exploitPlayers.clear();

		for (final Player player : server.getOnlinePlayers()) {
			final ExploitPlayer exploitPlayer = get(player);

			exploitPlayer.setLogged(true);
		}
	}

	public int getSize() {
		return exploitPlayers.size();
	}

	public int getPunishments() {
		return punishments;
	}

	public int addPunishment() {
		return ++punishments;
	}
}