package net.voltexstudios.npcConversations.npc;

import lombok.Getter;
import org.bukkit.entity.Entity;

import java.util.HashSet;
import java.util.Set;

@Getter
public final class NPCService {

    private static NPCService instance;
    private final Set<NPC> npcs;

    public NPCService(NPCData npcData) {
        instance = this;

        if (npcData == null) {
            this.npcs = new HashSet<>();
            return;
        }

        this.npcs = npcData.npcs();
    }

    public NPC getNPC(String name) {
        return npcs.stream().filter(npc -> npc.getName().equals(name)).findFirst().orElse(null);
    }

    public NPC getNPC(Entity entity) {
        return npcs.stream().filter(npc -> npc.getClickableEntity().equals(entity.getUniqueId())).findFirst().orElse(null);
    }

    public void addNPC(NPC npc) {
        npcs.add(npc);
    }

    public void removeNPC(NPC npc) {
        npcs.remove(npc);
    }

    public NPCData getNPCData() {
        return new NPCData(npcs);
    }

    public static void saveNPCData() {
        NPCData.saveNPCData();
    }

    public static NPCService getInstance() {
        if (instance == null) {
            instance = new NPCService(null);
        }
        return instance;
    }
}
