package kr.xit.batch.ens.task.cmm;

import static egovframework.com.cmm.util.EgovStringUtil.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.MDC;

import kr.xit.biz.common.ApiConstants;
import kr.xit.biz.ens.model.cmm.SndngMssageParam;
import kr.xit.core.biz.batch.model.BatchCmmDTO.BatchLock;
import kr.xit.core.spring.util.BatchSpringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
 * <pre>
 * description :
 *
 * packageName : kr.xit.batch.ens.task.cmm
 * fileName    : TaskCmmUtils
 * author      : limju
 * date        : 2023-06-19
 * ======================================================================
 * 변경일         변경자        변경 내용
 * ----------------------------------------------------------------------
 * 2023-06-19    limju       최초 생성
 *
 * </pre>
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TaskCmmUtils {
    public static void taskBatchErrorLog(final String instanceId, final String errorMsg, final String isSlackEnabled){
        BatchSpringUtils.getBatchCmmService().modifyBatchLog(
                BatchLock.builder()
                    .batchLogId(MDC.get("batch_log_id"))
                    .instanceId(instanceId)
                    .traceId(MDC.get("request_trace_id"))
                    .message(errorMsg)
                    .build()
        );
        if(Boolean.parseBoolean(isSlackEnabled)) {
            BatchSpringUtils.getSlackWebhookPush().sendSlackAlertLog(
                    errorMsg,
                    MDC.get("uri"),
                    "batch call");
        }
    }

    /**
     * 배치 실행시 발생한 에러 처리
     * <p>업무단에서 발생한 에러 처리를 위한 메소드
     * @param instanceId String 배치인스턴스
     * @param errorMsg String 에러 메세지
     * @param isSlackEnabled boolean 슬랙 push 여부
     */
    public static void taskEnsBatchServiceUpdateErrorLog(final String instanceId, final String errorMsg, final String isSlackEnabled){
        final String UNITY_SNDNG_MASTR_ID = "unitySndngMastrId";

        String unitySndngMastrId = null;
        String sndngProcessSttus = null;
        String sndngMastrId = null;

        if("SndngAcceptJob".equals(instanceId) || "PniCctvAcceptJob".equals(instanceId)){
            unitySndngMastrId = MDC.get(UNITY_SNDNG_MASTR_ID);
            sndngProcessSttus = ApiConstants.SndngProcessStatus.ACCEPT_FAIL.getCode();
        } else if ("SndngMakeJob".equals(instanceId)) {
            unitySndngMastrId = MDC.get(UNITY_SNDNG_MASTR_ID);
            sndngProcessSttus = MDC.get("sndngProcessSttus");
        } else if ("SndngSendBulksJob".equals(instanceId)) {
            unitySndngMastrId = MDC.get(UNITY_SNDNG_MASTR_ID);
            sndngMastrId = MDC.get("sndngMastrId");
            sndngProcessSttus = MDC.get("sndngProcessSttus");
        }
        BatchSpringUtils.getEnsBatchExtractService().updateErrorLog(
                SndngMssageParam.builder()
                        .unitySndngMastrId(unitySndngMastrId)
                        .sndngMastrId(sndngMastrId)
                        .newSndngProcessSttus(sndngProcessSttus)
                        .errorCode(instanceId)
                        .errorMssage(cutString(errorMsg, 300))
                        .build()
        );
    }

    public static List<ApiConstants.SignguCode> getSignguCodeList() {
        if(ApiConstants.IS_CCN){
            return Arrays.stream(ApiConstants.SignguCode.values())
                .filter(d -> d.getCode().equals(ApiConstants.SignguCode.CHUNCHEON.getCode()))
                .collect(Collectors.toList());

        }
        return Arrays.stream(ApiConstants.SignguCode.values())
            .filter(d -> !d.getCode().equals(ApiConstants.SignguCode.CHUNCHEON.getCode()))
            .collect(Collectors.toList());
    }
}
