package com.munjaon.client.util;

import com.munjaon.client.model.MunjaonMsg;
import com.munjaon.client.server.config.ErrorCode;
import com.munjaon.client.server.packet.*;

import java.io.File;

public class MessageCheckUtil {
    public static int validateMessageForCommon(MunjaonMsg data) {
        int code = isNullMessageForCommon(data);
        if (code != ErrorCode.OK.getCode()) {
            return code;
        }

        return isLengthMessageForCommon(data);
    }
    public static int isLengthMessageForCommon(MunjaonMsg data) {
        /* MSG_ID */
        String value = data.getMsgId().trim();
        if (value.length() == 0 || value.getBytes().length > CommonMessage.DELIVER_MESSAGE_ID_LENGTH) {
            return ErrorCode.ERROR_MSGID_IS_CAPACITY.getCode();
        }
        /* SENDER */
        value = MessageUtil.doNumber(data.getSendPhone());
        if (value.getBytes().length < 8 || value.getBytes().length > CommonMessage.DELIVER_SENDER_LENGTH) {
            return ErrorCode.ERROR_SENDER_IS_CAPACITY.getCode();
        }
        /* RECEIVER */
        value = MessageUtil.doNumber(data.getRecvPhone());
        if (value.getBytes().length < 10 || value.getBytes().length > CommonMessage.DELIVER_RECEIVER_LENGTH) {
            return ErrorCode.ERROR_RECEIVER_IS_CAPACITY.getCode();
        }
        if ("01".equals(value.substring(0, 2)) == false) {
            return ErrorCode.ERROR_RECEIVER_IS_INVALID.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int isNullMessageForCommon(MunjaonMsg data) {
        if (data == null) {
            return ErrorCode.ERROR_DATA_IS_NULL.getCode();
        }
        if (data.getMsgId() == null || data.getMsgId().isEmpty()) {
            return ErrorCode.ERROR_MSGID_IS_NULL.getCode();
        }
        if (data.getSendPhone() == null || data.getSendPhone().isEmpty()) {
            return ErrorCode.ERROR_SENDER_IS_NULL.getCode();
        }
        if (data.getRecvPhone() == null || data.getRecvPhone().isEmpty()) {
            return ErrorCode.ERROR_RECEIVER_IS_NULL.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int validateMessageForSms(MunjaonMsg data) {
        if (data.getMessage() == null || data.getMessage().isEmpty()) {
            return ErrorCode.ERROR_SMS_MSG_IS_NULL.getCode();
        }
        if (data.getMessage().getBytes().length > SmsMessage.DELIVER_MESSAGE_TRANS_LENGTH) {
            return ErrorCode.ERROR_SMS_MSG_IS_CAPACITY.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int validateMessageForMedia(MunjaonMsg data) {
        if (data == null) {
            return ErrorCode.ERROR_DATA_IS_NULL.getCode();
        }
        if (data.getSubject() == null) {
            return ErrorCode.ERROR_SUBJECT_IS_NULL.getCode();
        }
        if (data.getMessage() == null) {
            return ErrorCode.ERROR_MULTI_MESSAGE_IS_NULL.getCode();
        }
        if (data.getSubject().getBytes().length > LmsMessage.DELIVER_SUBJECT_LENGTH) {
            return ErrorCode.ERROR_SUBJECT_OVER_CAPACITY.getCode();
        }
        if (data.getMessage().getBytes().length > LmsMessage.DELIVER_MESSAGE_LENGTH) {
            return ErrorCode.ERROR_MULTI_MESSAGE_OVER_CAPACITY.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int validateMessageForImage(MunjaonMsg data, String path) {
        if (imageFileCount(data) == 0) {
            return ErrorCode.ERROR_MMS_IMAGE_IS_NULL.getCode();
        }
        if (imageExtErrorCount(data) > 0) {
            return ErrorCode.ERROR_MMS_IMAGE_IS_EXT.getCode();
        }
        if (imagePathErrorCount(data, path) > 0) {
            return ErrorCode.ERROR_MMS_IMAGE_IS_NOT_PATH.getCode();
        }
        if (imageSizeErrorCount(data, path) > 0) {
            return ErrorCode.ERROR_MMS_IMAGE_IS_OVER_CAPACITY.getCode();
        }
        return ErrorCode.OK.getCode();
    }

    public static int imageSizeErrorCount(MunjaonMsg data, String path) {
        int errorCount = 0;
        if (data.getFilename01() != null) {
            File file = new File(path + data.getFilename01());
            if (file.length() > MmsMessage.LIMIT_FILE_CAPACITY) {
                errorCount++;
            }
        }
        if (data.getFilename02() != null) {
            File file = new File(path + data.getFilename02());
            if (file.length() > MmsMessage.LIMIT_FILE_CAPACITY) {
                errorCount++;
            }
        }
        if (data.getFilename03() != null) {
            File file = new File(path + data.getFilename03());
            if (file.length() > MmsMessage.LIMIT_FILE_CAPACITY) {
                errorCount++;
            }
        }
        return errorCount;
    }

    public static int imagePathErrorCount(MunjaonMsg data, String path) {
        int errorCount = 0;
        if (data.getFilename01() != null) {
            File file = new File(path + data.getFilename01());
            if (file.exists() == false) {
                errorCount++;
            }
        }
        if (data.getFilename02() != null) {
            File file = new File(path + data.getFilename02());
            if (file.exists() == false) {
                errorCount++;
            }
        }
        if (data.getFilename03() != null) {
            File file = new File(path + data.getFilename03());
            if (file.exists() == false) {
                errorCount++;
            }
        }
        return errorCount;
    }

    public static int imageExtErrorCount(MunjaonMsg data) {
        int errorCount = 0;
        if (data.getFilename01() != null && checkImageFileForExtension(data.getFilename01()) == false) {
            errorCount++;
        }
        if (data.getFilename02() != null && checkImageFileForExtension(data.getFilename02()) == false) {
            errorCount++;
        }
        if (data.getFilename03() != null && checkImageFileForExtension(data.getFilename03()) == false) {
            errorCount++;
        }
        return errorCount;
    }

    public static int imageFileCount(MunjaonMsg data) {
        if (data == null) {
            return 0;
        }
        int count = 0;
        if (data.getFilename01() != null) {
            count++;
        }
        if (data.getFilename02() != null) {
            count++;
        }
        if (data.getFilename03() != null) {
            count++;
        }

        return count;
    }

    public static boolean checkImageFileForExtension(String fileName) {
        int lastIndex = fileName.lastIndexOf(".");
        if (lastIndex == -1) {
            return false;
        }
        String extension = fileName.substring(lastIndex + 1).toUpperCase();
        if ("JPG".equals(extension) || "PNG".equals(extension) || "GIF".equals(extension) || "JPEG".equals(extension)) {
            return true;
        }

        return false;
    }

    public static int validateMessageForKakao(MunjaonMsg data) {
        if (data == null) {
            return ErrorCode.ERROR_DATA_IS_NULL.getCode();
        }
        if (data.getSubject() == null) {
            return ErrorCode.ERROR_SUBJECT_IS_NULL.getCode();
        }
        if (data.getMessage() == null) {
            return ErrorCode.ERROR_MULTI_MESSAGE_IS_NULL.getCode();
        }
        if (data.getKakaoSenderKey() == null) {
            return ErrorCode.ERROR_KAKAO_SENDER_KEY_IS_NULL.getCode();
        }
        if (data.getKakaoTemplateCode() == null) {
            return ErrorCode.ERROR_KAKAO_TEMPLATE_CODE_IS_NULL.getCode();
        }
        if (data.getKakaoJsonFile() == null) {
            return ErrorCode.ERROR_KAKAO_JSON_FILE_IS_NULL.getCode();
        }
        if (data.getSubject().getBytes().length > LmsMessage.DELIVER_SUBJECT_LENGTH) {
            return ErrorCode.ERROR_SUBJECT_OVER_CAPACITY.getCode();
        }
        if (data.getMessage().getBytes().length > LmsMessage.DELIVER_MESSAGE_LENGTH) {
            return ErrorCode.ERROR_MULTI_MESSAGE_OVER_CAPACITY.getCode();
        }
        if (data.getKakaoSenderKey().getBytes().length > KakaoMessage.DELIVER_KAKAO_SENDER_KEY_LENGTH) {
            return ErrorCode.ERROR_KAKAO_SENDER_KEY_OVER_CAPACITY.getCode();
        }
        if (data.getKakaoTemplateCode().getBytes().length > KakaoMessage.DELIVER_KAKAO_TEMPLATE_CODE_LENGTH) {
            return ErrorCode.ERROR_KAKAO_TEMPLATE_CODE_OVER_CAPACITY.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int validateJsonFile(String path, String fileName) {
        int lastIndex = fileName.lastIndexOf(".");
        if (lastIndex == -1) {
            return ErrorCode.ERROR_KAKAO_JSON_FILE_IS_EXT.getCode();
        }
        String extension = fileName.substring(lastIndex + 1).toUpperCase();
        if ("JSON".equals(extension) == false) {
            return ErrorCode.ERROR_KAKAO_JSON_FILE_IS_EXT.getCode();
        }
        File file = new File(path + fileName);
        if (file.exists() == false) {
            return ErrorCode.ERROR_KAKAO_JSON_FILE_IS_NOT_PATH.getCode();
        }
        if (file.length() > KakaoMessage.LIMIT_FILE_CAPACITY) {
            return ErrorCode.ERROR_KAKAO_JSON_FILE_IS_OVER_CAPACITY.getCode();
        }

        return ErrorCode.OK.getCode();
    }

    public static int validateMessageForJson(MunjaonMsg data) {
        return ErrorCode.OK.getCode();
    }

    public static MunjaonMsg setReportMessage(String msgId, String agentCode, String sendStatus, String sendDate, String telecom) {
        MunjaonMsg msg = new MunjaonMsg();
        msg.setMsgId(msgId);
        msg.setAgentCode(agentCode);
        msg.setSendStatus(sendStatus);
        msg.setSendDate(sendDate);
        msg.setTelecom(telecom);

        return msg;
    }


    public static void main(String[] args) {
        String fileName = " .dat";
        int lastIndex = fileName.lastIndexOf(".");
        if (lastIndex < 0) {
            System.out.println("EXT is not ");
        } else {
            System.out.println("EXT : " + fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()));
        }
        System.out.println("NM :" + fileName.substring(0, lastIndex) + "<<");
    }
}
