package com.munjaon.client.server.packet;

import com.munjaon.client.util.ByteUtil;

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

public final class Header {
    public static final int HEADER_LENGTH = 10;

    public static final String VERSION = "ITN10";
    public static final int VERSION_LENGTH = 5;
    public static final int VERSION_POSITION = 0;

    public static final int COMMAND_LENGTH = 1;
    public static final int COMMAND_POSITION = VERSION_POSITION + VERSION_LENGTH;

    public static final String COMMAND_BIND = "1";
    public static final String COMMAND_BIND_ACK = "2";
    public static final String COMMAND_DELIVER = "3";
    public static final String COMMAND_DELIVER_ACK = "4";
    public static final String COMMAND_REPORT = "5";
    public static final String COMMAND_REPORT_ACK = "6";
    public static final String COMMAND_LINK_CHECK = "7";
    public static final String COMMAND_LINK_CHECK_ACK = "8";

    public static final int BODY_LENGTH = 4;
    public static final int BODY_POSITION = COMMAND_POSITION + COMMAND_LENGTH;

    public static final int BODY_BIND_LENGTH = 41;
    public static final int BODY_BIND_ACK_LENGTH = 2;
    public static final int BODY_LINK_CHECK_LENGTH = 3;
    public static final int BODY_LINK_CHECK_ACK_LENGTH = 3;
    public static final int BODY_DELIVER_SMS_LENGTH = 239;
    public static final int BODY_DELIVER_SMS_ACK_LENGTH = 21;
    public static final int BODY_DELIVER_LMS_LENGTH = 2091;
    public static final int BODY_DELIVER_LMS_ACK_LENGTH = 21;
    public static final int BODY_DELIVER_MMS_LENGTH = 2091;
    public static final int BODY_DELIVER_MMS_ACK_LENGTH = 21;
    public static final int BODY_REPORT_LENGTH = 58;
    public static final int BODY_REPORT_ACK_LENGTH = 1;

    public static void putVersion(final ByteBuffer buffer) {
        if (buffer == null) {
            return;
        }
        buffer.put(VERSION_POSITION, VERSION.getBytes());
    }
    public static String getVersion(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(VERSION_POSITION);
        byte[] destArray = new byte[VERSION_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putCommand(final ByteBuffer buffer, String command) {
        if (buffer == null) {
            return;
        }
        buffer.put(COMMAND_POSITION, command.getBytes());
    }
    public static String getCommand(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(COMMAND_POSITION);
        byte[] destArray = new byte[COMMAND_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putBodyLength(final ByteBuffer buffer, int bodyLength) {
        putBodyLength(buffer, Integer.toString(bodyLength));
    }
    public static String getBodyLength(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(BODY_POSITION);
        byte[] destArray = new byte[BODY_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putBodyLength(final ByteBuffer buffer, String bodyLength) {
        if (buffer == null) {
            return;
        }
        System.out.println(ByteUtil.byteToHex(bodyLength.getBytes(), true));

        buffer.put(BODY_POSITION, bodyLength.getBytes());
    }
    public static void putHeader(final ByteBuffer buffer, String command, int bodyLength) {
        putHeader(buffer, command, Integer.toString(bodyLength));
    }
    public static void putHeader(final ByteBuffer buffer, String command, String bodyLength) {
        putVersion(buffer);
        putCommand(buffer, command);
        putBodyLength(buffer, bodyLength);
    }
}
