package de.viper.survivalplus.Manager;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.util.*;

import de.viper.survivalplus.SurvivalPlus;

public class BlockManager {

    private final SurvivalPlus plugin;
    private final Map<UUID, Set<UUID>> blockedPlayers = new HashMap<>();
    private File blocksFile;
    private FileConfiguration blocksConfig;

    public BlockManager(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.blocksFile = new File(plugin.getDataFolder(), "blocks.yml");
        if (!blocksFile.exists()) {
            try {
                blocksFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.blocksConfig = YamlConfiguration.loadConfiguration(blocksFile);
        loadBlocks();
    }

    private void saveBlocks() {
        for (UUID key : blockedPlayers.keySet()) {
            List<String> uuids = new ArrayList<>();
            for (UUID uuid : blockedPlayers.get(key)) {
                uuids.add(uuid.toString());
            }
            blocksConfig.set(key.toString(), uuids);
        }
        try {
            blocksConfig.save(blocksFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadBlocks() {
        blockedPlayers.clear();
        for (String key : blocksConfig.getKeys(false)) {
            UUID blockerUUID = UUID.fromString(key);
            List<String> blockedUUIDs = blocksConfig.getStringList(key);
            Set<UUID> blockedSet = new HashSet<>();
            for (String uuidStr : blockedUUIDs) {
                blockedSet.add(UUID.fromString(uuidStr));
            }
            blockedPlayers.put(blockerUUID, blockedSet);
        }
    }

    public void blockPlayer(Player blocker, Player toBlock) {
        blockedPlayers.computeIfAbsent(blocker.getUniqueId(), k -> new HashSet<>()).add(toBlock.getUniqueId());
        saveBlocks(); // Sofort speichern
    }

    public void unblockPlayer(Player blocker, Player toUnblock) {
        Set<UUID> blocked = blockedPlayers.get(blocker.getUniqueId());
        if (blocked != null) {
            blocked.remove(toUnblock.getUniqueId());
            if (blocked.isEmpty()) {
                blockedPlayers.remove(blocker.getUniqueId());
            }
            saveBlocks(); // Sofort speichern
        }
    }

    public boolean hasBlocked(Player blocker, Player potentialBlocked) {
        return blockedPlayers.getOrDefault(blocker.getUniqueId(), Collections.emptySet())
                .contains(potentialBlocked.getUniqueId());
    }

    public Set<UUID> getBlockedPlayers(Player player) {
        return blockedPlayers.getOrDefault(player.getUniqueId(), Collections.emptySet());
    }

    public void clear(Player player) {
        blockedPlayers.remove(player.getUniqueId());
        saveBlocks();
    }
}