package com.munjaon.client.server.packet;

import com.munjaon.client.util.CommonUtil;

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

public final class CommonMessage {
    /* DELIVER */
    /* MSG_ID */
    public static final int DELIVER_MESSAGE_ID_LENGTH = 20;
    public static final int DELIVER_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;
    /* SENDER */
    public static final int DELIVER_SENDER_LENGTH = 15;
    public static final int DELIVER_SENDER_POSITION = DELIVER_MESSAGE_ID_POSITION + DELIVER_MESSAGE_ID_LENGTH;
    /* RECEIVER */
    public static final int DELIVER_RECEIVER_LENGTH = 15;
    public static final int DELIVER_RECEIVER_POSITION = DELIVER_SENDER_POSITION + DELIVER_SENDER_LENGTH;
    /* RESERVE_TIME */
    public static final int DELIVER_RESERVE_TIME_LENGTH = 14;
    public static final int DELIVER_RESERVE_TIME_POSITION = DELIVER_RECEIVER_POSITION + DELIVER_RECEIVER_LENGTH;
    /* REQUEST_TIME */
    public static final int DELIVER_REQUEST_TIME_LENGTH = 14;
    public static final int DELIVER_REQUEST_TIME_POSITION = DELIVER_RESERVE_TIME_POSITION + DELIVER_RESERVE_TIME_LENGTH;
    /* MSG_TYPE */
    public static final int DELIVER_MSG_TYPE_LENGTH = 1;
    public static final int DELIVER_MSG_TYPE_POSITION = DELIVER_REQUEST_TIME_POSITION + DELIVER_REQUEST_TIME_LENGTH;

    /* DELIVER_ACK */
    /* MSG_ID */
    public static final int DELIVER_ACK_MESSAGE_ID_LENGTH = 20;
    public static final int DELIVER_ACK_MESSAGE_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;
    /* RESULT */
    public static final int DELIVER_ACK_RESULT_LENGTH = 1;
    public static final int DELIVER_ACK_RESULT_POSITION = DELIVER_ACK_MESSAGE_ID_POSITION + DELIVER_ACK_MESSAGE_ID_LENGTH;

    public static void putMessageIdForDeliver(ByteBuffer buffer, String messageId) {
        if (buffer == null || messageId == null) {
            return;
        }
        buffer.put(DELIVER_MESSAGE_ID_POSITION, messageId.getBytes());
    }
    public static String getMessageIdForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_MESSAGE_ID_POSITION);
        byte[] destArray = new byte[DELIVER_MESSAGE_ID_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putSenderForDeliver(ByteBuffer buffer, String sender) {
        if (buffer == null || sender == null) {
            return;
        }
        sender = CommonUtil.cutString(CommonUtil.doNumber(sender), DELIVER_SENDER_LENGTH);
        buffer.put(DELIVER_SENDER_POSITION, sender.getBytes());
    }
    public static String getSenderForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_SENDER_POSITION);
        byte[] destArray = new byte[DELIVER_SENDER_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putReceiverForDeliver(ByteBuffer buffer, String receiver) {
        if (buffer == null || receiver == null) {
            return;
        }
        receiver = CommonUtil.cutString(CommonUtil.doNumber(receiver), DELIVER_RECEIVER_LENGTH);
        buffer.put(DELIVER_RECEIVER_POSITION, receiver.getBytes());
    }
    public static String getReceiverForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_RECEIVER_POSITION);
        byte[] destArray = new byte[DELIVER_RECEIVER_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putReserveTimeForDeliver(ByteBuffer buffer, String reserveTime) {
        if (buffer == null || reserveTime == null) {
            return;
        }
        buffer.put(DELIVER_RESERVE_TIME_POSITION, reserveTime.getBytes());
    }
    public static String getReserveTimeForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_RESERVE_TIME_POSITION);
        byte[] destArray = new byte[DELIVER_RESERVE_TIME_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putRequestTimeForDeliver(ByteBuffer buffer, String requestTime) {
        if (buffer == null || requestTime == null) {
            return;
        }
        buffer.put(DELIVER_REQUEST_TIME_POSITION, requestTime.getBytes());
    }
    public static String getRequestTimeForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_REQUEST_TIME_POSITION);
        byte[] destArray = new byte[DELIVER_REQUEST_TIME_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putMsgTypeForDeliver(ByteBuffer buffer, String msgType) {
        if (buffer == null || msgType == null) {
            return;
        }
        buffer.put(DELIVER_MSG_TYPE_POSITION, msgType.getBytes());
    }
    public static String getMsgTypeForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_MSG_TYPE_POSITION);
        byte[] destArray = new byte[DELIVER_MSG_TYPE_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }


    public static void putMessageIdForDeliverAck(ByteBuffer buffer, String messageId) {
        if (buffer == null || messageId == null) {
            return;
        }
        buffer.put(DELIVER_ACK_MESSAGE_ID_POSITION, messageId.getBytes());
    }
    public static String getMessageIdForDeliverAck(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_ACK_MESSAGE_ID_POSITION);
        byte[] destArray = new byte[DELIVER_ACK_MESSAGE_ID_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putResultForDeliverAck(ByteBuffer buffer, String result) {
        if (buffer == null || result == null) {
            return;
        }
        buffer.put(DELIVER_ACK_RESULT_POSITION, result.getBytes());
    }
    public static String getResultForDeliverAck(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_ACK_RESULT_POSITION);
        byte[] destArray = new byte[DELIVER_ACK_RESULT_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }
}
