package com.munjaon.client.server.packet;

import com.munjaon.client.model.MunjaonMsg;

import java.nio.ByteBuffer;

public final class Report {
    public static final int REPORT_BODY_LENGTH = 44;

    public static final int REPORT_MSG_ID_LENGTH = 20;
    public static final int REPORT_MSG_ID_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;
    public static final int REPORT_AGENT_CODE_LENGTH = 2;
    public static final int REPORT_AGENT_CODE_POSITION = REPORT_MSG_ID_POSITION + REPORT_MSG_ID_LENGTH;
    public static final int REPORT_SEND_TIME_LENGTH = 14;
    public static final int REPORT_SEND_TIME_POSITION = REPORT_AGENT_CODE_POSITION + REPORT_AGENT_CODE_LENGTH;
    public static final int REPORT_TELECOM_LENGTH = 3;
    public static final int REPORT_TELECOM_POSITION = REPORT_SEND_TIME_POSITION + REPORT_SEND_TIME_LENGTH;
    public static final int REPORT_RESULT_LENGTH = 5;
    public static final int REPORT_RESULT_POSITION = REPORT_TELECOM_POSITION + REPORT_TELECOM_LENGTH;

    public static final int REPORT_ACK_BODY_LENGTH = 1;
    public static final int REPORT_ACK_RESULT_CODE_LENGTH = 1;
    public static final int REPORT_ACK_RESULT_CODE_POSITION = Header.BODY_POSITION + Header.BODY_LENGTH;

    public static ByteBuffer makeReport(MunjaonMsg msgData) {
        ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + REPORT_BODY_LENGTH);
        Packet.setDefaultByte(buffer);
        Header.putHeader(buffer, Header.COMMAND_REPORT, REPORT_BODY_LENGTH);
        /* 1. MSG_ID */
        makeReportForMsgId(buffer, msgData.getMsgId());
        /* 2. AGENT_CODE */
        makeReportForAgentCode(buffer, msgData.getAgentCode());
        /* 3. SEND TIME */
        makeReportForSendTime(buffer, msgData.getSendDate());
        /* 4. TELECOM */
        makeReportForTelecom(buffer, msgData.getTelecom());
        /* 5. RESULT */
        makeReportForResult(buffer, msgData.getSendStatus());

        return buffer;
    }

    public static MunjaonMsg getMsgData(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }
        MunjaonMsg msgData = new MunjaonMsg();
        msgData.setMsgId(getReportForMsgId(buffer));
        msgData.setAgentCode(getReportForAgentCode(buffer));
        msgData.setSendDate(getReportForSendTime(buffer));
        msgData.setTelecom(getReportForTelecom(buffer));
        msgData.setSendStatus(getReportForResult(buffer));

        return msgData;
    }

    public static void makeReportForMsgId(final ByteBuffer buffer, final String msgId) {
        if (buffer == null || msgId == null) {
            return;
        }

        buffer.put(REPORT_MSG_ID_POSITION, msgId.getBytes());
    }

    public static String getReportForMsgId(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(REPORT_MSG_ID_POSITION);
        byte[] destArray = new byte[REPORT_MSG_ID_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void makeReportForAgentCode(final ByteBuffer buffer, final String agentCode) {
        if (buffer == null || agentCode == null) {
            return;
        }

        buffer.put(REPORT_AGENT_CODE_POSITION, agentCode.getBytes());
    }

    public static String getReportForAgentCode(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(REPORT_AGENT_CODE_POSITION);
        byte[] destArray = new byte[REPORT_AGENT_CODE_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void makeReportForSendTime(final ByteBuffer buffer, final String sendTime) {
        if (buffer == null || sendTime == null) {
            return;
        }

        buffer.put(REPORT_SEND_TIME_POSITION, sendTime.getBytes());
    }

    public static String getReportForSendTime(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(REPORT_SEND_TIME_POSITION);
        byte[] destArray = new byte[REPORT_SEND_TIME_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void makeReportForTelecom(final ByteBuffer buffer, final String telecom) {
        if (buffer == null || telecom == null) {
            return;
        }

        buffer.put(REPORT_TELECOM_POSITION, telecom.getBytes());
    }

    public static String getReportForTelecom(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(REPORT_TELECOM_POSITION);
        byte[] destArray = new byte[REPORT_TELECOM_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static void makeReportForResult(final ByteBuffer buffer, final String result) {
        if (buffer == null || result == null) {
            return;
        }

        buffer.put(REPORT_RESULT_POSITION, result.getBytes());
    }

    public static String getReportForResult(final ByteBuffer buffer) {
        if (buffer == null) {
            return null;
        }

        buffer.position(REPORT_RESULT_POSITION);
        byte[] destArray = new byte[REPORT_RESULT_LENGTH];
        buffer.get(destArray);

        return Packet.getString(destArray);
    }

    public static ByteBuffer makeReportAckBuffer() {
        ByteBuffer buffer = ByteBuffer.allocate(Header.HEADER_LENGTH + REPORT_ACK_BODY_LENGTH);
        Packet.setDefaultByte(buffer);
        Header.putHeader(buffer, Header.COMMAND_REPORT_ACK, REPORT_ACK_BODY_LENGTH);
        buffer.put(REPORT_ACK_RESULT_CODE_POSITION, "1".getBytes());

        return buffer;
    }
}
