package com.nokia.sms.sgip.message;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.CharsetDecoder;

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

import com.nokia.sms.sgip.codec.MessageUtil;

public class DeliverMessage extends SgipMessage {
    public static final int COMMAND_ID = 4;
    private String userNumber;
    private String spNumber;
    private byte tppid;
    private byte tpudhi;
    private byte messageCoding;
    private int messageLength;
    private String messageContent;
    private String reserve;

    public IoBuffer getBuffer() {
        IoBuffer buffer = IoBuffer.allocate(200).setAutoExpand(true);
        buffer.position(20);
        int oldPosition = buffer.position();
        buffer.put(this.userNumber.getBytes());
        buffer.position(oldPosition + 21);
        oldPosition = buffer.position();
        buffer.put(this.spNumber.getBytes());
        buffer.position(oldPosition + 21);
        buffer.put(this.tppid);
        buffer.put(this.tpudhi);
        buffer.put(this.messageCoding);
        byte[] content = null;
        try {
            if (this.messageCoding == 8) {
                content = this.messageContent.getBytes("UnicodeBigUnmarked");
            } else if (this.messageCoding == 0) {
                content = this.messageContent.getBytes("ASCII");
            } else {
                content = this.messageContent.getBytes("GBK");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        this.messageLength = content.length;
        buffer.putInt(this.messageLength);
        buffer.put(content);
        oldPosition = buffer.position();
        if (this.reserve != null) {
            buffer.put(this.reserve.getBytes());
        }
        buffer.position(oldPosition + 8);
        oldPosition = buffer.position();
        if (this.head == null) {
            this.head = new Head();
        }
        this.head.setCommandId(4);
        this.head.setMessageLength(oldPosition);
        buffer.position(0);
        buffer.put(this.head.getBuffer());
        buffer.position(oldPosition);

        return buffer.flip();
    }

    public void parseBody(IoBuffer ioBuffer)
            throws IOException {
        this.userNumber = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        this.spNumber = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        this.tppid = ioBuffer.get();
        this.tpudhi = ioBuffer.get();
        this.messageCoding = ioBuffer.get();
        this.messageLength = ioBuffer.getInt();
        CharsetDecoder decoder = null;
        if (this.messageCoding == 8) {
            decoder = MessageUtil.ucs2Decoder;
        } else {
            decoder = MessageUtil.gbkDecoder;
        }
        this.messageContent = ioBuffer.getString(this.messageLength, decoder);
        this.reserve = ioBuffer.getString(8, MessageUtil.gbkDecoder);
    }

    public byte[] getmessageContenByBytes() {
        byte[] bytes = null;
        try {
            if (this.messageContent == null) {
                return null;
            }
            if (this.messageCoding == 8) {
                bytes = this.messageContent.getBytes("UnicodeBigUnmarked");
            } else if (this.messageCoding == 0) {
                bytes = this.messageContent.getBytes("ASCII");
            } else {
                bytes = this.messageContent.getBytes("GBK");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    public String getUserNumber() {
        return this.userNumber;
    }

    public void setUserNumber(String userNumber) {
        this.userNumber = userNumber;
    }

    public String getSpNumber() {
        return this.spNumber;
    }

    public void setSpNumber(String spNumber) {
        this.spNumber = spNumber;
    }

    public byte getTppid() {
        return this.tppid;
    }

    public void setTppid(byte tppid) {
        this.tppid = tppid;
    }

    public byte getTpudhi() {
        return this.tpudhi;
    }

    public void setTpudhi(byte tpudhi) {
        this.tpudhi = tpudhi;
    }

    public byte getMessageCoding() {
        return this.messageCoding;
    }

    public void setMessageCoding(byte messageCoding) {
        this.messageCoding = messageCoding;
    }

    public int getMessageLength() {
        return this.messageLength;
    }

    public void setMessageLength(int messageLength) {
        this.messageLength = messageLength;
    }

    public String getMessageContent() {
        return this.messageContent;
    }

    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }

    public String getReserve() {
        return this.reserve;
    }

    public void setReserve(String reserve) {
        this.reserve = reserve;
    }

    public String toString() {
        return "DeliverMessage [head=" + this.head + ", messageContent=" + this.messageContent + ", messageLength="
                + this.messageLength + ", reserve=" + this.reserve + ", spNumber=" + this.spNumber + ", tppid="
                + this.tppid + ", tpudhi=" + this.tpudhi + ", userNumber=" + this.userNumber + ",messageCoding="
                + this.messageCoding + "]";
    }
}
