package com.munjaon.client.server.service;

import com.munjaon.client.model.MunjaonMsg;
import com.munjaon.client.server.config.ErrorCode;
import com.munjaon.client.server.packet.*;
import com.munjaon.client.service.DatabaseTypeWorker;
import com.munjaon.client.util.MessageCheckUtil;
import com.munjaon.client.util.MessageUtil;
import org.json.simple.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class CollectClientService extends Service {
    private final String serviceType;
    private DatabaseTypeWorker worker;
    private SocketChannel socketChannel;
    private long lastPacketSendTime = 0;
    private String address;
    private int port;
    private String id;
    private String pwd;

    private StringBuilder deliverBuilder;
    private List<String> deliverList = null;

    public CollectClientService(String serviceName, String serviceType) {
        super(serviceName);
        this.serviceType = serviceType;
    }

    @Override
    public void checkReady() {
        worker = DatabaseTypeWorker.find(System.getProperty("DBMS"));
        if (worker == null) {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is not ready]");
            return;
        }

        this.IS_READY_YN = worker.checkTableExists();
        if (this.IS_READY_YN) {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is ready]");
        } else {
            saveSystemLog("[checkReady][DBMS : " + System.getProperty("DBMS") + " is not ready]");
        }
    }

    @Override
    public void initResources() {
        saveSystemLog("Resource is initializing ... ...");
        this.address = getProp("HOST");
        this.port = Integer.parseInt(getProp("DELIVER.PORT"));
        this.id = getProp("ID");
        this.pwd = getProp("PASSWORD");
        saveSystemLog("Try Connect to [ADDRESS : " + this.address + "] [PORT : " + this.port + "] [ID : " + this.id + "]");
        try {
            socketChannel = SocketChannel.open(new InetSocketAddress(this.address, this.port));
            socketChannel.configureBlocking(false);
        } catch (IOException e) {
            saveSystemLog("Connect Fail to [ADDRESS : " + this.address + "] [PORT : " + this.port + "] [ID : " + this.id + "]");
            saveSystemLog("ERROR [" + e.getMessage() + "]");
            throw new RuntimeException(e);
        }
    }

    @Override
    public void releaseResources() {
        if (socketChannel != null) {
            try {
                socketChannel.close();
            } catch (IOException e) {
                saveSystemLog("ERROR [" + e.getMessage() + "]");
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void doService() {
        bind();
        while (isRun()) {
            try {
//                saveSystemLog("Root Path : " + System.getProperty("ROOTPATH"));
                messageService();
                linkCheckService();
            } catch (Exception e) {
                saveSystemLog("ERROR [" + e.getMessage() + "]");
                saveSystemLog("ERROR DETAIL");
                saveSystemLog(e.toString());
                throw new RuntimeException(e);
            }
        }
    }

    private void bind() {
        ByteBuffer sendBuffer = Bind.makeBindBuffer(id, pwd);
        ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + Bind.BIND_ACK_BODY_LENGTH);
        try {
            saveSystemLog("[BIND REQUEST] [IP : " + this.address + "] [PORT : " + this.port + "] [ID : " + this.id + "]");
            socketChannel.write(sendBuffer);
            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[BIND ERROR] [CONNECTION CLOSED]");
                    throw new RuntimeException("BIND ERROR");
                } else if (recvCount > 0) {
                    String resultCode = Bind.getBindAckResultCode(recvBuffer);
                    saveSystemLog("resultCode " + resultCode);
                    if ("00".equals(resultCode)) {
                        lastPacketSendTime = System.currentTimeMillis();
                        saveSystemLog("[BIND SUCCESS] [CONNECTION IS ESTABLISHED]");
                        break;
                    } else {
                        saveSystemLog("[BIND FAIL] [RESULT CODE : " + resultCode + "]");
                        throw new RuntimeException("Bind FAIL");
                    }
                }
            }
        } catch (Exception e) {
            saveSystemLog("ERROR [" + e.getMessage() + "]");
            saveSystemLog("ERROR DETAIL");
            saveSystemLog(e.toString());
            throw new RuntimeException(e);
        }
    }

    private List<MunjaonMsg> selectToDeliver() {
        if ("SMS".equals(this.serviceType)) {
            return worker.selectToDeliver("S");
        } else if ("LMS".equals(this.serviceType)) {
            return worker.selectToDeliver("L");
        } else if ("MMS".equals(this.serviceType)) {
            return worker.selectToDeliver("M");
        } else if ("KAT".equals(this.serviceType)) {
            return worker.selectToDeliver("A");
        } else if ("KFT".equals(this.serviceType)) {
            return worker.selectToDeliver("F");
        }

        return null;
    }

    private void setDeliverMsgId(String msgId) {
        if (msgId == null) {
            return;
        }
        if (this.deliverList == null) {
            this.deliverList = new ArrayList<>();
        }
        this.deliverList.add(msgId);
//        if (this.deliverBuilder == null) {
//            return;
//        }
//
//        if (this.deliverBuilder.isEmpty()) {
//            this.deliverBuilder.append(msgId);
//        } else {
//            this.deliverBuilder.append(",").append(msgId);
//        }
    }

    private void messageService() {
        List<MunjaonMsg> list = selectToDeliver();
        this.deliverBuilder = new StringBuilder();
        if (list == null || list.isEmpty()) {
            try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}
            return;
        }

        for (MunjaonMsg data : list) {
            switch (this.serviceType) {
                case "SMS": smsMessageService(data); break;
                case "LMS": lmsMessageService(data); break;
                case "MMS": mmsMessageService(data); break;
                case "KAT": katMessageService(data); break;
                case "KFT": kftMessageService(data); break;
                default:break;
            }
        }
        if (this.deliverList != null && this.deliverList.size() > 0) {
            worker.updateDeliverForList(this.deliverList);
            this.deliverList = null;    // NULL로 초기화
        }
    }

    private void smsMessageService(MunjaonMsg data) {
        try {
            /* 공통 메시지 유효성 체크 */
            int checkCommonCode = MessageCheckUtil.validateMessageForCommon(data);
            int checkMsgCode = MessageCheckUtil.validateMessageForSms(data);
            if (checkCommonCode != ErrorCode.OK.getCode() || checkMsgCode != ErrorCode.OK.getCode()) {
                saveSystemLog("[MESSAGE FILTER] [COMMON_CODE : " + checkCommonCode + "] [MSG_CODE : " + checkMsgCode + "]");
                saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
                /* 전송처리 */
                worker.updateToDeliver(data.getMsgId());
                /* 실패처리 */
                MunjaonMsg errorMsg = null;
                if (checkCommonCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkCommonCode), MessageUtil.getTime(), "ETC");;
                } else {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkMsgCode), MessageUtil.getTime(), "ETC");;
                }
                worker.updateToReport(errorMsg);
                /* 처리완료 */
                return;
            }

            /* 정상인 경우 메시지 전송 */
            ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + SmsMessage.DELIVER_SMS_BODY_LENGTH);
            ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + SmsMessage.DELIVER_SMS_ACK_BODY_LENGTH);
            Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, SmsMessage.DELIVER_SMS_BODY_LENGTH);
            SmsMessage.makeDataForDeliver(sendBuffer, data);
            saveSystemLog("[MESSAGE SEND] [... ...]");
            saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
            socketChannel.write(sendBuffer);
            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[MESSAGE SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("DELIVER ERROR");
                } else if (recvCount > 0) {
                    setDeliverMsgId(data.getMsgId());
//                    worker.updateToDeliver(data.getMsgId());
                    saveSystemLog("[MESSAGE SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
//            throw new RuntimeException(e);
        }
    }

    private void lmsMessageService(MunjaonMsg data) {
        try {
            /* 공통 메시지 유효성 체크 */
            int checkCommonCode = MessageCheckUtil.validateMessageForCommon(data);
            int checkMsgCode = MessageCheckUtil.validateMessageForMedia(data);
            if (checkCommonCode != ErrorCode.OK.getCode() || checkMsgCode != ErrorCode.OK.getCode()) {
                saveSystemLog("[MESSAGE FILTER] [COMMON_CODE : " + checkCommonCode + "] [MSG_CODE : " + checkMsgCode + "]");
                saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
                /* 전송처리 */
                worker.updateToDeliver(data.getMsgId());
                /* 실패처리 */
                MunjaonMsg errorMsg = null;
                if (checkCommonCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkCommonCode), MessageUtil.getTime(), "ETC");;
                } else {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkMsgCode), MessageUtil.getTime(), "ETC");;
                }
                worker.updateToReport(errorMsg);
                /* 처리완료 */
                return;
            }

            /* 정상인 경우 메시지 전송 */
            ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LmsMessage.DELIVER_LMS_BODY_LENGTH);
            ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LmsMessage.DELIVER_LMS_ACK_BODY_LENGTH);
            Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, LmsMessage.DELIVER_LMS_BODY_LENGTH);
            LmsMessage.makeDataForDeliver(sendBuffer, data);
            saveSystemLog("[MESSAGE SEND] [... ...]");
            saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
            socketChannel.write(sendBuffer);
            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[MESSAGE SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("DELIVER ERROR");
                } else if (recvCount > 0) {
                    worker.updateToDeliver(data.getMsgId());
                    saveSystemLog("[MESSAGE SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void mmsMessageService(MunjaonMsg data) {
        try {
            /* MMS Image 저장 경로 */
            String path = System.getProperty("ROOTPATH") + File.separator + "mmsfile" + File.separator;
            /* 공통 메시지 유효성 체크 */
            int checkCommonCode = MessageCheckUtil.validateMessageForCommon(data);
            int checkMsgCode = MessageCheckUtil.validateMessageForMedia(data);
            int checkImageCode = MessageCheckUtil.validateMessageForImage(data, path);
            if (checkCommonCode != ErrorCode.OK.getCode() || checkMsgCode != ErrorCode.OK.getCode() || checkImageCode != ErrorCode.OK.getCode()) {
                saveSystemLog("[MESSAGE FILTER] [COMMON_CODE : " + checkCommonCode + "] [MSG_CODE : " + checkMsgCode + "] [IMAGE_CODE : " + checkImageCode + "]");
                saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
                /* 전송처리 */
                worker.updateToDeliver(data.getMsgId());
                /* 실패처리 */
                MunjaonMsg errorMsg = null;
                if (checkCommonCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkCommonCode), MessageUtil.getTime(), "ETC");
                }else if (checkMsgCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkMsgCode), MessageUtil.getTime(), "ETC");
                } else {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkImageCode), MessageUtil.getTime(), "ETC");
                }
                worker.updateToReport(errorMsg);
                /* 처리완료 */
                return;
            }

            /* 정상인 경우 메시지 전송 */
            ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + MmsMessage.DELIVER_MMS_BODY_LENGTH);
            ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + MmsMessage.DELIVER_MMS_ACK_BODY_LENGTH);
            int fileCount = 0;
            /* File check */
            ByteBuffer file01Buffer = MmsMessage.makeImageForDeliver(path, data.getFilename01());
            if (file01Buffer != null) {
                saveSystemLog("file01Buffer : " + file01Buffer.capacity());
            }
            if (file01Buffer != null) {
                fileCount++;
            }
            ByteBuffer file02Buffer = MmsMessage.makeImageForDeliver(path, data.getFilename02());
            if (file02Buffer != null) {
                saveSystemLog("file02Buffer : " + file02Buffer.capacity());
            }
            if (file02Buffer != null) {
                fileCount++;
            }
            ByteBuffer file03Buffer = MmsMessage.makeImageForDeliver(path, data.getFilename03());
            if (file03Buffer != null) {
                saveSystemLog("file03Buffer : " + file03Buffer.capacity());
            }
            if (file03Buffer != null) {
                fileCount++;
            }
            /* fileCount 저장 */
            data.setFileCount(fileCount);

            Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, MmsMessage.DELIVER_MMS_BODY_LENGTH);
            MmsMessage.makeDataForDeliver(sendBuffer, data);
            saveSystemLog("[MESSAGE SEND] [... ...]");
            saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");

            ByteBuffer[] byteBuffers = new ByteBuffer[fileCount + 1];
            int index = 0;
            byteBuffers[index] = sendBuffer;
            index++;
            if (file01Buffer != null) {
                byteBuffers[index] = file01Buffer;
                index++;
            }
            if (file02Buffer != null) {
                byteBuffers[index] = file02Buffer;
                index++;
            }
            if (file03Buffer != null) {
                byteBuffers[index] = file03Buffer;
                index++;
            }

            socketChannel.write(byteBuffers);

            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[MESSAGE SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("DELIVER ERROR");
                } else if (recvCount > 0) {
                    worker.updateToDeliver(data.getMsgId());
                    saveSystemLog("[MESSAGE SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private void katMessageService(MunjaonMsg data) {
        try {
            /* Kakao Json 저장 경로 */
            String path = System.getProperty("ROOTPATH") + File.separator + "kakaofile" + File.separator;
            /* 공통 메시지 유효성 체크 */
            int checkCommonCode = MessageCheckUtil.validateMessageForCommon(data);
            int checkMsgCode = MessageCheckUtil.validateMessageForKakao(data);
            int checkFileCode = MessageCheckUtil.validateJsonFile(path, data.getKakaoJsonFile());

            if (checkCommonCode != ErrorCode.OK.getCode() || checkMsgCode != ErrorCode.OK.getCode() || checkFileCode != ErrorCode.OK.getCode()) {
                saveSystemLog("[MESSAGE FILTER] [COMMON_CODE : " + checkCommonCode + "] [MSG_CODE : " + checkMsgCode + "] [JSON_FILE_CODE : " + checkFileCode + "]");
                saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
                /* 전송처리 */
                worker.updateToDeliver(data.getMsgId());
                /* 실패처리 */
                MunjaonMsg errorMsg = null;
                if (checkCommonCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkCommonCode), MessageUtil.getTime(), "ETC");
                }else if (checkMsgCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkMsgCode), MessageUtil.getTime(), "ETC");
                } else {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkFileCode), MessageUtil.getTime(), "ETC");
                }
                worker.updateToReport(errorMsg);
                /* 처리완료 */
                return;
            }

            /* 정상인 경우 메시지 전송 */
            ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + KakaoMessage.DELIVER_KAKAO_BODY_LENGTH);
            ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + KakaoMessage.DELIVER_KAKAO_ACK_BODY_LENGTH);
            /* File check */
            ByteBuffer fileBuffer = KakaoMessage.makeJsonForDeliver(path, data.getKakaoJsonFile());

            Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, KakaoMessage.DELIVER_KAKAO_BODY_LENGTH);
            KakaoMessage.makeDataForDeliver(sendBuffer, data);
            saveSystemLog("[MESSAGE SEND] [... ...]");
            saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");

            ByteBuffer[] byteBuffers = new ByteBuffer[2];
            byteBuffers[0] = sendBuffer;
            byteBuffers[1] = fileBuffer;

            socketChannel.write(byteBuffers);

            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[MESSAGE SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("DELIVER ERROR");
                } else if (recvCount > 0) {
                    worker.updateToDeliver(data.getMsgId());
                    saveSystemLog("[MESSAGE SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private void kftMessageService(MunjaonMsg data) {
        try {
            /* Kakao Json 저장 경로 */
            String path = System.getProperty("ROOTPATH") + File.separator + "kakaofile" + File.separator;
            /* 공통 메시지 유효성 체크 */
            int checkCommonCode = MessageCheckUtil.validateMessageForCommon(data);
            int checkMsgCode = MessageCheckUtil.validateMessageForKakao(data);
            int checkFileCode = MessageCheckUtil.validateJsonFile(path, data.getKakaoJsonFile());
            if (checkCommonCode != ErrorCode.OK.getCode() || checkMsgCode != ErrorCode.OK.getCode() || checkFileCode != ErrorCode.OK.getCode()) {
                saveSystemLog("[MESSAGE FILTER] [COMMON_CODE : " + checkCommonCode + "] [MSG_CODE : " + checkMsgCode + "] [JSON_FILE_CODE : " + checkFileCode + "]");
                saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");
                /* 전송처리 */
                worker.updateToDeliver(data.getMsgId());
                /* 실패처리 */
                MunjaonMsg errorMsg = null;
                if (checkCommonCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkCommonCode), MessageUtil.getTime(), "ETC");
                }else if (checkMsgCode != ErrorCode.OK.getCode()) {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkMsgCode), MessageUtil.getTime(), "ETC");
                } else {
                    errorMsg = MessageCheckUtil.setReportMessage(data.getMsgId(), "00", String.valueOf(checkFileCode), MessageUtil.getTime(), "ETC");
                }
                worker.updateToReport(errorMsg);
                /* 처리완료 */
                return;
            }

            /* 정상인 경우 메시지 전송 */
            ByteBuffer sendBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + KakaoMessage.DELIVER_KAKAO_BODY_LENGTH);
            ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + KakaoMessage.DELIVER_KAKAO_ACK_BODY_LENGTH);
            /* File check */
            ByteBuffer fileBuffer = KakaoMessage.makeJsonForDeliver(path, data.getKakaoJsonFile());

            Header.putHeader(sendBuffer, Header.COMMAND_DELIVER, KakaoMessage.DELIVER_KAKAO_BODY_LENGTH);
            KakaoMessage.makeDataForDeliver(sendBuffer, data);
            saveSystemLog("[MESSAGE SEND] [... ...]");
            saveSystemLog("[MESSAGE DATA : " + data.toString() + "]");

            ByteBuffer[] byteBuffers = new ByteBuffer[2];
            byteBuffers[0] = sendBuffer;
            byteBuffers[1] = fileBuffer;

            socketChannel.write(byteBuffers);

            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[MESSAGE SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("DELIVER ERROR");
                } else if (recvCount > 0) {
                    worker.updateToDeliver(data.getMsgId());
                    saveSystemLog("[MESSAGE SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private void linkCheckService() {
        if (System.currentTimeMillis() - lastPacketSendTime < Packet.LINK_CHECK_CYCLE) {
//            saveSystemLog("LinkCheck Is Not");
            return;
        }

        ByteBuffer recvBuffer = ByteBuffer.allocate(Header.HEADER_LENGTH + LinkCheck.LINK_CHECK_ACK_BODY_LENGTH);
        try {
            saveSystemLog("[LINK_CHECK SEND] [... ...]");
            socketChannel.write(LinkCheck.makeLinkCheckBuffer());
            while (true) {
                int recvCount = socketChannel.read(recvBuffer);
                if (recvCount == -1) {
                    saveSystemLog("[LINK_CHECK SEND] [FAIL] [SOCKET IS CLOSED]");
                    throw new RuntimeException("LINK_CHECK ERROR");
                } else if (recvCount > 0) {
                    saveSystemLog("[LINK_CHECK SEND] [SUCCESS]");
                    lastPacketSendTime = System.currentTimeMillis();
                    break;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public JSONObject monitorService() {
        return null;
    }
}
