package com.nokia.sms.sgip.message;

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

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

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

public class ReportMessage extends SgipMessage {
    public static final int COMMAND_ID = 5;
    private int[] submitSN;
    private byte reportType;
    private String userNumber;
    private byte state;
    private byte errorCode;
    private String reserve;

    public IoBuffer getBuffer() {
        if (this.head == null) {
            this.head = new Head();
        }
        this.head.setCommandId(5);
        this.head.setMessageLength(64);
        IoBuffer buffer = IoBuffer.allocate(64);
        buffer.put(this.head.getBuffer());
        buffer.putInt(this.submitSN[0]);
        buffer.putInt(this.submitSN[1]);
        buffer.putInt(this.submitSN[2]);
        buffer.put(this.reportType);
        int oldPosition = buffer.position();
        buffer.put(this.userNumber.getBytes());
        buffer.position(oldPosition + 21);
        buffer.put(this.state);
        buffer.put(this.errorCode);
        oldPosition = buffer.position();
        if (this.reserve != null) {
            buffer.put(this.reserve.getBytes());
        }
        buffer.position(oldPosition + 8);
        return buffer.flip();
    }

    public void parseBody(IoBuffer ioBuffer)
            throws IOException {
        this.submitSN = new int[3];
        this.submitSN[0] = ioBuffer.getInt();
        this.submitSN[1] = ioBuffer.getInt();
        this.submitSN[2] = ioBuffer.getInt();
        this.reportType = ioBuffer.get();
        this.userNumber = ioBuffer.getString(21, MessageUtil.gbkDecoder);
        this.state = ioBuffer.get();
        this.errorCode = ioBuffer.get();
        this.reserve = ioBuffer.getString(8, MessageUtil.gbkDecoder);
    }

    public int[] getSubmitSN() {
        return this.submitSN;
    }

    public void setSubmitSN(int[] submitSN) {
        this.submitSN = submitSN;
    }

    public int getReportType() {
        return this.reportType;
    }

    public void setReportType(byte reportType) {
        this.reportType = reportType;
    }

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

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

    public int getState() {
        return this.state;
    }

    public void setState(byte state) {
        this.state = state;
    }

    public int getErrorCode() {
        return this.errorCode;
    }

    public void setErrorCode(byte errorCode) {
        this.errorCode = errorCode;
    }

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

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

    public String toString() {
        return "ReportMessage [head=" + this.head + ",reportType=" + this.reportType + ",reserve=" + this.reserve
                + ",state=" + this.state + ",submitSN=" + Arrays.toString(this.submitSN) + ",userNumber="
                + this.userNumber + ",errorCode=" + this.errorCode + "]";
    }
}
