package cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service;

import cokr.xit.ens.core.exception.EnsException;
import cokr.xit.ens.core.exception.code.EnsErrCd;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.entity.BillKkoPay;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.KkoPayAbleService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.KkoPayNoticeService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.KkoPayResultService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.KkoPayUrlService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.kko.service.process.model.KkoPayNoticeRespData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.Pay;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.PayProcessService;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.model.PayApiRespDTO;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.PayAbleData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.PayNoticeData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.PayResultData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.model.PayUrlData;
import cokr.xit.ens.modules.common.ctgy.intgrnbill.support.service.process.template.PayHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.Map;

@Log4j2
@RequiredArgsConstructor
@Service
public class KkoPayProcessService implements PayProcessService<PayApiRespDTO, Map<String, Object>> {
    private final KkoPayUrlService kkoPayUrlService;
    private final KkoPayNoticeService kkoPayNoticeService;
    private final KkoPayAbleService kkoPayAbleService;
    private final KkoPayResultService kkoPayResultService;


    @Override
    public PayApiRespDTO gnrUrl(Map<String, Object> data) {

        String billUid = (String) data.get("billUid");

        
        Pay<PayApiRespDTO<Map<String, Object>>> pay = PayHelper.<PayUrlData<BillKkoPay>, String, PayApiRespDTO<Map<String, Object>>>builder()
                .param(billUid)
                .payHelperService(kkoPayUrlService)
                .build();

        
        pay.exec();


        return pay.getResult();
    }

    @Override
    public PayApiRespDTO payNotice(Map<String, Object> data) {
        
        Pay<PayApiRespDTO<KkoPayNoticeRespData>> pay = PayHelper.<PayNoticeData<BillKkoPay>, Map<String, Object>, PayApiRespDTO<KkoPayNoticeRespData>>builder()
                .param(data)
                .payHelperService(kkoPayNoticeService)
                .build();

        
        pay.exec();


        return pay.getResult();
    }

    @Override
    public PayApiRespDTO payAble(Map<String, Object> data) {

        
        Pay<PayApiRespDTO<Map<String, Object>>> pay = PayHelper.<PayAbleData<BillKkoPay>, Map<String, Object>, PayApiRespDTO<Map<String, Object>>>builder()
                .param(data)
                .payHelperService(kkoPayAbleService)
                .build();

        
        pay.exec();

        return pay.getResult();
    }

    @Override
    public PayApiRespDTO payResult(Map<String, Object> data) {

        
        Pay<PayApiRespDTO<Map<String, Object>>> pay = PayHelper.<PayResultData<BillKkoPay>, Map<String, Object>, PayApiRespDTO<Map<String, Object>>>builder()
                .param(data)
                .payHelperService(kkoPayResultService)
                .build();

        
        pay.exec();

        return pay.getResult();
    }

    @Override
    public PayApiRespDTO payCancel(Map<String, Object> data) {
        throw new EnsException(EnsErrCd.SERVICE_NOT_SUPPORTED, EnsErrCd.SERVICE_NOT_SUPPORTED.getCodeNm());
    }
}
