package speiger.src.coreengine.assets;

import java.util.Map;

import speiger.src.coreengine.assets.base.IAsset;
import speiger.src.coreengine.assets.base.IAssetProvider;
import speiger.src.coreengine.assets.base.MultiAsset;

public record AssetFilter(String prefix, String extension) {
	public static AssetFilter json(String prefix) {
		return new AssetFilter(prefix, ".json");
	}
	
	public AssetLocation id(AssetLocation file) {
		String location = file.location();
		return AssetLocation.of(file.domain(), location.substring(prefix().length()+1, location.length()-extension().length()));
	}
	
	public AssetLocation file(AssetLocation id) {
		return AssetLocation.of(id.domain(), prefix()+"/"+id.location()+extension());
	}
	
	public Map<AssetLocation, IAsset> list(IAssetProvider provider) { 
		return provider.listAssets(prefix, T -> T.endsWith(extension)); 
	}
	
	public Map<AssetLocation, IAsset> listRoot(IAssetProvider provider) { 
		return provider.listAssets(prefix, T -> T.endsWith(extension) && !T.isInFolder(prefix));
	}
	
	public Map<AssetLocation, MultiAsset> multi(IAssetProvider provider) {
		return provider.listAllAssets(prefix, T -> T.endsWith(extension));
	}
	
	public Map<AssetLocation, MultiAsset> multiRoot(IAssetProvider provider) {
		return provider.listAllAssets(prefix, T -> T.endsWith(extension) && !T.isInFolder(prefix));
	}
	
}
