package com.nokia.esb;

import java.io.UnsupportedEncodingException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nokia.pojo.TousuSheet;

public class MessageUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 构造一条鉴权消息
     * 
     * @param userName
     * @param password
     * @return 字符串格式的鉴权消息
     */
    public static String getVerifyMessage(String userName, String password) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<Connect>\r\n");
        stringBuffer.append("UserName:").append(userName).append("\r\n");
        stringBuffer.append("Password:").append(password).append("\r\n");
        stringBuffer.append("</Connect>\r\n");
        return stringBuffer.toString();
    }

    /**
     * 解析鉴权消息的返回值
     * 
     * @param bytes
     * @return 字符串0表示成功，失败返回完整的鉴权消息
     */
    public static String parseVerifyRespone(byte[] bytes) {
        try {
            String resp = new String(bytes, "utf-8");
            resp = resp.trim();
            if (resp.startsWith("<ConnectACK>")) {
                int index = resp.indexOf("VerifyResult:0");
                if (index > 0) {
                    return "0";
                } else {
                    return resp;
                }
            } else {
                throw new RuntimeException("鉴权失败，鉴权消息格式错误");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 心跳检测的消息
     * 
     * @return
     */
    public static String getHeartbeatMessage() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<HeartBeat>\r\n");
        stringBuffer.append("ProvinceID:18\r\n");
        stringBuffer.append("</HeartBeat>\r\n");
        return stringBuffer.toString();
    }

    /**
     * 解析心跳消息
     * 
     * @param bytes
     * @return
     */
    public static String parseHeartbeatRespone(byte[] bytes) {
        try {
            String resp = new String(bytes, "utf-8");
            resp = resp.trim();
            if (resp.startsWith("<HeartBeatACK>")) {
                return resp;
            } else {
                throw new RuntimeException("心跳失败" + resp);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

    public static String getBusinessMessage(TousuSheet tousuSheet) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<AlarmStart>\r\n");
        try {
            stringBuffer.append(objectMapper.writeValueAsString(tousuSheet));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
        stringBuffer.append("\r\n");
        // stringBuffer.append("MsgSerial:").append(tousuSheet.getKfsn()).append("\r\n");
        // stringBuffer.append("city:").append(tousuSheet.getCity()).append("\r\n");
        // stringBuffer.append("kfsn:").append(tousuSheet.getKfsn()).append("\r\n");
        // stringBuffer.append("acceptNo:").append(tousuSheet.getAcceptNo()).append("\r\n");
        // stringBuffer.append("custLevel:").append(tousuSheet.getCustLevel()).append("\r\n");
        // stringBuffer.append("pkgName:").append(tousuSheet.getPkgName()).append("\r\n");
        // stringBuffer.append("acceptChannel:").append(tousuSheet.getAcceptChannel()).append("\r\n");
        // stringBuffer.append("cplLevel:").append(tousuSheet.getCplLevel()).append("\r\n");
        // stringBuffer.append("cplStype:").append(tousuSheet.getCplStype()).append("\r\n");
        // stringBuffer.append("cplSname:").append(tousuSheet.getCplSname()).append("\r\n");
        // stringBuffer.append("cplSno:").append(tousuSheet.getCplSno()).append("\r\n");
        // stringBuffer.append("cplLon:").append(tousuSheet.getCplLon()).append("\r\n");
        // stringBuffer.append("cplLat:").append(tousuSheet.getCplLat()).append("\r\n");
        // stringBuffer.append("yhLon:").append(tousuSheet.getYhLon()).append("\r\n");
        // stringBuffer.append("yhLat:").append(tousuSheet.getYhLat()).append("\r\n");
        // stringBuffer.append("yhStype:").append(tousuSheet.getYhStype()).append("\r\n");
        // stringBuffer.append("yhNtype:").append(tousuSheet.getYhNtype()).append("\r\n");
        // stringBuffer.append("dstaddress:").append(tousuSheet.getDstaddress()).append("\r\n");
        // stringBuffer.append("yhOpinion:").append(tousuSheet.getYhOpinion()).append("\r\n");
        stringBuffer.append("<AlarmEnd>\r\n");
        return stringBuffer.toString();
    }
}
