package com.malk.laidi.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
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 java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Map;

/**
 * Decription:
 *
 * @author hzk
 * @ClassName HttpUtil
 * @Date 2022/8/3 17:04
 */
public class HttpUtil {

    @Qualifier("permissionRestTemplate")
    @Autowired
    private RestTemplate restTemplate;

    public static  String get(String url){
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.getForEntity(url, String.class);
        return stringResponseEntity.getBody();
    }

    public static String post(String url,String AcceptType,String body,Map<String,String> headersMap){
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);

        if(AcceptType == null || AcceptType.equals("")){
            headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        }else{
            headers.add("Accept", AcceptType);
        }
        if(headersMap != null) {
            for (String key : headersMap.keySet()) {
                headers.add(key, headersMap.get(key));
            }
        }
        //指定格式
        HttpEntity<String> formEntity = new HttpEntity<String>(body, headers);
        ResponseEntity<String> stringResponseEntity = null;
        int index = 0;
        while (true) {
            try {
                stringResponseEntity = restTemplate.postForEntity(url, formEntity, String.class);
                break;
            } catch (Exception ex) {
                index++;
                if(index > 2)
                {
                    break;
                }
                continue;
            }
        }
        return stringResponseEntity.getBody();
    }



    public static void downloadUsingStream(String urlStr, String file) throws IOException{

        URL url = new URL(urlStr);

        BufferedInputStream bis = new BufferedInputStream(url.openStream());

        FileOutputStream fis = new FileOutputStream(file);

        byte[] buffer = new byte[1024];

        int count=0;

        while((count = bis.read(buffer,0,1024)) != -1)

        {

            fis.write(buffer, 0, count);

        }

        fis.close();

        bis.close();

    }

    public static void downloadUsingNIO(String urlStr, String file) throws IOException {

        URL url = new URL(urlStr);

        ReadableByteChannel rbc = Channels.newChannel(url.openStream());

        FileOutputStream fos = new FileOutputStream(file);

        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

        fos.close();

        rbc.close();

    }


    public static String doPostFile2(String url, MultiValueMap<String, Object> param, File file) {
        ResponseEntity<String> stringResponseEntity = null;
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.add("Accept",MediaType.APPLICATION_JSON.toString());
            headers.setContentType(MediaType.parseMediaType("multipart/form-data;charset=UTF-8"));

            FileSystemResource resource = new FileSystemResource(file);
            param.add("img",resource);

            HttpEntity<MultiValueMap<String,Object>> formEntity = new HttpEntity<>(param,headers);
            stringResponseEntity = new RestTemplate().postForEntity(url, formEntity, String.class);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return stringResponseEntity.getBody();
    }



}
