package com.dsk.common.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.dsk.acc.openapi.api.AccClient;
import com.dsk.acc.openapi.client.Config;
import com.dsk.acc.openapi.client.util.CommonUtils;
import com.dsk.common.config.DskOpenApiConfig;
import com.dsk.common.core.page.TableDataInfo;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class DskOpenApiUtil {
	@Autowired
	private DskOpenApiConfig dskOpenApiConfig;

	/**
	 * <b>application/json请求</b>
	 * @param path 请求路径
	 * @param bodyMap 参数体
	 * @return
	 */
	public Map<String,Object> requestBody(String path, Map<String, Object> bodyMap){
		return request(path, bodyMap, "json");
	}

	public Map<String,Object> requestBodyLocal(String path, Map<String, Object> bodyMap){
		return JSONUtil.toBean(HttpUtil.post("http://127.0.0.1:8766"+path, JSONUtil.toJsonStr(bodyMap)), JSONConfig.create().setIgnoreNullValue(true), Map.class);
	}

	public Map<String,Object> requestBodyWithHeader(String path, Map<String, Object> bodyMap, Map<String, String> headerMap){
		return requestWithHeader(path, bodyMap, "json", headerMap);
	}

	/**
	 * <b>application/x-www-form-urlencoded请求</b>
	 * @param path 请求路径
	 * @param bodyMap 参数体
	 * @return
	 */
	public Map<String,Object> requestForm(String path, Map<String, Object> bodyMap){
		return request(path, bodyMap, "form");
	}

	/**
	 * <b>开放平台统一请求</b>
	 * @param path 请求路径
	 * @param bodyMap 参数体
	 * @param reqBodyType 请求格式
	 * @return
	 */
	public Map<String,Object> request(String path, Map<String, Object> bodyMap, String reqBodyType){
		try {
			AccClient.init(new Config(dskOpenApiConfig.accessKeyId, dskOpenApiConfig.accessKeySecret)
					.setEndpoint(dskOpenApiConfig.endPoint).setProtocol(dskOpenApiConfig.protocol));
			Map<String, ?> res = AccClient.request(reqBodyType, path, bodyMap);
			if(!res.containsKey("headers") || !res.containsKey("body")) {
				throw new RuntimeException(String.format("请求无返回:path=%s",path));
			}
			Object resBody = res.get("body");
			if(resBody == null) {
				return null;
			}
			return CommonUtils.assertAsMap(resBody);
		} catch (Exception e) {
			throw new RuntimeException(String.format("请求异常:path=%s,err=%s",path,e.getMessage()));
		}
	}

	/**
	 * <b>开放平台统一请求</b>
	 * @param path 请求路径
	 * @param bodyMap 参数体
	 * @param reqBodyType 请求格式
	 * @return
	 */
	public Map<String,Object> requestWithHeader(String path, Map<String, Object> bodyMap, String reqBodyType, Map<String, String> headerMap){
		try {
			AccClient.init(new Config(dskOpenApiConfig.accessKeyId, dskOpenApiConfig.accessKeySecret)
					.setEndpoint(dskOpenApiConfig.endPoint));
			Map<String, ?> res = AccClient.request(reqBodyType, path, bodyMap, headerMap);
			if(!res.containsKey("headers") || !res.containsKey("body")) {
				throw new RuntimeException(String.format("请求无返回:path=%s",path));
			}
			Object resBody = res.get("body");
			if(resBody == null) {
				return null;
			}
			return CommonUtils.assertAsMap(resBody);
		} catch (Exception e) {
			throw new RuntimeException(String.format("请求异常:path=%s,err=%s",path,e.getMessage()));
		}
	}


	public TableDataInfo responsePage(Map<String, Object> resMap) throws Exception {
		Integer code = MapUtils.getInteger(resMap, "code", 300);
		Map data = MapUtils.getMap(resMap, "data", null);
		if (200 != code) throw new RuntimeException();
		Integer count = MapUtils.getInteger(data, "totalCount", 0);
		Object list = MapUtils.getObject(data, "list", "");
		return new TableDataInfo((List<?>) list, count);
	}

}
