package com.munjaon.client.server.packet;

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

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;

public final class MmsMessage {
    public static final int LIMIT_FILE_CAPACITY = 1024 * 300;

    public static final int DELIVER_MMS_FILENAME_LENGTH = 40;
    public static final int DELIVER_MMS_FILENAME_POSITION = 0;

    public static final int DELIVER_MMS_FILESIZE_LENGTH = 8;
    public static final int DELIVER_MMS_FILESIZE_POSITION = DELIVER_MMS_FILENAME_POSITION + DELIVER_MMS_FILENAME_LENGTH;

    public static final int DELIVER_MMS_BODY_LENGTH = 2130;
    public static final int DELIVER_MMS_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;
    /* FILECOUNT */
    public static final int DELIVER_FILECOUNT_LENGTH = 1;
    public static final int DELIVER_FILECOUNT_POSITION = DELIVER_MESSAGE_POSITION + DELIVER_MESSAGE_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 putFileCountForDeliver(ByteBuffer buffer, int fileCount) {
        putFileCountForDeliver(buffer, Integer.toString(fileCount));
    }

    public static void putFileCountForDeliver(ByteBuffer buffer, String fileCount) {
        if (buffer == null || fileCount == null) {
            return;
        }
        buffer.put(DELIVER_FILECOUNT_POSITION, fileCount.getBytes());
    }
    public static String getFileCountForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_FILECOUNT_POSITION);
        byte[] destArray = new byte[DELIVER_FILECOUNT_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());
        /* FileCount */
        putFileCountForDeliver(buffer, data.getFileCount());
    }

    public static ByteBuffer makeImageForDeliver(String path, String fileName) {
        if (path == null || fileName == null) {
            return null;
        }
        File file = new File(path + fileName);
        if (file.exists() == false) {
            return null;
        }
        ByteBuffer fileHeadBuffer = ByteBuffer.allocate(MmsMessage.DELIVER_MMS_FILENAME_LENGTH + MmsMessage.DELIVER_MMS_FILESIZE_LENGTH);
        fileHeadBuffer.put(DELIVER_MMS_FILENAME_POSITION, fileName.getBytes());
        fileHeadBuffer.put(DELIVER_MMS_FILESIZE_POSITION, String.valueOf(file.length()).getBytes());
        ByteBuffer fileBodyBuffer = ByteBuffer.allocate((int) file.length());
        ByteBuffer fileBuffer = null;
        try {
            fileBodyBuffer.put(Files.readAllBytes(file.toPath()));
            fileBuffer = ByteBuffer.allocate(fileHeadBuffer.capacity() + fileBodyBuffer.capacity());
            Packet.mergeBuffers(fileBuffer, fileHeadBuffer, fileBodyBuffer);
        } catch (IOException e) {}

        return fileBuffer;
    }
}
