package cokr.xit.ens.modules.kkomydoc.service.event;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.kkomydoc.service.KkoMydocService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

@Slf4j
@Component
@RequiredArgsConstructor
public class KkoMydocSendEventListener {
    private final KkoMydocService kkoMydocService;

    @Order(1)
//    @EventListener
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendRealtime(KkoMydocSendRealtimeEvent event) {
        event.getSendMastIds().stream()
                .forEach(sendMastId -> {
                    StringBuffer sb = new StringBuffer();
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 카카오 내문서함(인증톡) 실시간전송")
                            .append("\n요청ID: " + sendMastId);

                    EnsResponseVO responseVO = kkoMydocService.remake(sendMastId);
                    sb.append("\n제작 결과: " + responseVO.toString());
                    if (!EnsErrCd.OK.equals(responseVO.getErrCode())) {
                        sb.append("\n=======================================================");
                        log.info(sb.toString());
                        return;
                    }

                    responseVO = kkoMydocService.sendBulk(sendMastId);
                    sb.append("\n전송요청 결과: " + responseVO.toString());
                    if (!EnsErrCd.OK.equals(responseVO.getErrCode())) {
                        sb.append("\n=======================================================");
                        log.info(sb.toString());
                        return;
                    }

                    responseVO = kkoMydocService.statBulk(sendMastId);
                    sb.append("\n상태갱신 결과: " + responseVO.toString())
                            .append("\n=======================================================");
                    log.info(sb.toString());

                    if (event.getCallback() != null)
                        event.getCallback().execute();
                });
    }
//    @Order(2)
////    @EventListener
//    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
//    public void callbackBySendRealtime(KkoMydocSendRealtimeEvent event) {
//        if(event.getCallback()!=null)
//            event.getCallback().execute();
//    }


    @Async
    @Order(1)
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendReserve(KkoMydocSendReserveEvent event) {
        event.getSendMastIds().stream()
                .forEach(sendMastId -> {
                    StringBuffer sb = new StringBuffer();

                    EnsResponseVO responseVO = kkoMydocService.remake(sendMastId);
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 카카오 내문서함(인증톡) 예약전송")
                            .append("\n요청ID: " + sendMastId)
                            .append("\n제작 결과: " + responseVO.toString())
                            .append("\n=======================================================");
                    log.info(sb.toString());

                    if (event.getCallback() != null)
                        event.getCallback().execute();
                });
    }
//    @Order(2)
//    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
//    public void callbackBySendReserve(KkoMydocSendReserveEvent event) {
//        if(event.getCallback()!=null)
//            event.getCallback().execute();
//    }
}
