package com.sqx.common.utils.baidu;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import cn.hutool.setting.dialect.Props;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * @ClassName BaiduCommon
 * @Description TODO
 * @Author bai
 * @Date 2024/6/17 下午8:22
 * @Version 1.0
 **/
public class BaiduCommon {

	//接口版本
	public static final String VERSION = "1";

	//接口前缀
	public static final String BASE_PREFIX_PATH = "https://pns.baidubce.com/cloud/api/v";

	//接口前缀

	//拼接后的完整接口前缀
	public static final String BASE_PATH = BASE_PREFIX_PATH + VERSION;

	//绑定
	public static final String API_AXB_BINDING = BASE_PATH + "/axb/binding";

	//解绑
	public static final String API_AXB_UNBINDING = BASE_PATH + "/axb/unbinding";

	//转绑
	public static final String API_AXB_CHANGE_BINDING = BASE_PATH + "/axb/changeBinding";

	public static String genSigned(){
		return null;
	}

	public static String getUTCDate(){
		DateTime now = DateTime.now();
		return DateUtil.format(now, DatePattern.UTC_PATTERN);
	}

	//发出请求
	public static String baseSendHttp(String url, Map<String, String> headerMap, String json) {
		return HttpRequest
				.post(url)
				.headerMap(headerMap, true)
				//.form(paramMap)
				.body(json)
				.execute()
				.body();
	}

	public static Map<String, String> getBaseHeaderMap(){
		Map<String, String> baseHeaderMap = Maps.newHashMap();
		baseHeaderMap.put("Authorization","bce-auth-v{1}/{accessKeyId}/{timestamp}/{expirationPeriodInSeconds}/{signedHeaders}/{signature}");

//		version是正整数。
//		timestamp是生成签名时的UTC时间。
//		expirationPeriodInSeconds表示签名有效期限。
//		signedHeaders是签名算法中涉及到的头域列表。头域名之间用分号（;）分隔，如host;x-bce-date。列表按照字典序排列。（本API签名仅使用host和x-bce-date两个header）
//		signature是256位签名的十六进制表示，由64个小写字母组成。

		baseHeaderMap.put("Content-Type","application/json; charset=utf-8");
		baseHeaderMap.put("x-bce-date", getUTCDate());//2019-10-30T10:55:26Z
		return baseHeaderMap;
	}

	public static JSON sendHttpToJson(String url, Map<String, Object> paramMap){
		//
		Props props = new Props("test.properties");
		props.getStr("1111");
		return JSONUtil.parse(baseSendHttp(url, getBaseHeaderMap(), JSONUtil.toJsonStr(paramMap)));
	}





}
