package com.ydl.ydlcommon.base.config;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import com.ydl.burypointlib.MD5Util;
import com.ydl.ydlcommon.base.BaseApp;
import com.yidianling.common.tools.RxAppTool;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Ykai on 2021/1/11.
 *
 * 获取加密参数类
 */

public class EncryptionParams {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static Map<String,String> getParams(String path){
        String timestamp = String.valueOf(System.currentTimeMillis());  //值应该为毫秒数的字符串形式
        Map<String,String> map = new HashMap<>();
        map.put("timestamp",timestamp);
        map.put("path",path);
        map.put("version", RxAppTool.getAppVersionName(BaseApp.Companion.getApp()));

        List<String> storedKeys = Arrays.stream(map.keySet()
                .toArray(new String[]{}))
                .sorted(Comparator.naturalOrder())
                .collect(Collectors.toList());
        String sign = storedKeys.stream()
                .map(key -> String.join("", key, map.get(key)))
                .collect(Collectors.joining()).trim()
                .concat(HttpConfig.Companion.getENCRYPTION_APP_SECRET());
        sign = MD5Util.md5(sign).toUpperCase();
        Log.e("sign",sign);
        Map<String,String> headersMap = new HashMap<>();
        headersMap.put("appKey",HttpConfig.Companion.getENCRYPTION_APP_KEY());
        headersMap.put("sign",sign);
        headersMap.put("timestamp",timestamp);
        return headersMap;
    }
}
