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

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.direct.model.KtSigntalkAcceptReqDTO;
import cokr.xit.ens.modules.ktsigntalk.direct.model.config.Document;
import cokr.xit.ens.modules.ktsigntalk.direct.model.config.DocumentConfKtSt;
import cokr.xit.ens.modules.ktsigntalk.direct.model.config.XitProperty;

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

public class AcceptDataByKtSigntalk extends AcceptData<KtSigntalkAcceptReqDTO> {

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

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

    @Override
    public KtSigntalkAcceptReqDTO createReqDto() {
        KtSigntalkAcceptReqDTO acceptReqDTO = KtSigntalkAcceptReqDTO.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) {

        DocumentConfKtSt documentConf = gson.fromJson(intgrnSendDetail.getJAcptDocKtSt(), DocumentConfKtSt.class);

        return Document.builder()
                .clientSrcKey(intgrnSendDetail.getLinkedUuid())
                .ci(intgrnSendDetail.getCi())
                .hash(documentConf.getHash())
                .srcSeq(documentConf.getSrcSeq())
                .dBirth(documentConf.getDBirth())
                .juminFirstNo(documentConf.getJuminFirstNo())
                .cnPerson(documentConf.getCnPerson())
                .cZip(documentConf.getCZip())
                .addr(documentConf.getAddr())
                .rcvTelNo(documentConf.getRcvTelNo())
                .sndTelNo(documentConf.getSndTelNo())
                .rcvPwdUseYn(documentConf.getRcvPwdUseYn())
                .mmsDtlCnts(documentConf.getMmsDtlCnts())
                .mmsTitle(documentConf.getMmsTitle())
                .sendTel(documentConf.getSendTel())
                .url(documentConf.getUrl())
                .exTime(documentConf.getExTime())
                .mType(documentConf.getMType())
                .rcvType(documentConf.getRcvType())
                .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_link_info(this.createBillLinkInfo(intgrnSendDetail))
                .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);
    }
}
