package org.simpleim.server.server;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.simpleim.common.message.FailureResponse;
import org.simpleim.common.message.NewAccountOkResponse;
import org.simpleim.common.message.NewAccountRequest;
import org.simpleim.common.message.Response;
import org.simpleim.server.database.DataBase;
import org.simpleim.server.util.AccountGenerator;

import com.lambdaworks.crypto.SCryptUtil;

public class ServerHandler extends ChannelHandlerAdapter {

	private static final Logger logger = Logger.getLogger(ServerHandler.class.getName());
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		boolean closeNow = true;
		Response response = null;
		String id;
		String password;
		if(msg instanceof NewAccountRequest) {
			closeNow = true;
			id=AccountGenerator.nextId();
			password=AccountGenerator.generatePassword();
			response = new NewAccountOkResponse()
						.setId(id)
						.setPassword(password);
			String hashedPassword = SCryptUtil.scrypt(password, 1 << 15, 8, 1);
			DataBase.InsertNumberRow(id, hashedPassword);
		} else {
			closeNow = true;
			response = new FailureResponse();
		}
		ChannelFuture f = ctx.write(response);
		if(closeNow)
			f.addListener(ChannelFutureListener.CLOSE);
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
		// Close the connection when an exception is raised.
		logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
		ctx.close();
	}
}

@@ -321,32 +321,81 @@
 ailureResponse;%0A
+import org.simpleim.common.message.LoginRequest;%0A
 import org.simpl
@@ -1353,16 +1353,187 @@
 sword);%0A
+%09%09%7D else if(msg instanceof LoginRequest)%7B%0A%09%09%09closeNow = false;%0A%09%09%09LoginRequest request = (LoginRequest) msg;%0A%09%09%09id = request.getId();%0A%09%09%09password = request.getPassword();%0A
 %09%09%7D else

add partial function to start session