package com.nokia.sms.message;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.nokia.sms.entity.BindRespStatus;
import com.nokia.sms.entity.CommandId;
import com.nokia.sms.entity.DelBlkBody;
import com.nokia.sms.entity.DelBlkRespStatus;
import com.nokia.sms.exception.ParseException;
import com.nokia.sms.exception.PhoneNumberCanNotBeNullException;

public class ClientMessageUtil {

    private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    /*
     * SMIT_BIND 0x00000001 绑定请求
     */
    public static byte[] getSmitBindMessage(String systemId, String password) throws NoSuchAlgorithmException {
        // 初始化header
        MessageHeader header = new MessageHeader();
        // header 12 + body 16+20 所以是48字节
        header.setMessageLength(38);
        header.setCommandId(CommandId.SMIT_BIND);
        // 所有的初始连接需要从0开始
        header.setSequenceNumber(0L);
        // 初始化
        byte[] message = new byte[header.getMessageLength()];

        // 填充header
        System.arraycopy(header.toByteArray(), 0, message, 0, 12);

        // 填充systemId
        byte[] systemIdByteArray = systemId.getBytes();
        System.arraycopy(systemIdByteArray, 0, message, 12, systemIdByteArray.length);
        // System.arraycopy(systemIdByteArray, 0, message, 28 -
        // systemIdByteArray.length, systemIdByteArray.length);

        // 填充AuthCode
        String authCodeString = password + dateFormat.format(new Date());
        System.out.println("authCodeString : " + authCodeString);
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        // sha1以后为20字节
        System.arraycopy(digest.digest(authCodeString.getBytes()), 0, message, 18, 20);
        return message;
    }

    /*
     * SMIT_DELBLK 0x00000003 解除黑名单请求
     */
    public static byte[] getSmitDelBlkMessage(DelBlkBody delBlkBody, long sequenceNumber)
            throws PhoneNumberCanNotBeNullException {
        // 初始化header
        MessageHeader header = new MessageHeader();
        // header 12 + body 21 + 1 + 1 + 1 所以是36字节
        header.setMessageLength(27);
        header.setCommandId(CommandId.SMIT_DELBLK);
        // 从客户端获取下一个序号
        header.setSequenceNumber(sequenceNumber);
        // 初始化
        byte[] message = new byte[header.getMessageLength()];
        // 填充header
        System.arraycopy(header.toByteArray(), 0, message, 0, 12);

        // 填充 blk_Num 21字节
        if (delBlkBody.getBlkNum() == null) {
            throw new PhoneNumberCanNotBeNullException();
        }
        byte[] blkNumByteArray = delBlkBody.getBlkNum().getBytes();
        System.arraycopy(blkNumByteArray, 0, message, 12, blkNumByteArray.length);
        // 填充 block_type
        message[24] = delBlkBody.getBlock_type();
        message[25] = delBlkBody.getBlk_type();
        message[26] = delBlkBody.getProvince_code();
        return message;
    }

    /*
     * SMIT_ACTIVE_TEST 0x0000000f 链路检测
     */
    public static byte[] getSmitActiveTestMessage(long sequenceNumber) {
        // 初始化header
        MessageHeader header = new MessageHeader();
        // header 12 + body 0 所以是12字节
        header.setMessageLength(12);
        header.setCommandId(CommandId.SMIT_ACTIVE_TEST);
        // 从客户端获取下一个序号
        header.setSequenceNumber(sequenceNumber);
        // 初始化
        byte[] message = new byte[header.getMessageLength()];
        // 填充header
        System.arraycopy(header.toByteArray(), 0, message, 0, 12);
        return message;
    }

    /*
     * SMIT_BIND_RESP 0x80000001 绑定应答
     */
    public static BindRespStatus parseSmitBindRespMessage(byte[] message) throws ParseException {
        MessageHeader header = MessageHeader.parse(message);
        if (header.getCommandId().equals(CommandId.SMIT_BIND_RESP)) {
            return BindRespStatus.parse(message[12]);
        }
        throw new ParseException("期待 SMIT_BIND_RESP 消息,收到了 " + header.getCommandId() + " 消息");
    }

    /*
     * SMIT_DELBLK_RESP 0x80000003 解除黑名单应答
     */
    public static DelBlkRespStatus parseSmitDelBlkRespMessage(byte[] message) throws ParseException {
        MessageHeader header = MessageHeader.parse(message);
        if (header.getCommandId().equals(CommandId.SMIT_DELBLK_RESP)) {
            return DelBlkRespStatus.parse(message[12]);
        }
        throw new ParseException("期待 SMIT_DELBLK_RESP 消息,收到了 " + header.getCommandId() + " 消息");
    }

    /*
     * SMIT_ACTIVE_TEST_RESP 0x8000000f 链路检测应答
     */
    public static long parseSmitActiveTestRespMessage(byte[] message) throws ParseException {
        MessageHeader header = MessageHeader.parse(message);
        // 判断消息类型
        if (CommandId.SMIT_ACTIVE_TEST_RESP.equals(header.getCommandId())) {
            return header.getSequenceNumber();
        }
        throw new ParseException("期待 SMIT_ACTIVE_TEST_RESP 消息,收到了 " + header.getCommandId() + " 消息");
    }
}
