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

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.kkomydoc.model.KkoMydocAcceptReqDTO;
import cokr.xit.ens.modules.kkomydoc.model.config.Document;
import cokr.xit.ens.modules.kkomydoc.model.config.DocumentConfKkoMd;
import cokr.xit.ens.modules.kkomydoc.model.config.Receiver;
import cokr.xit.ens.modules.kkomydoc.model.config.XitProperty;

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

public class AcceptDataByKkoMydoc extends AcceptData<KkoMydocAcceptReqDTO> {

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

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

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

        DocumentConfKkoMd documentConf = gson.fromJson(intgrnSendDetail.getJAcptDocKkoMd(), DocumentConfKkoMd.class);

        return Document.builder()
                .external_document_uuid(intgrnSendDetail.getLinkedUuid())
                .hash(documentConf.getHash())
                .common_categories(documentConf.getCommon_categories())
                .read_expired_at(documentConf.getRead_expired_at())
                .read_expired_sec(documentConf.getRead_expired_sec())
                .receiver(this.createReceiver(intgrnSendDetail))
                .property(documentConf.getProperty())
                .bridge(documentConf.getBridge())
                .xit_property(this.createXitProperty(intgrnSendDetail))
                .build();
    }

    protected Receiver createReceiver(IntgrnSendDetail intgrnSendDetail) {
        return Receiver.builder()
                .ci(intgrnSendDetail.getCi())
                .phone_number(null)
                .name(null)
                .birthday(null)
                .is_required_verify_name(null)
                .build();
    }

    protected XitProperty createXitProperty(IntgrnSendDetail intgrnSendDetail) {
        return XitProperty.builder()
                .mbl_page_data(CmmnUtil.isEmpty(intgrnSendDetail.getJMblPageData()) ? null : gson.fromJson(intgrnSendDetail.getJMblPageData(), Map.class))
//                .bill_link_info(this.createBillLinkInfo(intgrnSendDetail))
                .bill_acpt_data(this.createBillAcptData(intgrnSendDetail))
//                .jid(intgrnSendDetail.getJids())
                .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);
    }

}
