package com.ruoyi.common.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.exception.BusinessException;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Map;

@Configuration
public class RestUtils {

    private static final RestTemplate restTemplate = new RestTemplate();

    public static JSONObject get(String str, Map<String,String> urlParams){
        return getStr(JSON.toJSONString(urlToUri(str,urlParams)));
    }

    private static JSONObject getStr(String str){
        URI uri = JSONObject.parseObject(str, URI.class);
        ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(uri, JSONObject.class);
        serverIsRight(responseEntity);   //判断服务器返回状态码
        return responseEntity.getBody();
    }


    public static JSONObject post(String str, JSONObject json){
        //组装urL
        return post(URI.create(str), json);
    }

    private static JSONObject post(URI uri, JSONObject json){
        //组装url
        //设置提交json格式数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> request = new HttpEntity<>(json, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(uri, request, JSONObject.class);
        serverIsRight(responseEntity);  //判断服务器返回状态码
        return responseEntity.getBody();
    }

    public static JSONObject post(String str, JSONObject json, int customStatusCode){
        //组装urL
        return post(URI.create(str), json, customStatusCode);
    }

    private static JSONObject post(URI uri, JSONObject json, int customStatusCode){
        //组装url
        //设置提交json格式数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> request = new HttpEntity<>(json, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(uri, request, JSONObject.class);
        serverIsRight(responseEntity, customStatusCode);  //判断服务器返回状态码
        return responseEntity.getBody();
    }

    private static URI urlToUri(String str, Map<String,String> urlParams){
        //设置提交json格式数据
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(str);
        for(Map.Entry<String,String> entry : urlParams.entrySet())  {
            uriBuilder.queryParam(entry.getKey(), entry.getValue()) ;
        }
        return  uriBuilder.build(true).toUri();
    }

    public static JSONObject upload(String str, MultiValueMap formParams){
        //设置表单提交
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(formParams, headers);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(str,request,JSONObject.class);
        serverIsRight(responseEntity);  //判断服务器返回状态码
        return responseEntity.getBody();
    }


    public static byte[] dowload(String url){
        ResponseEntity<byte[]> rsp = restTemplate.getForEntity(url, byte[].class);
        return rsp.getBody();
    }

    private static void serverIsRight(ResponseEntity responseEntity){
        if(responseEntity.getStatusCodeValue() != 200){
            throw new BusinessException("服务器请求异常：{}" + responseEntity.getStatusCodeValue());
        }
    }

    private static void serverIsRight(ResponseEntity responseEntity, int customStatusCode){
        if(responseEntity.getStatusCodeValue() != customStatusCode){
            throw new BusinessException("服务器请求异常：{}" + responseEntity.getStatusCodeValue());
        }
    }
}
