package com.nokia.sms.sgip.message;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

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

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

public class SubmitMessage extends SgipMessage {
    public static final int COMMAND_ID = 3;
    private String spNumber;
    private String chargeNumber;
    private byte userCount;
    private String[] userNumber;
    private String corpId;
    private String serviceType;
    private byte feeType;
    private String feeValue;
    private String givenValue;
    private byte agentFlag;
    private byte morelatetoMTFlag;
    private byte priority;
    private String expireTime;
    private String scheduleTime;
    private byte reportFlag;
    private byte tppid;
    private byte tpudhi;
    private byte messageCoding;
    private byte messageType;
    private int messageLength;
    private byte[] messageContent;
    private String reserve;

    public IoBuffer getBuffer() {
        IoBuffer buffer = IoBuffer.allocate(200).setAutoExpand(true);
        buffer.position(20);
        int oldPosition = buffer.position();
        buffer.put(this.spNumber.getBytes());
        buffer.position(oldPosition + 21);
        oldPosition = buffer.position();
        buffer.put(this.chargeNumber.getBytes());
        buffer.position(oldPosition + 21);
        this.userCount = ((byte) this.userNumber.length);
        buffer.put(this.userCount);
        for (int i = 0; i < this.userNumber.length; i++) {
            oldPosition = buffer.position();
            buffer.put(this.userNumber[i].getBytes());
            buffer.position(oldPosition + 21);
        }
        oldPosition = buffer.position();
        buffer.put(this.corpId.getBytes());
        buffer.position(oldPosition + 5);
        oldPosition = buffer.position();
        buffer.put(this.serviceType.getBytes());
        buffer.position(oldPosition + 10);
        buffer.put(this.feeType);
        oldPosition = buffer.position();
        buffer.put(this.feeValue.getBytes());
        buffer.position(oldPosition + 6);
        oldPosition = buffer.position();
        buffer.put(this.givenValue.getBytes());
        buffer.position(oldPosition + 6);
        buffer.put(this.agentFlag);
        buffer.put(this.morelatetoMTFlag);
        buffer.put(this.priority);
        oldPosition = buffer.position();
        if (this.expireTime != null) {
            buffer.put(this.expireTime.getBytes());
        }
        buffer.position(oldPosition + 16);
        oldPosition = buffer.position();
        if (this.scheduleTime != null) {
            buffer.put(this.scheduleTime.getBytes());
        }
        buffer.position(oldPosition + 16);
        buffer.put(this.reportFlag);
        buffer.put(this.tppid);
        buffer.put(this.tpudhi);
        buffer.put(this.messageCoding);

        buffer.put(this.messageType);
        this.messageLength = this.messageContent.length;
        buffer.putInt(this.messageLength);
        buffer.put(this.messageContent);
        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(3);
        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.spNumber = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        this.chargeNumber = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        this.userCount = ioBuffer.get();
        this.userNumber = new String[this.userCount];
        for (int i = 0; i < this.userCount; i++) {
            this.userNumber[i] = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        }
        this.corpId = ioBuffer.getString(5, MessageUtil.gbkDecoder);
        this.serviceType = ioBuffer.getString(10, MessageUtil.gbkDecoder);
        this.feeType = ioBuffer.get();
        this.feeValue = ioBuffer.getString(6, MessageUtil.gbkDecoder);
        this.givenValue = ioBuffer.getString(6, MessageUtil.gbkDecoder);
        this.agentFlag = ioBuffer.get();
        this.morelatetoMTFlag = ioBuffer.get();
        this.priority = ioBuffer.get();
        this.expireTime = ioBuffer.getString(16, MessageUtil.gbkDecoder);
        this.scheduleTime = ioBuffer.getString(16, MessageUtil.gbkDecoder);
        this.reportFlag = ioBuffer.get();
        this.tppid = ioBuffer.get();
        this.tpudhi = ioBuffer.get();
        this.messageCoding = ioBuffer.get();
        this.messageType = ioBuffer.get();
        this.messageLength = ioBuffer.getInt();
        this.messageContent = new byte[this.messageLength];
        ioBuffer.get(this.messageContent);
        this.reserve = ioBuffer.getString(8, MessageUtil.gbkDecoder);
    }

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

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

