package com.munjaon.client.server.packet;

import com.munjaon.client.model.MunjaonMsg;
import com.munjaon.client.util.CommonUtil;

import java.nio.ByteBuffer;

public final class LmsMessage {
    public static final int DELIVER_LMS_BODY_LENGTH = 2129;
    public static final int DELIVER_LMS_ACK_BODY_LENGTH = 21;

    /* DELIVER */
    /* SUBJECT */
    public static final int DELIVER_SUBJECT_LENGTH = 40;
    public static final int DELIVER_SUBJECT_POSITION = CommonMessage.DELIVER_MSG_TYPE_POSITION + CommonMessage.DELIVER_MSG_TYPE_LENGTH;
    /* MESSAGE */
    public static final int DELIVER_MESSAGE_LENGTH = 2000;
    public static final int DELIVER_MESSAGE_POSITION = DELIVER_SUBJECT_POSITION + DELIVER_SUBJECT_LENGTH;

    public static void putSubjectForDeliver(ByteBuffer buffer, String subject) {
        if (buffer == null || subject == null) {
            return;
        }
        subject = CommonUtil.cutString(subject, DELIVER_SUBJECT_LENGTH);
        buffer.put(DELIVER_SUBJECT_POSITION, subject.getBytes());
    }
    public static String getSubjectForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_SUBJECT_POSITION);
        byte[] destArray = new byte[DELIVER_SUBJECT_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void putMessageForDeliver(ByteBuffer buffer, String message) {
        if (buffer == null || message == null) {
            return;
        }
        message = CommonUtil.cutString(message, DELIVER_MESSAGE_LENGTH);
        buffer.put(DELIVER_MESSAGE_POSITION, message.getBytes());
    }
    public static String getMessageForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_MESSAGE_POSITION);
        byte[] destArray = new byte[DELIVER_MESSAGE_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }
    public static void makeDataForDeliver(ByteBuffer buffer, MunjaonMsg data) {
        if (buffer == null || data == null) {
            return;
        }
        /* MSG_ID */
        CommonMessage.putMessageIdForDeliver(buffer, data.getMsgId());
        /* SENDER */
        CommonMessage.putSenderForDeliver(buffer, data.getSendPhone());
        /* RECEIVER */
        CommonMessage.putReceiverForDeliver(buffer, data.getRecvPhone());
        /* RESERVE_TIME */
        CommonMessage.putReserveTimeForDeliver(buffer, data.getReserveDate());
        /* REQUEST_TIME */
        CommonMessage.putRequestTimeForDeliver(buffer, data.getRequestDate());
        /* MSG_TYPE */
        CommonMessage.putMsgTypeForDeliver(buffer, data.getMsgType());
        /* Subject */
        putSubjectForDeliver(buffer, data.getSubject());
        /* MSG */
        putMessageForDeliver(buffer, data.getMessage());
    }
}
