package de.siphalor.was.content;

import de.siphalor.was.content.pack.ContentPack;
import de.siphalor.was.content.resource.Resource;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class ContentManager {
	private final List<ContentPack> packs = new LinkedList<>();

	public ContentManager() {
	}

	public void clear() {
		packs.clear();
	}

	public void addPack(ContentPack pack) {
		packs.add(pack);
	}

	public Collection<ContentPack> getPacks() {
		return packs;
	}

	@NotNull
	public Stream<Resource> getResources(@NotNull String location, @NotNull String type) {
		return packs.stream().flatMap(pack -> pack.getResources(location, type)).distinct();
	}

	@NotNull
	public Optional<Resource> getResource(@NotNull String location) {
		Resource resource;
		for (ContentPack pack : packs) {
			resource = pack.getResource(location);
			if (resource != null) {
				return Optional.of(resource);
			}
		}
		return Optional.empty();
	}

	@NotNull
	public Stream<Resource> getAllOfResource(@NotNull String location) {
		return packs.stream().flatMap(pack -> Stream.ofNullable(pack.getResource(location)));
	}
}
