package speiger.src.coreengine.assets.oldImpl;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import javax.imageio.ImageIO;

import com.google.gson.JsonObject;

import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.assets.base.IAsset;
import speiger.src.coreengine.assets.base.IAssetPackage;
import speiger.src.coreengine.assets.base.IAssetParser;
import speiger.src.coreengine.utils.helpers.JsonUtil;

public class SimpleAsset extends BaseAsset {
	Path path;
	
	public SimpleAsset(AssetLocation location, IAssetPackage owner, Path path) {
		super(location, owner);
		this.path = path;
	}
	
	@Override
	public IAsset subAsset(String alternative) {
		Path newPath = path.resolveSibling(path.getFileName().toString()+"."+alternative);
		return Files.exists(newPath) ? new SimpleAsset(location.alternate(alternative), owner, newPath) : null;
	}
	
	@Override
	public InputStream stream() throws IOException { return markClosed(Files.newInputStream(path)); }
	@Override
	public ByteBuffer bytes() throws IOException { return ByteBuffer.wrap(Files.readAllBytes(path)); }
	@Override
	public BufferedImage texture() throws Exception { return ImageIO.read(stream()); }
	@Override
	public BufferedReader reader() throws IOException { return markClosed(Files.newBufferedReader(path)); }
	@Override
	public List<String> lines() throws IOException { return Files.readAllLines(path); }
	@Override
	public JsonObject json() throws IOException { return JsonUtil.loadFile(path); }
	@Override
	public <T> T custom(IAssetParser<T> parser) throws IOException { return parser.parseAsset(path, closeable::add); }
	
}
