package projectzombie.entity;

import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
import projectzombie.world.layer.layergen.LayerGenRememberPlayerPos;

public class EntityGrapplingHook extends Entity
{
	private int layerId;
	private double height;
	private Entity entity;
	
	public EntityGrapplingHook(BdfObject bdf) {
		BdfClassLoad(bdf);
	}
	
	@Override
	public void BdfClassLoad(BdfObject bdf) {
		super.BdfClassLoad(bdf);
		
		BdfNamedList nl = bdf.getNamedList();
		layerId = nl.get("layer").getInteger();
		height = nl.get("height").getDouble();
	}
	
	@Override
	public void BdfClassSave(BdfObject bdf) {
		super.BdfClassSave(bdf);
		
		BdfNamedList nl = bdf.getNamedList();
		nl.set("layer", bdf.newObject().setInteger(layerId));
		nl.set("height", bdf.newObject().setDouble(height));
	}

	public EntityGrapplingHook(Vec3d pos, int layerId, Entity entity) {
		super(pos, new Vec3d(0, 0, 0));
		
		this.layerId = layerId;
		this.height = -16;
		this.entity = entity;
		
		if(entity instanceof EntityPlayer) {
			EntityPlayer ep = (EntityPlayer)entity;
			ep.in_animation = true;
		}
	}
	
	@Override
	public void tick(Chunk chunk, Layer layer) {
		super.tick(chunk, layer);
		
		if(height >= -8)
		{
			Vec3d epos = entity.getPos();
			
			double h = epos.y;
			epos.y += 0.02;
			
			if(entity instanceof EntityPlayer) {
				EntityPlayer ep = (EntityPlayer)entity;
				ep.moving = true;
			}
			
			if(h >= 8)
			{
				epos.y = 0;
				Main.world.setLayer(layerId);
				
				if(layer.layergen.destroyOnLeave) {
					Main.world.removeLayer(layerId);
				}
				
				if(entity instanceof EntityPlayer)
				{
					EntityPlayer ep = (EntityPlayer)entity;
					ep.in_animation = false;
					ep.moving = false;
				}
				
				
				if(layer.layergen instanceof LayerGenRememberPlayerPos) {
					LayerGenRememberPlayerPos lgrpp = (LayerGenRememberPlayerPos)layer.layergen;
					entity.setPos(lgrpp.getPlayerPos());
				}
				
				kill();
				return;
			}
		}
		
		else {
			height += 0.05;
		}
	}
	
	@Override
	public Model getModel() {
		return Models.ENTITY_GRAPPLING_HOOK;
	}
	
}
