package com.munjaon.client.server.packet;

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

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

public final class SmsMessage {
    public static final int DELIVER_SMS_BODY_LENGTH = 239;
    public static final int DELIVER_SMS_ACK_BODY_LENGTH = 21;

    /* DELIVER */
    /* MESSAGE */
    public static final int DELIVER_MESSAGE_TRANS_LENGTH = 90;
    public static final int DELIVER_MESSAGE_LENGTH = 160;
    public static final int DELIVER_MESSAGE_POSITION = CommonMessage.DELIVER_MSG_TYPE_POSITION + CommonMessage.DELIVER_MSG_TYPE_LENGTH;

    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());
        /* MSG */
        putMessageForDeliver(buffer, data.getMessage());
    }
}
