package com.mercury.chat.server.protocol;

import static com.mercury.chat.common.MessageType.CRUD;
import static com.mercury.chat.common.MessageType.LOGIN;
import static com.mercury.chat.common.constant.StatusCode.NOT_LOGIN;
import static com.mercury.chat.common.util.Channels.has;
import static com.mercury.chat.common.util.Messages.buildMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.mercury.chat.client.impl.QuickReplyRequest;
import com.mercury.chat.common.constant.Constant;
import com.mercury.chat.common.constant.Operation;
import com.mercury.chat.common.struct.protocol.Message;
import com.mercury.chat.user.repository.UserRepository;

public class CommonCRUDHandler extends SimpleChannelInboundHandler<Message> {

	static final Logger logger = LogManager.getLogger(CommonCRUDHandler.class);
	
	private final UserRepository userService;
	
	public CommonCRUDHandler(UserRepository userService){
		this.userService = userService;
	}
	
	@Override
	 public void messageReceived(ChannelHandlerContext ctx, Message msg) throws Exception {
		
		//FIXME check the authroised user.
		if (CRUD.$(msg)) {
			if(!has(ctx.channel(), Constant.userInfo)){
	    		ctx.writeAndFlush(buildMessage(LOGIN, NOT_LOGIN));
	    		return;
	    	}
			logger.log(Level.INFO, "Receive client CRUD request : ---> "+ msg);

			Object response = null;
			
			Object request = msg.getBody();
			if(request instanceof QuickReplyRequest){
				QuickReplyRequest qrreq = (QuickReplyRequest) request;
				Operation operation = Operation.valOf(qrreq.getOperation());
				switch(operation){
					case LOAD:
						response = userService.loadQuickReply(qrreq.getSaleId());
						break;
					case UPDATE:
						userService.updateQuickReply(qrreq.getSaleId(), qrreq.getQuickReply());
						break;
					case DELETE:
						userService.deleteReply(qrreq.getSaleId(), qrreq.getQuickReply());
						break;
					default:
						break;
				}
			}else{
				//add other handler logic.
			}
			
		    //FIXME the find logic need to be implemented.
			ctx.writeAndFlush(buildMessage(CRUD,response));
		} else
		    ctx.fireChannelRead(msg);
    }
	
}

@@ -576,24 +576,128 @@
 .Logger;%0D%0A%0D%0A
+import com.mercury.chat.client.impl.OrderRequest;%0D%0Aimport com.mercury.chat.client.impl.ProductRequest;%0D%0A
 import com.m
@@ -2297,16 +2297,391 @@
 logic.%0D%0A
+%09%09%09%09if(request instanceof ProductRequest)%7B%0D%0A%09%09%09%09%09ProductRequest productRequest = (ProductRequest) request;%0D%0A%09%09%09%09%09response = userService.loadProductSummary(productRequest.getProductId());%0D%0A%09%09%09%09%7Delse if(request instanceof OrderRequest)%7B%0D%0A%09%09%09%09%09OrderRequest orderRequest = (OrderRequest) request;%0D%0A%09%09%09%09%09response = userService.loadOrderSummary(orderRequest.getOrderId());%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%09%09

implement the load product and order summary.