package cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support.ktGibis;

import cokr.xit.ens.core.utils.CmmnUtil;
import cokr.xit.ens.core.utils.crypto.AES256;
import cokr.xit.ens.core.utils.crypto.Crypto;
import cokr.xit.ens.core.model.EnsBillAcptReqDTO;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.IntgrnSendDetail;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.IntgrnSendMast;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support.AcceptData;
import cokr.xit.ens.modules.ktsigntalk.gibis.model.KtGibisAcceptReqDTO;
import cokr.xit.ens.modules.ktsigntalk.gibis.model.config.Document;
import cokr.xit.ens.modules.ktsigntalk.gibis.model.config.DocumentConfKtGbs;
import cokr.xit.ens.modules.ktsigntalk.gibis.model.config.XitProperty;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AcceptDataByKtGibis extends AcceptData<KtGibisAcceptReqDTO> {

    private AES256 aes256 = new AES256(Crypto.AES256.getKey());

    public AcceptDataByKtGibis(IntgrnSendMast intgrnSendMast, List<IntgrnSendDetail> intgrnSendDetails, Boolean isReserveSend) {
        super(intgrnSendMast, intgrnSendDetails, isReserveSend);
    }

    @Override
    public KtGibisAcceptReqDTO createReqDto() {
        KtGibisAcceptReqDTO acceptReqDTO = KtGibisAcceptReqDTO.builder()
                .vender(intgrnSendMast.getVender().getCode())
                .org_cd(intgrnSendMast.getOrgCd())
                .tmplt_cd(intgrnSendMast.getTmpltCd())
                .post_bundle_title(intgrnSendMast.getPostBundleTitle())
                .send_dt(intgrnSendMast.getSendDt().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")))
                .close_dt(intgrnSendMast.getCloseDt().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")))
                .documents(intgrnSendDetails.stream().map(data -> this.createDocument(data)).collect(Collectors.toList()))
                .build();

        return acceptReqDTO;
    }

    protected Document createDocument(IntgrnSendDetail intgrnSendDetail) {

        DocumentConfKtGbs documentConf = gson.fromJson(intgrnSendDetail.getJAcptDocKtGbs(), DocumentConfKtGbs.class);

        return Document.builder()
                .linkedUuid(intgrnSendDetail.getLinkedUuid())
                .ci(intgrnSendDetail.getCi())
                .hash(documentConf.getHash())
                .srcSeq(documentConf.getSrcSeq())
                .mmsDtlCnts(documentConf.getMmsDtlCnts())
                .rcsDtlCnts(documentConf.getRcsDtlCnts())
                .url(documentConf.getUrl())
                .mdn(documentConf.getMdn())
                .xit_property(this.createXitProperty(intgrnSendDetail))
                .build();
    }

    protected XitProperty createXitProperty(IntgrnSendDetail intgrnSendDetail) {
        return XitProperty.builder()
                .mobile_page(CmmnUtil.isEmpty(intgrnSendDetail.getJMblPageData()) ? null : gson.fromJson(intgrnSendDetail.getJMblPageData(), Map.class))
                .bill_acpt_data(this.createBillAcptData(intgrnSendDetail))
                .jid(aes256.decrypt(intgrnSendDetail.getJid()))
                .tmplt_msg_data(this.createTmpltMsgData(intgrnSendDetail))
                .build();
    }

    protected EnsBillAcptReqDTO createBillAcptData(IntgrnSendDetail intgrnSendDetail) {
        if (CmmnUtil.isEmpty(intgrnSendDetail.getBill()))
            return null;
        return EnsBillAcptReqDTO.builder()
                .billUid(intgrnSendDetail.getBill().getBillUid())
                .billerUserKey(intgrnSendDetail.getBill().getBillerUserKey())
                .billSe(intgrnSendDetail.getBill().getBillSeCd())
                .build();
    }

    protected Map<String, String> createTmpltMsgData(IntgrnSendDetail intgrnSendDetail) {
        if (CmmnUtil.isEmpty(intgrnSendDetail.getJTmpltMsgData()))
            return null;
        else
            return gson.fromJson(intgrnSendDetail.getJTmpltMsgData(), Map.class);
    }
}
