package de.siphalor.was.content.quest;

import de.siphalor.was.content.ContentManager;
import de.siphalor.was.content.product.ProductManager;
import de.siphalor.was.util.ResourceManager;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class QuestManager implements ResourceManager<QuestGenerator> {
	private final Map<String, QuestGenerator> questGenerators = new HashMap<>();

	@Override
	public void clear() {
		questGenerators.clear();
	}

	public void reload(@NotNull ContentManager contentManager, @NotNull ProductManager productManager) {
		contentManager.getResources("quests", "csv").forEach(resource -> {
			InputStream inputStream = resource.getInputStream();
			if (inputStream != null) {
				questGenerators.put(resource.getId(), StaticQuestGenerator.fromCsv(inputStream, productManager));
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public QuestGenerator get(String id) {
		return questGenerators.get(id);
	}
}
