package com.nokia.sms.sgip.message;

import java.util.Arrays;

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

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

public class Head {
    private int messageLength;
    private int commandId;
    private int[] flowId;

    public IoBuffer getBuffer() {
        IoBuffer ioBuffer = IoBuffer.allocate(20);
        ioBuffer.putInt(this.messageLength);
        ioBuffer.putInt(this.commandId);
        if (this.flowId == null) {
            this.flowId = MessageUtil.getFlowId();
        }
        ioBuffer.putInt(this.flowId[0]);
        ioBuffer.putInt(this.flowId[1]);
        ioBuffer.putInt(this.flowId[2]);
        return ioBuffer.flip();
    }

    public void parse(IoBuffer ioBuffer) {
        this.messageLength = ioBuffer.getInt();
        this.commandId = ioBuffer.getInt();
        this.flowId = new int[3];
        this.flowId[0] = ioBuffer.getInt();
        this.flowId[1] = ioBuffer.getInt();
        this.flowId[2] = ioBuffer.getInt();
    }

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

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

    public int getCommandId() {
        return this.commandId;
    }

    public void setCommandId(int commandId) {
        this.commandId = commandId;
    }

    public int[] getFlowId() {
        return this.flowId;
    }

    public void setFlowId(int[] flowId) {
        this.flowId = flowId;
    }

    public String toString() {
        return "Head [messageLength=" + this.messageLength + ",commandId=0x" + Integer.toHexString(this.commandId)
                + ",flowId=" + Arrays.toString(this.flowId) + "]";
    }

    public String getStrFlowId() {
        if ((this.flowId != null) && (this.flowId.length == 3)) {
            StringBuilder sb = new StringBuilder(12);
            sb.append(this.flowId[0]);
            sb.append(this.flowId[1]);
            sb.append(this.flowId[2]);
            return sb.toString();
        }
        return null;
    }
}
