package com.zcloud.util.docking;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @author fangjiakai
 * @date 2022/11/07 13:11
 */
public class ConstCorps {
    public static String KEY = "BF732B1070D75778427B2BA6ECE074E5";
    public static String IV = "AFA9AE7D9A8BA0C11995E34DA961C5D1";
    public static List<PageData> CORPS = Arrays.asList(new PageData() {{
        put("CORP_NAME", "河北圣莱欧化工科技有限公司");
        put("CORPINFO_ID", "fe58b51278094635800b944e4ae3a871");
        put("CODE", "130180013");
        put("APPID", "uf3f13d6c");
        put("SECRET", "sb27ca08bb7bebafa");
    }}, new PageData() {{
        put("CORP_NAME", "石家庄东翔化工有限公司");
        put("CORPINFO_ID", "0fa689dd674546b69ebd77728ffd1caa");
        put("CODE", "130110421");
        put("APPID", "u95475bc7");
        put("SECRET", "s838c5c0c0d6badfe");
    }}
//            , new PageData() {{
//        put("CORP_NAME", "卓云企业");
//        put("CORPINFO_ID", "13cf0f4ec77e4d98ae8cdd9c3386ae0c");
//        put("CODE", "130110421");
//        put("APPID", "u95475bc7");
//        put("SECRET", "s838c5c0c0d6badfe");
//    }}

    );

    //获取Authorization
    public static String getToken(String value) {
        return AesGcm256Util.encrypt(value, AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
    }
    public static String getTokenByCorpCompanyCode(String companyCode) {
        JSONObject token = new JSONObject();
        Iterator var2 = CORPS.iterator();

        while(var2.hasNext()) {
            PageData corps = (PageData)var2.next();
            if (corps.getString("CODE").equals(companyCode)) {
                token.put("appid", corps.getString("APPID"));
                token.put("secret", corps.getString("SECRET"));
            }
        }

        return AesGcm256Util.encrypt(token.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
    }

    public static String getTokenByCorpId(String corpId) {
        JSONObject token = new JSONObject();
        // todo 上线前删除
        if (corpId.equals("467088695e1340b2897c264bb8100766")) {
            token.put("appid", "u95475bc7");
            token.put("secret", "s838c5c0c0d6badfe");
            return AesGcm256Util.encrypt(token.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
        }
        if (corpId.equals("13cf0f4ec77e4d98ae8cdd9c3386ae0c")) {
            token.put("appid", "u95475bc7");
            token.put("secret", "s838c5c0c0d6badfe");
            return AesGcm256Util.encrypt(token.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
        }
        for (PageData corps : ConstCorps.CORPS) {
            // 对应上编码
            if (corps.getString("CORPINFO_ID").equals(corpId)) {
                token.put("appid", corps.getString("APPID"));
                token.put("secret", corps.getString("SECRET"));
            }

        }
        return AesGcm256Util.encrypt(token.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
    }

    public static String getcorpCodeById(String corpId) {
        if (corpId.equals("13cf0f4ec77e4d98ae8cdd9c3386ae0c")) {
            return "130110421";
        }

        Optional<PageData> corpinfoId = CORPS.stream().filter(corps -> corps.getString("CORPINFO_ID").equals(corpId)).findFirst();
        if (corpinfoId.isPresent()) {
            return corpinfoId.get().getString("CODE");
        }
        return "";
    }
    public static String getcorpIdByCode(String code) {
        Optional<PageData> corpinfoId = CORPS.stream().filter(corps -> corps.getString("CODE").equals(code)).findFirst();
        if (corpinfoId.isPresent()) {
            return corpinfoId.get().getString("CORPINFO_ID");
        }
        return "";
    }
    public static void main(String[] args) {
        System.out.println(ConstCorps.getTokenByCorpId("13cf0f4ec77e4d98ae8cdd9c3386ae0c"));
        System.out.println("-----");


//        System.out.println(getToken("{\"appid\":\"uf3f13d6c,\"secret\":\"sb27ca08bb7bebafa\"}"));
//        String xxx = "{\"companyCodes\": [\"130110421\",\"130180013\"]}";
//        System.out.println(getToken(xxx));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid", "u95475bc7");
        jsonObject.put("secret", "s838c5c0c0d6badfe");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("companyCodes", Arrays.asList("130180013", "130110421"));
        String data = AesGcm256Util.encrypt(jsonObject2.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
        System.out.println(data);
        String encrypt = AesGcm256Util.encrypt(jsonObject.toString(), AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
        System.out.println(encrypt);
        String decrypt = AesGcm256Util.decrypt(encrypt, AesGcm256Util.hexToByte(KEY), AesGcm256Util.hexToByte(IV));
        System.out.println(decrypt);

    }

    public static String getCorpIdByIndex(int finalI) {
       return CORPS.get(finalI).getString("CORPINFO_ID");
    }
}
