package org.kne.cloud.network.minecraft;
import static org.kne.cloud.network.minecraft.MinecraftPacket.*;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

import org.kne.cloud.network.klalb.PINGPacket;
import org.kne.io.VarDataInputStream;

public class MinecraftInputStream extends VarDataInputStream{
	private MinecraftProtocolContext context;
	private PushbackInputStream in;
	private VarDataInputStream vdi;
	public MinecraftInputStream(InputStream in,MinecraftProtocolContext context) {
		super(in);
	this.in=new PushbackInputStream(in);
	this.vdi=new VarDataInputStream(this.in);
		this.context=context;
	}
	public MinecraftProtocolContext getContext() {
		return context;
	}
	public MinecraftPacket readPacket() throws IOException {
		in.unread(in.read());
		if(context.getState()==2) {
			MinecraftUnsolvedPacket mup=new MinecraftUnsolvedPacket();
			mup.readFromStream(vdi);
			if(mup.getLenth()==-1) {
				return null;
			}else {
			return mup;
			}
		}
		byte[]b=vdi.readPacketVarBytes();
		if(b==null)
			return null;
		VarDataInputStream vdi=new VarDataInputStream(new ByteArrayInputStream(b));
		int type=vdi.readVarInt();
		MinecraftPacket mp;
		if(context.isServer()) {
			switch(context.getState()){
			case 0:
				switch(type) {
				case HANDSHAKE_MOTD:
							mp=new MinecraftHandShakePacket();
							mp.readFromStream(vdi);
							context.setState(((MinecraftHandShakePacket) mp).getNextstate());
							break;
				
					default :
						mp=new MinecraftDefaultPacket(type);
						mp.readFromStream(vdi);
						break;
				}
				break;
			case 1:
				switch(type) {
				case HANDSHAKE_MOTD:
					mp=new MinecraftMotdRequest();
					mp.readFromStream(vdi);
							break;
				case PING_PONG:
					mp=new MinecraftPingPacket();
					mp.readFromStream(vdi);
					break;
					default :
						mp=new MinecraftDefaultPacket(type);
						mp.readFromStream(vdi);
						break;
				}
			break;
			default:
			mp=new MinecraftDefaultPacket(type);
			mp.readFromStream(vdi);
			break;
			}
			
		}else {
			switch(context.getState()) {
					case 1:
			switch(type) {
			case HANDSHAKE_MOTD:
					
					mp=new MinecraftMotdResponse();
					mp.readFromStream(vdi);
					break;
					case PING_PONG:
						mp=new MinecraftPongPacket();
						mp.readFromStream(vdi);
						break;
					default:
						mp=new MinecraftDefaultPacket(type);
						mp.readFromStream(vdi);
						break;
					}
				break;
					
				default :
					mp=new MinecraftDefaultPacket(type);
					mp.readFromStream(vdi);
			}
		}
		//System.out.println(mp);
				return mp;
	}
}
