package cokr.xit.ens.core.init;

import cokr.xit.ens.core.aop.EnsResponseVO;
import cokr.xit.ens.modules.common.code.PostSeCd;
import cokr.xit.ens.modules.common.ctgy.intgrnnoti.model.TmpltMngIntgrnDTO;
import cokr.xit.ens.modules.common.ctgy.sys.mng.model.OrgMngDTO;
import cokr.xit.ens.modules.common.ctgy.sys.mng.service.OrgMngService;
import cokr.xit.ens.modules.common.ctgy.sys.mng.service.TmpltMngService;
import cokr.xit.ens.modules.kkoalimtalk.model.TmpltMngKkoAlimtalkDTO;
import cokr.xit.ens.modules.kkomydoc.model.TmpltMngKkoMydocDTO;
import cokr.xit.ens.modules.ktsigntalk.common.code.MTypeCd;
import cokr.xit.ens.modules.ktsigntalk.direct.model.TmpltMngKtSigntalkDTO;
import cokr.xit.ens.modules.ktsigntalk.gibis.model.TmpltMngKtGibisDTO;
import cokr.xit.ens.modules.nvsigntalk.model.TmpltMngNvSigntalkDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class InitJpaRunner implements ApplicationRunner {

    @Autowired
    private OrgMngService orgMngService;
    @Autowired
    private TmpltMngService tmpltMngService;

    @Value("${contract.kakao.pay.mydoc.access-token}")
    protected String KKO_MD_ACCESS_TOKEN;
    @Value("${contract.kakao.pay.mydoc.contract-uuid}")
    protected String KKO_MD_CONTRACT_UUID;
    @Value("${contract.kakao.pay.bill.dozn.biller-code}")
    protected String KKO_BP_BILLER_CODE;
    @Value("${contract.kakao.pay.bill.dozn.authorization}")
    protected String KKO_BP_AUTHORIZATION;
    @Value("${contract.kakao.alimtalk.biztalk.bsid}")
    protected String KKO_AT_BSID;
    @Value("${contract.kakao.alimtalk.biztalk.passwd}")
    protected String KKO_AT_PASSWD;
    @Value("${contract.kakao.alimtalk.biztalk.senderKey}")
    protected String KKO_AT_SENDER_KEY;
    @Value("${contract.nice.dici.socket.site-code}")
    protected String NICE_CD_SITE_CODE;
    @Value("${contract.nice.dici.socket.site-pw}")
    protected String NICE_CD_SITE_PW;
    @Value("${contract.nice.dici.rest.client-id}")
    protected String NICE_CD_CLIENT_ID;
    @Value("${contract.nice.dici.rest.client-secret}")
    protected String NICE_CD_CLIENT_SECRET;
    @Value("${contract.naver.signtalk.x-naver-client-id}")
    protected String NV_ST_X_NAVER_CLIENT_ID;
    @Value("${contract.naver.signtalk.x-naver-client-secret}")
    protected String NV_ST_X_NAVER_CLIENT_SECRET;

    @Value("${contract.kt.signtalk.direct.access-token}")
    protected String KT_ST_ACCESS_TOKEN;
    @Value("${contract.kt.signtalk.direct.service-cd}")
    protected String KT_ST_SERVICE_CD;
    @Value("${contract.kt.signtalk.direct.service-key}")
    protected String KT_ST_SERVICE_KEY;
    @Value("${contract.kt.signtalk.direct.client-id}")
    protected String KT_ST_CLIENT_ID;
    @Value("${contract.kt.signtalk.direct.client_pw}")
    protected String KT_ST_CLIENT_SECRET;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<EnsResponseVO> orgMngResult = createOrgMngDTO().stream().map(dto -> orgMngService.add(dto)).collect(Collectors.toList());
        List<EnsResponseVO> tmpltMngResult = createTmpltMngDTO().stream()
                .map(data -> tmpltMngService.add(data, (String) data.get("dtype")))
                .collect(Collectors.toList());
        System.out.println("============================================================================================================");
        System.out.println("======== Initailize Information [OrgMng & TmpltMng] :: active profiles - " + System.getProperty("spring.profiles.active") + " ========");
        System.out.println(orgMngResult.toString());
        System.out.println(tmpltMngResult.toString());
        System.out.println("============================================================================================================");
    }

    abstract protected List<OrgMngDTO> createOrgMngDTO();

    protected OrgMngDTO createOrgMngDTO(String orgCd, String nvOrgId, String orgNm, String kkoBpCsignYn, String kkoBpPrepayApi, String kkoBpPayResultApi) {
        return OrgMngDTO.builder()
                .orgCd(orgCd)
                .orgNm(orgNm)

                .kkoMdAccessToken(KKO_MD_ACCESS_TOKEN)
                .kkoMdContractUuid(KKO_MD_CONTRACT_UUID)

                .kkoBpCsignYn(kkoBpCsignYn)
                .kkoBpBillerCode(KKO_BP_BILLER_CODE)
                .kkoBpAuthorization(KKO_BP_AUTHORIZATION)
                .kkoBpPrepayApi(kkoBpPrepayApi)
                .kkoBpPayResultApi(kkoBpPayResultApi)

                .kkoAtBsid(KKO_AT_BSID)
                .kkoAtPasswd(KKO_AT_PASSWD)
                .kkoAtSenderKey(KKO_AT_SENDER_KEY)

                .niceCdSiteCode(NICE_CD_SITE_CODE)
                .niceCdSitePw(NICE_CD_SITE_PW)
                .niceCdClientId(NICE_CD_CLIENT_ID)
                .niceCdClientSercet(NICE_CD_CLIENT_SECRET)

                .nvStXNaverClientId(NV_ST_X_NAVER_CLIENT_ID)
                .nvStXNaverClientSecret(NV_ST_X_NAVER_CLIENT_SECRET)
                .nvStOrgId(nvOrgId)

                .ktStAccessToken(KT_ST_ACCESS_TOKEN)
                .ktStServiceCd(KT_ST_SERVICE_CD)
                .ktStServiceKey(KT_ST_SERVICE_KEY)
                .ktStClientId(KT_ST_CLIENT_ID)
                .ktStClientSecret(KT_ST_CLIENT_SECRET)
                .build();
    }


    abstract protected List<Map<String, Object>> createTmpltMngDTO();

    final protected TmpltMngKkoAlimtalkDTO createTmpltMngKkoAlimtalk(String orgCd, String tmpltCd, String biztalkTmpltCode, String title, String message, String useYn) {
        return
                TmpltMngKkoAlimtalkDTO.builder()
                        .dtype("kkoat")
                        .orgCd(orgCd)
                        .tmpltCd(tmpltCd)
                        .title(title)
                        .message(message)
                        .useYn(useYn == null ? "Y" : useYn)
                        .biztalkOrgCd(null)
                        .biztalkTmpltCode(biztalkTmpltCode)
                        .build();
    }

    protected TmpltMngKkoMydocDTO createTmpltMngKkoMydoc(String orgCd, String tmpltCd, String title, String message, String useYn, String csNumber, String csName, String ciTransUseYn, String tmpltMsgUseYn, String tmpltCsInfoUseYn) {
        return TmpltMngKkoMydocDTO.builder()
                .dtype("kkomd")
                .orgCd(orgCd)
                .tmpltCd(tmpltCd)
                .title(title)
                .message(message)
                .useYn(useYn == null ? "Y" : useYn)
                .csNumber(csNumber)
                .csName(csName)
                .ciTransUseYn(ciTransUseYn)
                .tmpltMsgUseYn(tmpltMsgUseYn)
                .tmpltCsInfoUseYn(tmpltCsInfoUseYn)
                .build();
    }

    final protected TmpltMngNvSigntalkDTO createTmpltMngNvSigntalk(String orgCd, String tmpltCd, String title, String message, String useYn, String callCenterNo, String ciTransUseYn, String tmpltMsgUseYn, String tmpltCsInfoUseYn, String messageDataFmt) {
        return TmpltMngNvSigntalkDTO.builder()
                .dtype("nvst")
                .orgCd(orgCd)
                .tmpltCd(tmpltCd)
                .title(title)
                .message(message)
                .useYn(useYn == null ? "Y" : useYn)
                .callCenterNo(callCenterNo)
                .ciTransUseYn(ciTransUseYn)
                .tmpltMsgUseYn(tmpltMsgUseYn)
                .tmpltCsInfoUseYn(tmpltCsInfoUseYn)
//                    .messageDataFmt(CmmnUtil.isEmpty(messageDataFmt) ? null : mapper.readValue(messageDataFmt, Map.class))
                .messageDataFmt(messageDataFmt)
                .build();


    }

    final protected TmpltMngKtGibisDTO createTmpltMngKtGibis(String orgCd, String tmpltCd, String title, String message, String useYn, String msgCd, MTypeCd mTypeCd, String msgType, Integer optType, String sndTelNo, String sendTel, String ciTransUseYn, String tmpltMsgUseYn, String tmpltCsInfoUseYn) {
        return TmpltMngKtGibisDTO.builder()
                .dtype("ktgbs")
                .orgCd(orgCd)
                .tmpltCd(tmpltCd)
                .title(title)
                .message(message)
                .useYn(useYn == null ? "Y" : useYn)
                .msgCd(msgCd)
                .mtype(mTypeCd)
                .msgType(msgType)
                .optType(optType)
                .sndTelNo(sndTelNo)
                .sendTel(sendTel)
                .ciTransUseYn(ciTransUseYn)
                .tmpltMsgUseYn(tmpltMsgUseYn)
                .tmpltCsInfoUseYn(tmpltCsInfoUseYn)
                .build();
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn) {
        return createTmpltMngIntgrn(try1, try2, try3, orgCd, tmpltCd, title, message, useYn, null, null, null, null, null);
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn, TmpltMngKkoAlimtalkDTO kkoAlimtalk) {
        return createTmpltMngIntgrn(try1, try2, try3, orgCd, tmpltCd, title, message, useYn, kkoAlimtalk, null, null, null, null);
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn, TmpltMngKkoAlimtalkDTO kkoAlimtalk, TmpltMngKkoMydocDTO kkoMydoc) {
        return createTmpltMngIntgrn(try1, try2, try3, orgCd, tmpltCd, title, message, useYn, kkoAlimtalk, kkoMydoc, null, null, null);
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn, TmpltMngKkoAlimtalkDTO kkoAlimtalk, TmpltMngKkoMydocDTO kkoMydoc, TmpltMngNvSigntalkDTO nvSigntalk) {
        return createTmpltMngIntgrn(try1, try2, try3, orgCd, tmpltCd, title, message, useYn, kkoAlimtalk, kkoMydoc, nvSigntalk, null, null);
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn, TmpltMngKkoAlimtalkDTO kkoAlimtalk, TmpltMngKkoMydocDTO kkoMydoc, TmpltMngNvSigntalkDTO nvSigntalk, TmpltMngKtSigntalkDTO ktSigntalk) {
        return createTmpltMngIntgrn(try1, try2, try3, orgCd, tmpltCd, title, message, useYn, kkoAlimtalk, kkoMydoc, nvSigntalk, ktSigntalk, null);
    }

    final protected TmpltMngIntgrnDTO createTmpltMngIntgrn(PostSeCd try1, PostSeCd try2, PostSeCd try3, String orgCd, String tmpltCd, String title, String message, String useYn, TmpltMngKkoAlimtalkDTO kkoAlimtalk, TmpltMngKkoMydocDTO kkoMydoc, TmpltMngNvSigntalkDTO nvSigntalk, TmpltMngKtSigntalkDTO ktSigntalk, TmpltMngKtGibisDTO ktGibis) {
        return
                TmpltMngIntgrnDTO.builder()
                        .dtype("intgrn")
                        .orgCd(orgCd)
                        .tmpltCd(tmpltCd)
                        .title(title)
                        .message(message)
                        .useYn(useYn == null ? "Y" : useYn)
                        .try1(try1)
                        .try2(try2)
                        .try3(try3)
                        .kkoAlimtalk(kkoAlimtalk)
                        .kkoMydoc(kkoMydoc)
                        .nvSigntalk(nvSigntalk)
                        .ktSigntalk(ktSigntalk)
                        .ktGibis(ktGibis)
                        .build();
    }


}