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

import cokr.xit.ens.core.utils.CmmnUtil;
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.kkoalimtalk.model.KkoAlimtalkAcceptReqDTO;
import cokr.xit.ens.modules.kkoalimtalk.model.config.Document;
import cokr.xit.ens.modules.kkoalimtalk.model.config.DocumentConfKkoAt;
import cokr.xit.ens.modules.kkoalimtalk.model.config.XitProperty;

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

public class AcceptDataByKkoAlimtalk extends AcceptData<KkoAlimtalkAcceptReqDTO> {


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

    @Override
    public KkoAlimtalkAcceptReqDTO createReqDto() {
        KkoAlimtalkAcceptReqDTO acceptReqDTO = KkoAlimtalkAcceptReqDTO.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) {
        DocumentConfKkoAt documentConf = gson.fromJson(intgrnSendDetail.getJAcptDocKkoAt(), DocumentConfKkoAt.class);

        return Document.builder()
                .msg_idx(intgrnSendDetail.getLinkedUuid())
                .country_code(documentConf.getCountry_code())
                .recipient(intgrnSendDetail.getMblphonNo())
                .app_user_id(documentConf.getApp_user_id())
                .title(documentConf.getTitle())
                .res_method(documentConf.getRes_method())
                .attach(documentConf.getAttach())
                .supplement(documentConf.getSupplement())
                .message_type(documentConf.getMessage_type())
                .use_failback(documentConf.getUse_failback())
                .mms_attach(documentConf.getMms_attach())
                .xit_property(this.createXitProperty(intgrnSendDetail))
                .build();
    }

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