package net.voltexstudios.npcConversations.npc;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Set;

public class NPCData {

    private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    private static final String npcDataPath = "plugins/NPCConversations/npcData.json";

    private final Set<NPC> npcs;

    public NPCData(Set<NPC> npcs) {
        this.npcs = npcs;
    }

    public static void saveNPCData() {
        File folder = new File("plugins/NPCConversations");

        if(!folder.exists()) {
            folder.mkdirs();
        }

        try(FileWriter fileWriter = new FileWriter(npcDataPath)) {
            gson.toJson(NPCService.getInstance().getNPCData(), fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static NPCData loadNPCData() {
        try(FileReader fileReader = new FileReader(npcDataPath)) {
            return gson.fromJson(fileReader, NPCData.class);
        } catch (Exception e) {
            return null;
        }
    }

    public Set<NPC> npcs() {
        return npcs;
    }

}
