package com.weaver.seconddev.chapanda.test;

import org.apache.commons.net.util.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

public class Test99 {
    private static final String HMAC_SHA1 = "HmacSHA1";


    public static void main(String[] args) throws Exception {
        String url = "http://api.beisenapp.com/Assess/431582/activity/testee/invite";

        // ?beisen_account=jiekou@test-chabaidao.com&emails=798989044@qq.com&format=json

        String oauth_consumer_secret = "8460bf7be9fb4db29a263681fc0e4583";
        String oauth_token_secret = "dbe6fbf7a60243d4a05838b739fb2730";
        String key = oauth_consumer_secret+"&"+oauth_token_secret ;

        String oauth_consumer_key = "cb6c411bd5ff46f4b5e12d0853de8dcb";

        System.out.println("oauth_consumer_key:"+oauth_consumer_key);
        String oauth_nonce = "avKKMm2tuGi";
//        String oauth_nonce = UUID.randomUUID().toString();
        System.out.println("oauth_nonce:"+oauth_nonce);

        String oauth_signature_method = "HMAC-SHA1";
        System.out.println("oauth_signature_method:"+oauth_signature_method);

        long oauth_timestamp = 1754363166L;


//        long oauth_timestamp = System.currentTimeMillis()/1000;
        System.out.println("oauth_timestamp:"+oauth_timestamp);

        String oauth_token = "6e8cce92d2474f808d0c5a662a81dcdb";
        System.out.println("oauth_token:"+oauth_token);
        String oauth_version = "1.0";
        System.out.println("oauth_version:"+oauth_version);

        String beisen_account = "jiekou@test-chabaidao.com";
        String data6 = URLEncoder.encode(beisen_account, "UTF-8");
        System.out.println("data6:"+data6);
        String emails = "yemintest@chabaidao.com";
        String format = "json";

        String data3 = "beisen_account="+URLEncoder.encode(beisen_account, "UTF-8")+"&emails="+URLEncoder.encode(emails, "UTF-8")+"&format="+URLEncoder.encode(format, "UTF-8")+"&oauth_consumer_key="+URLEncoder.encode(oauth_consumer_key, "UTF-8")+"&oauth_nonce="+URLEncoder.encode(oauth_nonce, "UTF-8")+"&oauth_signature_method="+URLEncoder.encode(oauth_signature_method, "UTF-8")+"&oauth_timestamp="+URLEncoder.encode(oauth_timestamp+"", "UTF-8")+"&oauth_token="+URLEncoder.encode(oauth_token, "UTF-8")+"&oauth_version="+URLEncoder.encode(oauth_version, "UTF-8");
        System.out.println("data3:"+data3);
//        String data4 = "beisen_account="+beisen_account+"&emails="+emails+"&format="+format+"&oauth_consumer_key="+oauth_consumer_key+"&oauth_nonce="+oauth_nonce+"&oauth_signature_method="+oauth_signature_method+"&oauth_timestamp="+oauth_timestamp+"&oauth_token="+oauth_token+"&oauth_version="+oauth_version;

        String data5 = URLEncoder.encode(data3, "UTF-8");
        System.out.println("data5:"+data5);

        String data2 = "POST&"+URLEncoder.encode(url, "UTF-8")+"&"+data5;
        System.out.println("data2:"+data2);
//        String message = URLEncoder.encode(data2, "UTF-8");
//        System.out.println("message:"+message);
        System.out.println("key:"+key);
        String hmac = hmacSha1(key, data2);
        System.out.println("hmac:"+hmac);
        String encodedStr = URLEncoder.encode(hmac, "UTF-8");
        System.out.println("Encoded String: " + encodedStr);

    }

    public static String hmacSha1(String key, String message) {
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
            mac.init(secretKeySpec);
            byte[] digest = mac.doFinal(message.getBytes());
            return java.util.Base64.getEncoder().encodeToString(digest);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
            return null;
        }
    }



    /**
     * 生成签名数据
     *
     * @param data 待加密的数据
     * @param key  加密使用的key
     */
    public static String getSignature(String data,String key) throws Exception{
        byte[] keyBytes=key.getBytes();
        SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);


//        byte[] text = data.getBytes("UTF-8");
//        byte[] text2 = mac.doFinal(text);
////        byte[] rawHmac = mac.doFinal(data.getBytes());
//        StringBuilder sb=new StringBuilder();
//        for(byte b:rawHmac){
//            sb.append(byteToHexString(b));
//        }

        byte[] encryptedBytes = mac.doFinal();
        return Base64.encodeBase64String(encryptedBytes);

    }

    private static String byteToHexString(byte ib){
        char[] Digit={
                '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'
        };
        char[] ob=new char[2];
        ob[0]=Digit[(ib>>>4)& 0X0f];
        ob[1]=Digit[ib & 0X0F];
        String s=new String(ob);
        return s;
    }


    public Map<String, Object> sortMap(Map<String,Object> paraMap){
        Map<String, Object> unSortMap = new HashMap<String, Object>();
        // 使用TreeMap并提供一个Comparator用于排序key
        Map<String, Object> sortedMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String k1, String k2) {
                // 对于字符串key升序排序
                return k1.compareTo(k2);
                // 若需要降序排序，则可以改为
                // return k2.compareTo(k1);
            }
        });
        sortedMap.putAll(unSortMap );
        return sortedMap;
    }

}
