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

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.nvsigntalk.service.NvSigntalkService;
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 NvSigntalkSendEventListener {
    private final NvSigntalkService nvSigntalkService;

    @Order(1)
//    @EventListener
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendRealtime(NvSigntalkSendRealtimeEvent event) {
        event.getSendMastIds().stream()
                .forEach(sendMastId -> {
                    StringBuffer sb = new StringBuffer();
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 네이버 고지서 실시간전송")
                            .append("\n요청ID: " + sendMastId);

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

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

//                            responseVO = nvSigntalkService.statBulk(sendMastId);
//                            .append("상태갱신 결과: {}", responseVO.toString());
                    sb.append("\n=======================================================");
                    log.info(sb.toString());

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


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

                    EnsResponseVO responseVO = nvSigntalkService.make(sendMastId);
                    sb.append("\n=======================================================")
                            .append("\n[ENS] 네이버 고지서 예약전송")
                            .append("\n요청ID: " + sendMastId)
                            .append("\n제작 결과: " + responseVO.toString())
                            .append("=======================================================");
                    log.info(sb.toString());


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