package cokr.xit.ens.modules.ktsigntalk.gibis.service.event;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.ktsigntalk.gibis.service.KtGibisService;
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 KtGibisSendEventListener {
    private final KtGibisService ktGibisService;

    @Order(1)
//    @EventListener
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
    public void sendRealtime(KtGibisSendRealtimeEvent event) {
        event.getSendMastIds().stream()
                .forEach(sendMastId -> {
                    StringBuffer sb = new StringBuffer();
                    sb.append("=======================================================")
                            .append("[ENS] KT 인증톡(지비스) 실시간전송")
                            .append("요청ID: " + sendMastId);

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

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

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

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


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

                    EnsResponseVO responseVO = ktGibisService.remake(sendMastId);
                    sb.append("=======================================================")
                            .append("[ENS] KT 인증톡(지비스) 예약전송")
                            .append("요청ID: " + sendMastId)
                            .append("제작 결과: " + responseVO.toString())
                            .append("=======================================================");
                    log.info(sb.toString());

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