package zmaster587.advancedRocketry.network;

import java.io.IOException;
import java.util.logging.Logger;

import zmaster587.advancedRocketry.dimension.DimensionManager;
import zmaster587.advancedRocketry.dimension.DimensionProperties;
import zmaster587.libVulpes.network.BasePacket;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;

public class PacketDimInfo extends BasePacket {

	DimensionProperties dimProperties;
	int dimNumber;

	public PacketDimInfo() {}

	public PacketDimInfo(int dimNumber,DimensionProperties dimProperties) {
		this.dimProperties = dimProperties;
		this.dimNumber = dimNumber;
	}

	@Override
	public void write(ByteBuf out) {
		NBTTagCompound nbt = new NBTTagCompound();
		out.writeInt(dimNumber);
		boolean flag = dimProperties == null;
		
		if(!flag) {
			
			//Try to send the nbt data of the dimension to the client, if it fails(probably due to non existent Biome ids) then remove the dimension
			try {
				dimProperties.writeToNBT(nbt);
				PacketBuffer packetBuffer = new PacketBuffer(out);
				out.writeBoolean(false);
				packetBuffer.writeNBTTagCompoundToBuffer(nbt);
			} catch(NullPointerException e) {
				out.writeBoolean(true);
				e.printStackTrace();
				Logger.getLogger("advancedRocketry").warning("Dimension " + dimNumber + " has thrown an exception trying to write NBT, deleting!");
				DimensionManager.getInstance().deleteDimension(dimNumber);
			}

		}
		else
			out.writeBoolean(flag);

	}

	@Override
	public void readClient(ByteBuf in) {
		PacketBuffer packetBuffer = new PacketBuffer(in);
		NBTTagCompound nbt;
		dimNumber = in.readInt();

		if(in.readBoolean()) {
			if(DimensionManager.getInstance().isDimensionCreated(dimNumber)) {
				DimensionManager.getInstance().deleteDimension(dimNumber);
			}
		}
		else {
			//TODO: error handling
			try {
				nbt = packetBuffer.readNBTTagCompoundFromBuffer();

			} catch (IOException e) {
				e.printStackTrace();
				return;
			}

			if(dimNumber == 0) {
				DimensionManager.overworldProperties.readFromNBT(nbt);
			}
			else if( DimensionManager.getInstance().isDimensionCreated(dimNumber) ) {
				DimensionManager.getInstance().getDimensionProperties(dimNumber).readFromNBT(nbt);
			} else {
				DimensionManager.getInstance().registerDimNoUpdate(DimensionProperties.createFromNBT(dimNumber, nbt), true);
			}
		}
	}

	@Override
	public void read(ByteBuf in) {
		//Should never be read on the server!
	}

	@Override
	public void executeClient(EntityPlayer thePlayer) {}

	@Override
	public void executeServer(EntityPlayerMP player) {}

}

@@ -595,16 +595,36 @@
 mNumber;
+%0A%09boolean deleteDim;
 %0A%0A%09publi
@@ -1780,182 +1780,63 @@
 ();%0A
-%0A%09%09if(in.readBoolean()) %7B%0A%09%09%09if(DimensionManager.getInstance().isDimensionCreated(dimNumber)) %7B%0A%09%09%09%09DimensionManager.getInstance().deleteDimension(dimNumber);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else
+%09%09%0A%0A%09%09deleteDim = in.readBoolean();%0A%09%09%0A%09%09if(!deleteDim)
  %7B%0A%09
@@ -2000,63 +2000,69 @@
 %09%09%7D%0A
-%0A
 %09%09%09
-if(dimNumber == 0) %7B%0A%09%09%09%09DimensionManager.overworld
+dimProperties = new DimensionProperties(dimNumber);%0A%09%09%09dim
 Prop
@@ -2092,23 +2092,189 @@
 ;%0A%09%09
+%7D%0A
 %09%7D%0A
-%09%09%09else if( 
+%0A%09@Override%0A%09public void read(ByteBuf in) %7B%0A%09%09//Should never be read on the server!%0A%09%7D%0A%0A%09@Override%0A%09public void executeClient(EntityPlayer thePlayer) %7B%0A%09%09if(deleteDim) %7B%0A%09%09%09if(
 Dime
@@ -2329,17 +2329,16 @@
 mNumber)
- 
 ) %7B%0A%09%09%09%09
@@ -2368,19 +2368,22 @@
 tance().
-g
+del
 et
+e
 Dimensio
@@ -2387,64 +2387,166 @@
 sion
-Properties(dimNumber).readFromNBT(nbt)
+(dimNumber);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else if(dimProperties != null)%0A%09%09%7B%0A%09%09%09if(dimNumber == 0) %7B%0A%09%09%09%09DimensionManager.overworldProperties = dimProperties
 ;%0A%09%09%09%7D
- 
+%0A%09%09%09
 else 
-%7B%0A%09%09%09%09
+if( 
 Dime
@@ -2576,249 +2576,223 @@
 e().
-registerDimNoUpdate(DimensionProperties.createFromNBT(dimNumber, nbt), true);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void read(ByteBuf in) %7B%0A%09%09//Should never be read on the server!%0A%09%7D%0A%0A%09@Override%0A%09public void executeClient(EntityPlayer thePlayer) %7B
+isDimensionCreated(dimNumber) ) %7B%0A%09%09%09%09DimensionManager.getInstance().setDimProperties(dimNumber, dimProperties);%0A%09%09%09%7D else %7B%0A%09%09%09%09DimensionManager.getInstance().registerDimNoUpdate(dimProperties, true);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09
 %7D%0A%0A%09

Fix some thread-unsafe code Possible fix for #346