package com.nokia.sms.sgip.codec;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.mina.core.buffer.IoBuffer;

public class MessageUtil {
    private static int sgipSeq;
    private static final SimpleDateFormat dateForamt = new SimpleDateFormat("MMddHHmmss");
    public static final Charset gbkCharset = Charset.forName("GBK");
    public static final CharsetDecoder gbkDecoder = gbkCharset.newDecoder();
    public static final CharsetEncoder gdkEncoder = gbkCharset.newEncoder();
    public static final Charset ucs2Charset = Charset.forName("UnicodeBigUnmarked");
    public static final CharsetDecoder ucs2Decoder = ucs2Charset.newDecoder();
    public static final CharsetEncoder ucs2Encoder = ucs2Charset.newEncoder();
    public static final byte[] IE_PORT_APP = { 5, 4, 11, -124, 35, -16 };
    public static final byte[] WBXML_HEADER = { 41, 6, 6, 3, -82, -127, -22, -115, -54, 2, 5, 106, 0, 69, -58, 8, 12,
            3 };
    public static final byte[] END1 = { 0, 1, 3 };
    public static final byte[] END2 = { 0, 1, 1 };
    private static int nodeId;
    private static byte smsSeq = 0;

    public static void setNodeId(int nodeId) {
        MessageUtil.nodeId = nodeId;
    }

    public static int[] getFlowId() {
        int[] flowId = new int[3];
        flowId[0] = nodeId;
        flowId[1] = Integer.parseInt(dateForamt.format(new Date()));
        flowId[2] = sgipSeq;
        if (sgipSeq == 2147483647) {
            sgipSeq = 0;
        } else {
            sgipSeq += 1;
        }
        return flowId;
    }

    public static byte[] getBytes(String str, int len) {
        byte[] bytes = null;
        if (str != null) {
            int strLen = str.length();
            if (strLen < len) {
                bytes = new byte[len];
                byte[] b = str.getBytes();
                for (int i = 0; i < b.length; i++) {
                    bytes[i] = b[i];
                }
                for (int i = b.length; i < bytes.length; i++) {
                    bytes[i] = 0;
                }
            } else if (strLen == len) {
                bytes = str.getBytes();
            } else {
                bytes = new byte[len];
                byte[] b = str.getBytes();
                for (int i = 0; i < len; i++) {
                    bytes[i] = b[i];
                }
            }
        } else {
            bytes = new byte[len];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = 0;
            }
        }
        return bytes;
    }

    private static synchronized byte getSmsSeq() {
        if (smsSeq >= 127) {
            return MessageUtil.smsSeq = 0;
        }
        return smsSeq++;
    }

    public static byte[][] subMessage(byte[] strByte, int length) {
        if (strByte == null) {
            return (byte[][]) null;
        }
        byte[][] result = (byte[][]) null;
        int count = (strByte.length - 1) / (length - 6) + 1;
        result = new byte[count][];
        int index = 0;
        byte smsSeq = getSmsSeq();
        for (int i = 0; i < count; i++) {
            byte[] b = null;
            if (index + (length - 6) <= strByte.length) {
                b = new byte[length];
            } else {
                b = new byte[strByte.length - index + 6];
            }
            b[0] = 5;
            b[1] = 0;
            b[2] = 3;
            b[3] = smsSeq;
            b[4] = ((byte) count);
            b[5] = ((byte) (i + 1));
            for (int j = 6; j < b.length; j++) {
                b[j] = strByte[index];
                index++;
            }
            result[i] = b;
        }
        return result;
    }

    public static byte[][] buildWAPPush(String message, String url)
            throws UnsupportedEncodingException {
        byte[] urlBytes = url.getBytes("UTF-8");
        byte[] messageBytes = message.getBytes("UTF-8");
        IoBuffer buffer = IoBuffer.allocate(140).setAutoExpand(true);
        buffer.put(WBXML_HEADER);
        buffer.put(urlBytes);
        buffer.put(END1);
        buffer.put(messageBytes);
        buffer.put(END2);
        buffer.flip();
        int contentLength = buffer.limit();

        int submitCount = contentLength / 128;
        if (contentLength % 128 != 0) {
            submitCount++;
        }
        byte[][] result = new byte[submitCount][];
        byte smsSeq = getSmsSeq();
        for (int i = 0; i < submitCount; i++) {
            int readLength = contentLength - buffer.position();
            if (readLength > 128) {
                readLength = 128;
            }
            IoBuffer buf1 = IoBuffer.allocate(12 + readLength);
            buf1.put((byte) 11);
            buf1.put(IE_PORT_APP);

            buf1.put(new byte[] { 0, 3, smsSeq, (byte) submitCount, (byte) (i + 1) });
            buf1.put(buffer.getSlice(readLength));
            result[i] = buf1.array();
        }
        return result;
    }
}
