package net.wayward_realms.waywardevents;

import net.wayward_realms.waywardlib.events.Dungeon;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

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

public class DungeonImpl implements Dungeon, ConfigurationSerializable {

    private File file;

    private Set<String> dungeonMasters = new HashSet<>();
    private Set<String> players = new HashSet<>();
    private boolean active;

    public DungeonImpl() {}

    public DungeonImpl(File file) {
        this.file = file;
        load();
    }

    @Override
    public Collection<OfflinePlayer> getDungeonMasters() {
        Set<OfflinePlayer> dungeonMasters = new HashSet<>();
        for (String dungeonMasterName : this.dungeonMasters) {
            dungeonMasters.add(Bukkit.getServer().getOfflinePlayer(dungeonMasterName));
        }
        return dungeonMasters;
    }

    @Override
    public void addDungeonMaster(OfflinePlayer dungeonMaster) {
        dungeonMasters.add(dungeonMaster.getName());
        save();
    }

    @Override
    public void removeDungeonMaster(OfflinePlayer dungeonMaster) {
        dungeonMasters.remove(dungeonMaster.getName());
        save();
    }

    @Override
    public Collection<OfflinePlayer> getPlayers() {
        Set<OfflinePlayer> players = new HashSet<>();
        for (String playerName : this.players) {
            players.add(Bukkit.getServer().getOfflinePlayer(playerName));
        }
        return players;
    }

    @Override
    public void addPlayer(OfflinePlayer player) {
        players.add(player.getName());
        save();
    }

    @Override
    public void removePlayer(OfflinePlayer player) {
        players.remove(player.getName());
        save();
    }

    @Override
    public boolean isActive() {
        return active;
    }

    @Override
    public void setActive(boolean active) {
        this.active = active;
        save();
    }

    @Override
    public Map<String, Object> serialize() {
        Map<String, Object> serialised = new HashMap<>();
        serialised.put("dungeon-masters", dungeonMasters);
        serialised.put("players", players);
        serialised.put("active", active);
        return serialised;
    }

    @SuppressWarnings("unchecked")
    public static DungeonImpl deserialize(Map<String, Object> serialised) {
        DungeonImpl deserialised = new DungeonImpl();
        deserialised.dungeonMasters.addAll((Set<String>) serialised.get("dungeon-masters"));
        deserialised.players.addAll((Set<String>) serialised.get("players"));
        deserialised.active = (Boolean) serialised.get("active");
        return deserialised;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void save() {
        YamlConfiguration config = new YamlConfiguration();
        config.set("dungeon", this);
        try {
            config.save(file);
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public void load() {
        YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
        Dungeon dungeon = (Dungeon) config.get("dungeon");
        for (OfflinePlayer dungeonMaster : dungeon.getDungeonMasters()) {
            dungeonMasters.add(dungeonMaster.getName());
        }
        for (OfflinePlayer player : dungeon.getPlayers()) {
            dungeonMasters.add(player.getName());
        }
        active = dungeon.isActive();
    }

}

@@ -462,22 +462,20 @@
 ate Set%3C
-String
+UUID
 %3E dungeo
@@ -518,22 +518,20 @@
 ate Set%3C
-String
+UUID
 %3E player
@@ -845,32 +845,17 @@
 or (
-String dungeonMasterName
+UUID uuid
  : t
@@ -947,25 +947,12 @@
 yer(
-dungeonMasterName
+uuid
 ));%0A
@@ -1113,36 +1113,40 @@
 ungeonMaster.get
-Name
+UniqueId
 ());%0A        sav
@@ -1277,36 +1277,40 @@
 ungeonMaster.get
-Name
+UniqueId
 ());%0A        sav
@@ -1458,25 +1458,17 @@
 or (
-String playerName
+UUID uuid
  : t
@@ -1546,18 +1546,12 @@
 yer(
-playerName
+uuid
 ));%0A
@@ -1677,36 +1677,40 @@
 s.add(player.get
-Name
+UniqueId
 ());%0A        sav
@@ -1813,36 +1813,40 @@
 emove(player.get
-Name
+UniqueId
 ());%0A        sav
@@ -2157,24 +2157,200 @@
 ashMap%3C%3E();%0A
+        Set%3CString%3E dungeonMasterUUIDStrings = new HashSet%3C%3E();%0A        for (UUID uuid : dungeonMasters) %7B%0A            dungeonMasterUUIDStrings.add(uuid.toString());%0A        %7D%0A
         seri
@@ -2392,20 +2392,185 @@
 onMaster
+UUIDString
 s);%0A
+        Set%3CString%3E playerUUIDStrings = new HashSet%3C%3E();%0A        for (UUID uuid : players) %7B%0A            playerUUIDStrings.add(uuid.toString());%0A        %7D%0A
         
@@ -2601,16 +2601,26 @@
 , player
+UUIDString
 s);%0A    
@@ -2864,29 +2864,28 @@
         
-deserialised.
+Set%3CString%3E 
 dungeonM
@@ -2889,25 +2889,24 @@
 onMaster
-s.addAll(
+UUIDs = 
 (Set%3CStr
@@ -2940,25 +2940,24 @@
 on-masters%22)
-)
 ;%0A        de
@@ -2958,77 +2958,376 @@
     
-deserialised.players.addAll((Set%3CString%3E) serialised.get(%22players%22));
+for (String dungeonMasterUUIDString : dungeonMasterUUIDs) %7B%0A            deserialised.dungeonMasters.add(UUID.fromString(dungeonMasterUUIDString));%0A        %7D%0A        Set%3CString%3E playerUUIDs = (Set%3CString%3E) serialised.get(%22players%22);%0A        for (String playerUUIDString : playerUUIDs) %7B%0A            deserialised.players.add(UUID.fromString(playerUUIDString));%0A        %7D
 %0A   
@@ -4042,28 +4042,32 @@
 onMaster.get
-Name
+UniqueId
 ());%0A       
@@ -4174,12 +4174,16 @@
 .get
-Name
+UniqueId
 ());

Switch dungeon masters & dungeon players to UUIDs, #94