package projectzombie.task;

import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.Entity;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.world.chunk.ChunkEventHandler;

public class TaskLadderUp extends Task
{
	private int layerID;
	private int step;

	public TaskLadderUp(Entity parent, BdfObject bdf) {
		super(parent, bdf);
	}
	
	public TaskLadderUp(Entity parent, int layerID) {
		super(parent);
		
		this.layerID = layerID;
		this.step = 0;
	}
	
	@Override
	public void BdfClassLoad(BdfObject bdf) {
		super.BdfClassLoad(bdf);
		
		BdfNamedList nl = bdf.getNamedList();
		
		layerID = nl.get("layerID").getInteger();
		step = nl.get("step").getInteger();
	}
	
	@Override
	public void BdfClassSave(BdfObject bdf) {
		super.BdfClassSave(bdf);
		
		BdfNamedList nl = bdf.getNamedList();
		
		nl.set("layerID", bdf.newObject().setInteger(layerID));
		nl.set("step", bdf.newObject().setInteger(step));
	}

	@Override
	public void update()
	{
		if(parent instanceof EntityPlayer)
		{
			EntityPlayer player = (EntityPlayer)parent;
			Vec3d pos = player.getPos();
			
			switch(step)
			{
			case 0:
				step = 1;
				player.setInAnimation(true);
				break;
			case 1:
				pos.y += 0.04;
				if(pos.y > 8) {
					step = 2;
					pos.y = -1;
					Main.world.setLayer(layerID);
				}
				break;
			case 2:
				if(ChunkEventHandler.loaded) {
					step = 3;
				}
				break;
			case 3:
				pos.y += 0.02;
				if(pos.y >= 0) {
					step = 4;
					pos.y = 0;
					player.setInAnimation(false);
				}
				break;
			}
		}
	}

	@Override
	public boolean isDone() {
		return step == 4;
	}
	
}
