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

import java.time.format.*;
import java.util.*;
import java.util.stream.*;

import cokr.xit.ens.core.model.*;
import cokr.xit.ens.core.utils.*;
import cokr.xit.ens.core.utils.crypto.*;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.domain.*;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.service.support.*;
import cokr.xit.ens.modules.kkomydoc.model.config.*;
import cokr.xit.ens.modules.kkotalk.model.*;
import cokr.xit.ens.modules.kkotalk.model.config.Document;

// FIXME: 카카오톡 신규 추가
public class AcceptDataByKkoTalk extends AcceptData<KkotalkDTO.KkoTalkAcceptReqDTO> {

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

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

    @Override
    public KkotalkDTO.KkoTalkAcceptReqDTO createReqDto() {
        return KkotalkDTO.KkoTalkAcceptReqDTO.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(this::createDocument).collect(Collectors.toList()))
                .build();
    }

    protected Document createDocument(IntgrnSendDetail intgrnSendDetail) {

        KkotalkApiDTO.Envelope envelope = gson.fromJson(intgrnSendDetail.getJAcptDocKkoTalk(), KkotalkApiDTO.Envelope.class);

        return Document.builder()
            .title(envelope.getTitle())
            .content(envelope.getContent())
            .hash(envelope.getHash())
            .guide(envelope.getGuide())
            .payload(envelope.getPayload())
            .readExpiresAt(envelope.getReadExpiresAt())
            .reviewExpiresAt(envelope.getReviewExpiresAt())
            .useNonPersonalizedNotification(envelope.getUseNonPersonalizedNotification())
            .ci(envelope.getCi())
            .phoneNumber(envelope.getPhoneNumber())
            .name(envelope.getName())
            .birthday(envelope.getBirthday())
            .externalId(intgrnSendDetail.getLinkedUuid())
            .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_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);
    }

}
