package cokr.xit;

import cokr.xit.ens.core.utils.DateUtil;
import cokr.xit.ens.core.utils.crypto.Crypto;
import cokr.xit.ens.core.utils.crypto.SHA256;
import cokr.xit.ens.modules.common.ctgy.nicedici.support.rest.model.NiceCiRespDTO;
import cokr.xit.ens.modules.common.ctgy.nicedici.support.rest.model.conf.DataBodyGenerateTokenResp;
import cokr.xit.ens.modules.common.ctgy.nicedici.support.rest.model.conf.DataBodySymkeyResp;
import cokr.xit.ens.core.utils.crypto.AES256;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TempTest {

    @Test
    public void t() throws NoSuchAlgorithmException {
        List<String> jids = Arrays.asList("861128-1018410", "8611281234567", "9311281234567", "1211281234567", "1111281234567", "3211281234567");
        jids.forEach(s -> {
            AES256 aes = new AES256(Crypto.AES256.getKey());
            String encStr = aes.encrypt(s);
            String decStr = aes.decrypt(encStr);
            System.out.println("AES256 enc:::" + encStr);
            System.out.println("AES256 dec:::" + decStr);
        });
        String enc = SHA256.encrypt("8611281018410");
        System.out.println("SHA256 enc:::" + enc);
        System.out.println("688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6".length());

//        Collections.singletonList(Collections.singletonMap())
//        new Gson().
    }

    @Test
    public void test() {
        Gson gson = new Gson();
        ObjectMapper mapper = new ObjectMapper();
        String json = "{ \"access_token\": \"a12341234\"}";

        try {
            DataBodyGenerateTokenResp dto = gson.fromJson(json, DataBodyGenerateTokenResp.class);
            DataBodyGenerateTokenResp dto2 = mapper.readValue(json, DataBodyGenerateTokenResp.class);
            System.out.println("=========================================");
            System.out.println("dto " + dto.toString());
            System.out.println("dto2 " + dto2.toString());

            Map<String, String> m = gson.fromJson(json, Map.class);
            DataBodyGenerateTokenResp dto3 = mapper.convertValue(m, DataBodyGenerateTokenResp.class);
            System.out.println("dto3 " + dto3.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() throws ParseException {
        System.out.println(DateUtil.daysByFromBetweenTo(new SimpleDateFormat("yyyyMMdd").parse("20220403"), new Date()));
        System.out.println(DateUtil.absTimeSecToDate(1576983012, "yyyyMMdd"));
        int curSec = (int) (new SimpleDateFormat("yyyyMMdd").parse("20220503").getTime() / 1000);
        System.out.println(DateUtil.daysByFromBetweenTo(1576983012, curSec));
        System.out.println(1576983012 + curSec);
        System.out.println((1576983012 / 24 / 60 / 60));
//        System.out.println(new Date().getTime());


        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis((1576983012 * 1000L) + (curSec * 1000L));
        Date date = calendar.getTime();
        System.out.println(new SimpleDateFormat("yyyyMMdd").format(date));
        System.out.println(DateUtil.relTimeSecToDate(1576983012, "yyyyMMdd"));

    }

    @Test
    public void test3() {
        System.out.println(randomAlphaWord(30));
        System.out.println(randomAlphaWord(16));
        System.out.println(randomAlphaWord(2));
    }

    public static String randomAlphaWord(int wordLength) {
        Random r = new Random();

        StringBuilder sb = new StringBuilder(wordLength);
        for (int i = 0; i < wordLength; i++) {
            char tmp = (char) ('a' + r.nextInt('z' - 'a'));
            sb.append(tmp);

        }
        return sb.toString();

    }

    @Test
    public void test4() {

        final ObjectMapper mapper = new ObjectMapper()
//                .disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);


//        Map<String, String> dataHeader = new HashMap<>();
//        dataHeader.put("GW_RSLT_CD", "abcd");
//        dataHeader.put("GW_RSLT_MSG", "aaaaa");
//        Map<String, String> dataBody = new HashMap<>();
//        dataBody.put("rsp_cd", "1234");
//        dataBody.put("res_msg", "hahahah");
//        Map<String, Object> mParam = new HashMap<>();
//        mParam.put("dataHeader", dataHeader);
////        mParam.put("dataBody", dataBody);
//        mParam.put("dataBody", "");
////        mParam.put("dataBody", null);
//
//
//
        try {
//            String jsonStr = mapper.writeValueAsString(mParam);
////            System.out.println(jsonStr);
////            jsonStr = jsonStr.replaceAll("\"\"", "null");
////            System.out.println(jsonStr);
//
//            NiceCiRespDTO<DataBodyCiResp> respDTO = mapper.readValue(jsonStr, NiceCiRespDTO.class);
////            NiceCiRespDTO<DataBodyCiResp> respDTO = mapper.readValue(jsonStr, new TypeReference<NiceCiRespDTO<DataBodyCiResp>>(){});
////            NiceCiRespDTO<DataBodyCiResp> respDTO = mapper.readValue(jsonStr, mapper.getTypeFactory().constructType(DataBodyCiResp.class));
//            System.out.println(respDTO.toString());

            String strRespBody = "{  \"dataHeader\": {    \"CNTY_CD\": \"ko\",    \"GW_RSLT_CD\": \"1200\",    \"GW_RSLT_MSG\": \"오류 없음\"  },  \"dataBody\": {    \"rsp_cd\": \"P000\",    \"result_cd\": \"0000\",    \"symkey_stat_info\": \"{\\\"cur_symkey_version\\\":\\\"20220506A6834B1B-1288-IPCD-965C-8C9B30558CCF\\\",\\\"cur_valid_dtim\\\":\\\"20221106180509\\\"}\"  }}";
            strRespBody = strRespBody.replace("\\", "").replace("\"{", "{").replace("}\"", "}");
            NiceCiRespDTO<DataBodySymkeyResp> result = mapper.readValue(strRespBody, new TypeReference<NiceCiRespDTO<DataBodySymkeyResp>>() {
            });
            System.out.println(result);
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
