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

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.nvsigntalk.model.NvSigntalkAcceptReqDTO;
import cokr.xit.ens.modules.nvsigntalk.model.config.Document;
import cokr.xit.ens.modules.nvsigntalk.model.config.DocumentConfNvSt;
import cokr.xit.ens.modules.nvsigntalk.model.config.XitProperty;

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

public class AcceptDataByNvSigntalk extends AcceptData<NvSigntalkAcceptReqDTO> {

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

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

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

        DocumentConfNvSt documentConf = gson.fromJson(intgrnSendDetail.getJAcptDocNvSt(), DocumentConfNvSt.class);

        return Document.builder()
                .client_doc_id(intgrnSendDetail.getLinkedUuid())
                .ci(intgrnSendDetail.getCi())
                .valid_duration(documentConf.getValid_duration())
                .valid_duration_at(documentConf.getValid_duration_at())
                .redirect_url(documentConf.getRedirect_url())
                .publish_document_yn(documentConf.getPublish_document_yn())
                .document_hash(documentConf.getDocument_hash())
                .call_center_no(documentConf.getCall_center_no())
                .org_id(documentConf.getOrg_id())
                .auth_require_yn(documentConf.getAuth_require_yn())
                .notification(documentConf.getNotification())
                .xit_property(this.createXitProperty(intgrnSendDetail))
                .build();
    }

    protected XitProperty createXitProperty(IntgrnSendDetail intgrnSendDetail) {
        return XitProperty.builder()
                .mbl_page_data(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);
    }
}
