package com.bs.web.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.XML;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;

class Sign {

    @Resource
    private WeiXinUtil weiXinUtil;
    public static void main(String[] args) {
        String jsapi_ticket = " <?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Root>\n" +
                "    <Corporation Name=\"三明市***电脑贸易有限公司\"/>\n" +
                "    <Corporation Name=\"三明市**运输有限公司\"/>\n" +
                "    <Corporation Name=\"东山县****有限公司\"/>\n" +
                "    <Corporation Name=\"中科**（福建）****有限公司\"/>\n" +
                "    <Corporation Name=\"仙游县*****铜门加工厂\"/>\n" +
                "    <Corporation Name=\"光泽县****开发有限公司\"/>\n" +
                "    <Corporation Name=\"南安市******有限公司\"/>\n" +
                "    <Corporation Name=\"南安市****加工厂\"/>\n" +
                "    <Corporation Name=\"南安****有限公司\"/>\n" +
                "    <Corporation Name=\"南靖县******有限公司\"/>\n" +
                "</Root>";

        String s = convertXmlToJson(jsapi_ticket);
        Map<String, Object> mapJson = new HashMap<>();
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapJson = mapper.readValue(s, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Object messageJson = mapJson.get("Root");
        LinkedHashMap<String, Object> nestedMapNts = (LinkedHashMap<String, Object>) messageJson;
        LinkedHashMap<String, LinkedHashMap<String, Object>> nestedMapInfo = (LinkedHashMap<String, LinkedHashMap<String, Object>>) messageJson;
        Object corporation = nestedMapNts.get("Corporation");
        try {
            JSONArray jsonArray = new JSONArray(corporation);
            List<String> names = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name = jsonObject.getStr("Name");
                names.add(name);
            }
            System.out.println(names);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    };

    public static Map<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";

        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                  "&noncestr=" + nonce_str +
                  "&timestamp=" + timestamp +
                  "&url=" + url;
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);

        return ret;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }




    public static String convertXmlToJson(String xmlString) {
        JSONObject jsonObject = XML.toJSONObject(xmlString); // 使用JSON库将XML字符串转换为JSONObject对象
        String jsonString = jsonObject.toString(); // 将JSONObject对象转换为JSON字符串
        return jsonString;
    }

}
