package cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support.kkoalimtalk;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.core.monitor.slack.event.MonitorEvent;
import cokr.xit.ens.modules.common.code.PostSeCd;
import cokr.xit.ens.modules.common.code.StatCd;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.IntgrnSendDetail;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.repository.IntgrnSendDetailRepository;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support.AcceptDataEventListener;
import cokr.xit.ens.modules.common.monitor.MessageByPhase;
import cokr.xit.ens.modules.kkoalimtalk.domain.SendDetailKkoAlimtalk;
import cokr.xit.ens.modules.kkoalimtalk.service.KkoAlimtalkService;
import cokr.xit.ens.modules.kkoalimtalk.service.event.KkoAlimtalkSendRealtimeEvent;
import cokr.xit.ens.modules.kkoalimtalk.service.event.KkoAlimtalkSendReserveEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
@RequiredArgsConstructor
public class AcceptByKkoAlimtalkEventListener implements AcceptDataEventListener<AcceptDataByKkoAlimtalk, SendDetailKkoAlimtalk> {

    private final ApplicationEventPublisher applicationEventPublisher;
    private final KkoAlimtalkService kkoAlimtalkService;
    private final IntgrnSendDetailRepository intgrnSendDetailRepository;


    @Override
    public void accept(AcceptDataByKkoAlimtalk ev) {
        final PostSeCd POST_SE_CD = PostSeCd.kkoAlimtalk;
        EnsResponseVO responseVO = null;
        try {

            responseVO = kkoAlimtalkService.accept(ev.createReqDto());


            Map<String, Object> resultInfo = (Map<String, Object>) responseVO.getResultInfo();
            Long sendMastId = (Long) resultInfo.get("sendMastId");
            if (EnsErrCd.OK.equals(responseVO.getErrCode())) {
                List<SendDetailKkoAlimtalk> sendDetails = (List<SendDetailKkoAlimtalk>) resultInfo.get("sendDetails");
                Map<String, Long> sendDetailIdMap = convertSendDetailIdMap(sendDetails);
                this.acceptSuccess(ev.getIntgrnSendDetails(), POST_SE_CD, sendDetailIdMap);
                intgrnSendDetailRepository.saveAll(ev.getIntgrnSendDetails());
            } else {
                this.acceptFail(ev.getIntgrnSendDetails(), POST_SE_CD, responseVO.getErrCode(), String.format("%s %s", responseVO.getErrMsg(), responseVO.getResultInfo().toString()));
                intgrnSendDetailRepository.saveAll(ev.getIntgrnSendDetails());
                return;
            }


            if (ev.IsReserveSend()) {
                KkoAlimtalkSendReserveEvent event = KkoAlimtalkSendReserveEvent.builder()
                        .sendMastIds(Arrays.asList(sendMastId))
                        .build();
                applicationEventPublisher.publishEvent(event);

            } else {
                KkoAlimtalkSendRealtimeEvent event = KkoAlimtalkSendRealtimeEvent.builder()
                        .sendMastIds(Arrays.asList(sendMastId))
//                    .callback(() -> this.fetch(sendMastId, ev.getSendDetails()))
                        .build();
                applicationEventPublisher.publishEvent(event);
            }
        } catch (Exception e) {
            this.acceptFail(ev.getIntgrnSendDetails(), POST_SE_CD, EnsErrCd.ACPT999, e.getMessage());
            intgrnSendDetailRepository.saveAll(ev.getIntgrnSendDetails());
            responseVO = EnsResponseVO.errBuilder().errCode(EnsErrCd.ERR999).errMsg(e.getMessage()).build();
        } finally {
            if (!EnsErrCd.OK.equals(responseVO.getErrCode())) {
                String message = createFailMessage(ev.getIntgrnSendDetails().get(0).getIntgrnSendMast().getIntSendMastId(), POST_SE_CD, responseVO);
                applicationEventPublisher.publishEvent(MonitorEvent.builder()
                        .message(MessageByPhase.builder()
                                .oClass(getClass().getSimpleName() + "." + new Throwable().getStackTrace()[0].getMethodName())
                                .postSeCd(PostSeCd.intgrnNoti)
                                .statCd(StatCd.sendfail)
                                .errCd(responseVO.getErrCode())
                                .message(message)
                                .build())
                        .build());
            }
        }

    }

    @Override
    public Map<String, Long> convertSendDetailIdMap(List<SendDetailKkoAlimtalk> sendDetails) {

        Map<String, Long> sendDetailIdMap = sendDetails.stream()
                .map(row -> {
                    Map<String, Object> m = new HashMap<>();
                    m.put("key", row.getMsgIdx());
                    m.put("value", row.getSendDetailId());
                    return m;
                })
                .collect(Collectors.toMap(m -> String.valueOf(m.get("key")), m -> (Long) m.get("value"), (k1, k2) -> k1));

        return sendDetailIdMap;
    }

    @Override
    public void setSendDetailId(IntgrnSendDetail intgrnSendDetail, Long sendDetailId) {
        intgrnSendDetail.setSendDetailIdKkoAt(sendDetailId);
    }
}
