package cokr.xit.ens.biz.traffic.chuncheon.init;

import cokr.xit.ens.biz.traffic.chuncheon.mydoc.domain.repository.NhtTmplatManageRepository;
import cokr.xit.ens.core.aop.EnsResponseVO;
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.kkomydoc.model.TmpltMngKkoMydocDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
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 org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * 주정차웹-춘천
 */
@Component
@Transactional
@Profile({"prod-traffic"})
public class TrafficCcJpaRunner implements ApplicationRunner {

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

    ObjectMapper mapper = new ObjectMapper();
    @Value("${contract.kakao.pay.mydoc.access-token}")
    private String KKO_MD_ACCESS_TOKEN = "b40d3c623d1011ecbceae6cf4630da62";
    @Value("${contract.kakao.pay.mydoc.contract-uuid}")
    private String KKO_MD_CONTRACT_UUID = "CON-b4126c833d1011ecbceae6cf4630da62";
    private String KKO_BP_BILLER_CODE = null;
    private String KKO_BP_AUTHORIZATION = null;
    private String KKO_AT_BSID = null;
    private String KKO_AT_PASSWD = null;
    private String KKO_AT_SENDER_KEY = null;
    @Value("${contract.nice.dici.socket.site-code}")
    private final String NICE_CD_SITE_CODE = "EQ00";
    @Value("${contract.nice.dici.socket.site-pw}")
    private String NICE_CD_SITE_PW = "76646885";
    private String NV_ST_X_NAVER_CLIENT_ID = null;
    private String NV_ST_X_NAVER_CLIENT_SECRET = null;

    @Autowired
    private NhtTmplatManageRepository nhtTmplatManageRepository;

    @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(map -> tmpltMngService.add(map, (String) map.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("============================================================================================================");

    }


    private List<OrgMngDTO> createOrgMngDTO() {
        List<OrgMngDTO> list = nhtTmplatManageRepository.findAll()
                .stream()
                .map(data -> createOrgMngDTO(data.getSignguCode(), "주정차웹CC-" + data.getSignguCode()))
                .collect(Collectors.toList());
        return list;
    }


    private List<Map<String, Object>> createTmpltMngDTO() {
        List<Map<String, Object>> list = nhtTmplatManageRepository.findAll()
                .stream()
                .map(data -> createTmpltMngKkoMydoc(data.getSignguCode(), data.getNhtTmplatId(), data.getNhtSj(), data.getNhtCn(), "Y", data.getCstmrCnterTlphonNo(), data.getCstmrCnterNm(), "Y", "Y", "Y"))
                .collect(Collectors.toList());
        return list;
    }

    final private OrgMngDTO createOrgMngDTO(String orgCd, String orgNm) {
        return OrgMngDTO.builder()
                .orgCd(orgCd)
                .orgNm(orgNm)
                .kkoMdAccessToken(KKO_MD_ACCESS_TOKEN)
                .kkoMdContractUuid(KKO_MD_CONTRACT_UUID)
                .kkoBpBillerCode(KKO_BP_BILLER_CODE)
                .kkoBpAuthorization(KKO_BP_AUTHORIZATION)
                .kkoBpCsignYn("N")
                .kkoBpPrepayApi(null)
                .kkoBpPayResultApi(null)
                .kkoAtBsid(KKO_AT_BSID)
                .kkoAtPasswd(KKO_AT_PASSWD)
                .kkoAtSenderKey(KKO_AT_SENDER_KEY)
                .niceCdSiteCode(NICE_CD_SITE_CODE)
                .niceCdSitePw(NICE_CD_SITE_PW)
                .nvStXNaverClientId(NV_ST_X_NAVER_CLIENT_ID)
                .nvStXNaverClientSecret(NV_ST_X_NAVER_CLIENT_SECRET)
                .nvStOrgId(null)
                .build();
    }

    final private Map<String, Object> createTmpltMngKkoMydoc(String orgCd, String tmpltCd, String title, String message, String useYn, String csNumber, String csName, String ciTransUseYn, String tmpltMsgUseYn, String tmpltCsInfoUseYn) {
        return mapper.convertValue(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(), Map.class);
    }


}