package projectzombie.worker;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import java.util.zip.GZIPOutputStream;

import bdf.types.BdfReader;
import gl_engine.vec.Vec2i;
import projectzombie.settings.Environment;
import projectzombie.util.FileHelpers;
import projectzombie.world.chunk.Chunk;

public class WorkerTaskChunkSave extends WorkerTaskChunk
{
	Chunk chunk;
	String path;
	int id;
	
	public WorkerTaskChunkSave(String path, Vec2i pos, Chunk chunk, int id) {
		this.path = path;
		this.pos = pos;
		this.chunk = chunk;
		this.id = id;
	}
	
	@Override
	public void run()
	{
		if(path == null) {
			return;
		}
		
		String path = "./saves/" + this.path + "/c_" + id + "_" + pos.x + "_" + pos.y + ".bdf.gz";
		
		BdfReader reader = new BdfReader();
		
		chunk.BdfClassSave(reader.getObject());
		
		try
		{
			OutputStream out = new GZIPOutputStream(new FileOutputStream(Environment.gdir + path));
			
			reader.serialize().writeToStream(out);
			
			out.close();
		}
		
		catch(IOException e) {
			e.printStackTrace();
		}
	}
}
