package com.munjaon.client.server.packet;

import java.nio.ByteBuffer;
import java.util.Arrays;

public final class Bind {
    public static final int BIND_BODY_LENGTH = 41;

    public static final int BIND_ID_LENGTH = 20;
    public static final int BIND_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;
    public static final int BIND_PWD_LENGTH = 20;
    public static final int BIND_PWD_POSITION = BIND_ID_POSITION + BIND_ID_LENGTH;
    public static final int BIND_ENCRYPTION_LENGTH = 1;
    public static final int BIND_ENCRYPTION_POSITION = BIND_PWD_POSITION + BIND_PWD_LENGTH;

    public static final int BIND_ACK_BODY_LENGTH = 2;
    public static final int BIND_ACK_RESULT_CODE_LENGTH = 2;
    public static final int BIND_ACK_RESULT_CODE_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;

    public static final String ENCRYPTION = "0";

    public static ByteBuffer makeBindBuffer(String id, String pwd) {
        ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_BODY_LENGTH);
        Packet.setDefaultByte(buffer);
        Header.putHeader(buffer, Header.COMMAND_BIND, BIND_BODY_LENGTH);
        /* ID */
        if (id != null) {
            buffer.put(BIND_ID_POSITION, id.getBytes());
        }
        /* PWD */
        if (pwd != null) {
            buffer.put(BIND_PWD_POSITION, pwd.getBytes());
        }
        /* ENCRYPTION */
        buffer.put(BIND_ENCRYPTION_POSITION, ENCRYPTION.getBytes());
//        buffer.limit(buffer.capacity());

        return buffer;
    }

    public static ByteBuffer makeBindAckBuffer(String resultCode) {
        ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + BIND_ACK_BODY_LENGTH);
        Packet.setDefaultByte(buffer);
        Header.putHeader(buffer, Header.COMMAND_BIND_ACK, BIND_ACK_BODY_LENGTH);
        /* resultCode */
        if (resultCode != null) {
            buffer.put(BIND_ACK_RESULT_CODE_POSITION, resultCode.getBytes());
        }

        return buffer;
    }

    public static String getBindId(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(BIND_ID_POSITION);
        byte[] destArray = new byte[BIND_ID_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static String getBindPwd(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(BIND_PWD_POSITION);
        byte[] destArray = new byte[BIND_PWD_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static String getBindEncryption(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(BIND_ENCRYPTION_POSITION);
        byte[] destArray = new byte[BIND_ENCRYPTION_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static String getBindAckResultCode(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(BIND_ACK_RESULT_CODE_POSITION);
        byte[] destArray = new byte[BIND_ACK_RESULT_CODE_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }
}
