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 KakaoMessage {
    public static final int LIMIT_FILE_CAPACITY = 1024 * 50;

    public static final int DELIVER_JSON_FILENAME_LENGTH = 40;
    public static final int DELIVER_JSON_FILENAME_POSITION = 0;

    public static final int DELIVER_JSON_FILESIZE_LENGTH = 8;
    public static final int DELIVER_JSON_FILESIZE_POSITION = DELIVER_JSON_FILENAME_POSITION + DELIVER_JSON_FILENAME_LENGTH;

    public static final int DELIVER_KAKAO_BODY_LENGTH = 2233;
    public static final int DELIVER_KAKAO_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;
    /* KAKAO_SENDER_KEY */
    public static final int DELIVER_KAKAO_SENDER_KEY_LENGTH = 40;
    public static final int DELIVER_KAKAO_SENDER_KEY_POSITION = DELIVER_MESSAGE_POSITION + DELIVER_MESSAGE_LENGTH;
    /* KAKAO_TEMPLATE_CODE */
    public static final int DELIVER_KAKAO_TEMPLATE_CODE_LENGTH = 64;
    public static final int DELIVER_KAKAO_TEMPLATE_CODE_POSITION = DELIVER_KAKAO_SENDER_KEY_POSITION + DELIVER_KAKAO_SENDER_KEY_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 putKakaoSenderKeyForDeliver(ByteBuffer buffer, String kakaoSenderKey) {
        if (buffer == null || kakaoSenderKey == null) {
            return;
        }
        kakaoSenderKey = CommonUtil.cutString(kakaoSenderKey, DELIVER_KAKAO_SENDER_KEY_LENGTH);
        buffer.put(DELIVER_KAKAO_SENDER_KEY_POSITION, kakaoSenderKey.getBytes());
    }
    public static String getKakaoSenderKeyForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_KAKAO_SENDER_KEY_POSITION);
        byte[] destArray = new byte[DELIVER_KAKAO_SENDER_KEY_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }
    public static void putKakaoTemplateCodeForDeliver(ByteBuffer buffer, String kakaoTemplateCode) {
        if (buffer == null || kakaoTemplateCode == null) {
            return;
        }
        kakaoTemplateCode = CommonUtil.cutString(kakaoTemplateCode, DELIVER_KAKAO_TEMPLATE_CODE_LENGTH);
        buffer.put(DELIVER_KAKAO_TEMPLATE_CODE_POSITION, kakaoTemplateCode.getBytes());
    }
    public static String getKakaoTemplateCodeForDeliver(ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(DELIVER_KAKAO_TEMPLATE_CODE_POSITION);
        byte[] destArray = new byte[DELIVER_KAKAO_TEMPLATE_CODE_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());
        /* KAKAO_SENDER_KEY */
        putKakaoSenderKeyForDeliver(buffer, data.getKakaoSenderKey());
        /* KAKAO_TEMPLATE_CODE */
        putKakaoTemplateCodeForDeliver(buffer, data.getKakaoTemplateCode());
    }

    public static ByteBuffer makeJsonForDeliver(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_JSON_FILENAME_POSITION, fileName.getBytes());
        fileHeadBuffer.put(DELIVER_JSON_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;
    }
}