    public String getChargeNumber() {
        return this.chargeNumber;
    }

    public void setChargeNumber(String chargeNumber) {
        this.chargeNumber = chargeNumber;
    }

    public byte getUserCount() {
        return this.userCount;
    }

    public void setUserCount(byte userCount) {
        this.userCount = userCount;
    }

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

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

    public String getCorpId() {
        return this.corpId;
    }

    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }

    public String getServiceType() {
        return this.serviceType;
    }

    public void setServiceType(String serviceType) {
        this.serviceType = serviceType;
    }

    public byte getFeeType() {
        return this.feeType;
    }

    public void setFeeType(byte feeType) {
        this.feeType = feeType;
    }

    public String getFeeValue() {
        return this.feeValue;
    }

    public void setFeeValue(String feeValue) {
        this.feeValue = feeValue;
    }

    public String getGivenValue() {
        return this.givenValue;
    }

    public void setGivenValue(String givenValue) {
        this.givenValue = givenValue;
    }

    public byte getAgentFlag() {
        return this.agentFlag;
    }

    public void setAgentFlag(byte agentFlag) {
        this.agentFlag = agentFlag;
    }

    public byte getMorelatetoMTFlag() {
        return this.morelatetoMTFlag;
    }

    public void setMorelatetoMTFlag(byte morelatetoMTFlag) {
        this.morelatetoMTFlag = morelatetoMTFlag;
    }

    public byte getPriority() {
        return this.priority;
    }

    public void setPriority(byte priority) {
        this.priority = priority;
    }

    public String getExpireTime() {
        return this.expireTime;
    }

    public void setExpireTime(String expireTime) {
        this.expireTime = expireTime;
    }

    public String getScheduleTime() {
        return this.scheduleTime;
    }

    public void setScheduleTime(String scheduleTime) {
        this.scheduleTime = scheduleTime;
    }

    public byte getReportFlag() {
        return this.reportFlag;
    }

    public void setReportFlag(byte reportFlag) {
        this.reportFlag = reportFlag;
    }

    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 byte getMessageType() {
        return this.messageType;
    }

    public void setMessageType(byte messageType) {
        this.messageType = messageType;
    }

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

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

    public byte[] getMessageContent() {
        return this.messageContent;
    }

    public void setMessageContent(byte[] messageContent) {
        this.messageContent = messageContent;
    }

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

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

    public String toString() {
        String content = null;
        try {
            if (this.messageCoding == 8) {
                content = new String(this.messageContent, "UnicodeBigUnmarked");
            } else if (this.messageCoding == 15) {
                content = new String(this.messageContent, "GBK");
            } else {
                content = new String(this.messageContent, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "SubmitMessage [head=" + this.head + ",spNumber=" + this.spNumber + ",chargeNumber=" + this.chargeNumber
                + ",userCount=" + this.userCount + ",userNumber=" + Arrays.toString(this.userNumber) + ",corpId="
                + this.corpId + ",serviceType=" + this.serviceType + ",feeType=" + this.feeType + ",feeValue="
                + this.feeValue + ",givenValue=" + this.givenValue + ",agentFlag=" + this.agentFlag
                + ",morelatetoMTFlag=" + this.morelatetoMTFlag + ",priority=" + this.priority + ",expireTime="
                + this.expireTime + ",scheduleTime=" + this.scheduleTime + ",reportFlag=" + this.reportFlag + ",tppid="
                + this.tppid + ",tpudhi=" + this.tpudhi + ",messageCoding=" + this.messageCoding + ",messageType="
                + this.messageType + ",messageLength=" + this.messageLength + ",messageContent=" + content + ",reserve="
                + this.reserve + "]";
    }
}
