package speiger.src.coreengine.rendering.textures.base;

import java.util.function.Predicate;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectLinkedOpenHashSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.coreengine.assets.base.IAssetProvider;
import speiger.src.coreengine.assets.base.IManagedAsset;
import speiger.src.coreengine.assets.base.IReloadableAsset.ISimpleRealodableAsset;

public class TextureTracker implements ISimpleRealodableAsset, IManagedAsset {
	IAssetProvider provider;
	ObjectList<ITexture> textures = new ObjectArrayList<>();
	ObjectSet<ITexture> reloadable = new ObjectLinkedOpenHashSet<>();
	
	public void registerTexture(ITexture texture) {
		register(texture, false);
	}
	
	public void registerReloadable(ITexture texture) {
		register(texture, true);
	}
	
	public void register(ITexture texture, boolean reloadable) {
		textures.add(texture);
		if(reloadable) this.reloadable.add(texture);
	}
	
	public void deleteTexture(ITexture texture) {
		textures.remove(texture);
		reloadable.remove(texture);
	}
	
	@Override
	public void onAssetsReloaded(IAssetProvider provider) {
		textures.filter(Predicate.not(reloadable::contains)).forEach(ITexture::delete);
		textures.clear();
		reloadable.peek(ITexture::reload).forEach(textures::add);
	}
	
	@Override
	public void destroy() {
		textures.forEach(ITexture::delete);
		textures.clear();
		reloadable.clear();
	}
	
	@Override
	public String getName() { return "Texture Tracker"; }
	
	@Override
	public void setProvider(IAssetProvider provider) { this.provider = provider; }
}
