package com.nokia.sms.sgip.message;

import java.io.IOException;

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

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

public class BindMessage extends SgipMessage {
    public static final int COMMAND_ID = 1;
    private byte loginType;
    private String loginName;
    private String loginPassword;
    private String reserve;

    public IoBuffer getBuffer() {
        if (this.head == null) {
            this.head = new Head();
        }
        this.head.setCommandId(1);
        this.head.setMessageLength(61);
        IoBuffer buffer = IoBuffer.allocate(61);
        buffer.put(this.head.getBuffer());
        buffer.put(this.loginType);
        int oldPosition = buffer.position();
        buffer.put(this.loginName.getBytes());
        buffer.position(oldPosition + 16);
        oldPosition = buffer.position();
        buffer.put(this.loginPassword.getBytes());
        buffer.position(oldPosition + 16);
        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.loginType = ioBuffer.get();
        this.loginName = ioBuffer.getString(16, MessageUtil.gbkDecoder);
        this.loginPassword = ioBuffer.getString(16, MessageUtil.gbkDecoder);
        this.reserve = ioBuffer.getString(8, MessageUtil.gbkDecoder);
    }

    public byte getLoginType() {
        return this.loginType;
    }

    public void setLoginType(byte loginType) {
        this.loginType = loginType;
    }

    public String getLoginName() {
        return this.loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getLoginPassword() {
        return this.loginPassword;
    }

    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }

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

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

    public String toString() {
        return "BindMessage [head=" + this.head + ", loginName=" + this.loginName + ", loginPassword="
                + this.loginPassword + ", loginType=" + this.loginType + ", reserve=" + this.reserve + "]";
    }
}
