package com.muchinfo.mtp3century.listener;

import com.muchinfo.mtp.proto.SystemMI1.LoginStatusOperRsp;
import com.muchinfo.mtp.proto.SystemMI1.ManageKickoffRsp;
import com.muchinfo.mtp3common.enumtype.FunCodeConstants;
import com.muchinfo.mtp3common.vo.Calculator;
import com.muchinfo.mtp3common.utils.PropertyUtils;
import com.muchinfo.mtp3common.vo.ProtoHelper;
import com.muchinfo.mtp3common.utils.SerialNumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class LoginStatusMsgListener implements MessageListener {
	private final static Logger logger = LoggerFactory.getLogger(LoginStatusMsgListener.class);
	
	@Override
	@RabbitListener(queues = "loginStatusMsgQueue")
	public void onMessage(Message message) {
		byte[] msgBody = message.getBody();
		//ProtoHelper
		logger.debug("get message-----------LoginStatusMsgListener-----------------------------:---");
		int serverId = ProtoHelper.getServerIdForNTAS(msgBody);
		int funCode = ProtoHelper.getFunCodeForNTAS(msgBody);
		if(serverId != PropertyUtils.serverId){
			logger.debug("非本服务器消息-------------------------{}-----------",funCode);
			return;
		}
		try{
			byte[] body = ProtoHelper.getProtoForNTAS(msgBody);
			switch (funCode){
				case FunCodeConstants.FunCode_LoginStatusOperRsp:{
					LoginStatusOperRsp rsp = LoginStatusOperRsp.parseFrom(body);
					logger.debug("锁定、解锁响应-{}-----------",rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if(c != null){
						c.start();
					}
					
					break;
				}
				case FunCodeConstants.FunCode_Trade_ManageKickoffRsp:{
					ManageKickoffRsp rsp = ManageKickoffRsp.parseFrom(body);
					logger.debug("管理端踢人应答-{}-----------",rsp.toString());
					Long uuid = Long.valueOf(rsp.getHeader().getUUID());
					SerialNumberUtils.putMessageMap(uuid, rsp);
					Calculator c = SerialNumberUtils.lockMap.get(uuid);
					if(c != null){
						c.start();
					}
					break;
				}
				default:break;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
	}
}
