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

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.ktsigntalk.direct.service.KtSigntalkService;
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 KtSigntalkSendEventListener {
    private final KtSigntalkService ktSigntalkService;

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

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

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

                    responseVO = ktSigntalkService.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(KtSigntalkSendRealtimeEvent event) {
//        if(event.getCallback()!=null)
//            event.getCallback().execute();
//    }


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

                    EnsResponseVO responseVO = ktSigntalkService.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(KtSigntalkSendReserveEvent event) {
//        if(event.getCallback()!=null)
//            event.getCallback().execute();
//    }
}
