package cn.estsh.i3plus.ext.mes.apiservice.util;

import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

@Slf4j
public class FileExtUtil {

    /**
     * 根据文件地址获取输入流信息
     * @param urlPath
     * @return
     */
    public static InputStream getInputStream(String urlPath) {
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection;
        try {
            URL url = new URL(urlPath);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(3000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            int responseCode = httpURLConnection.getResponseCode();
            System.out.println("responseCode is:" + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
            } else {
                inputStream = httpURLConnection.getErrorStream();
            }
        } catch (MalformedURLException e) {
            log.error("FileUtil.getInputStream MalformedURLException urlPath={} errorMsg={}",urlPath, ExceptionUtils.getStackTrace(e));
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("CdmExportTaskDetailService.getInputStream MalformedURLException urlPath=%s errorMsg=%s",urlPath,ExceptionUtils.getStackTrace(e))
                    .build();
        } catch (IOException e) {
            log.error("FileUtil.getInputStream IOException urlPath={} errorMsg={}",urlPath,ExceptionUtils.getStackTrace(e));
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("FileUtil.getInputStream IOException urlPath=%s errorMsg=%s",urlPath,ExceptionUtils.getStackTrace(e))
                    .build();
        }
        return inputStream;
    }

    /**
     * 导出文件
     * @param file
     * @param resp
     */
    public static void exportFile(HttpServletResponse resp, File file) {
        InputStream inputStream;
        OutputStream out = null;
        try {
            inputStream = new FileInputStream(file);
            out = resp.getOutputStream();
            int len;
            byte[] b = new byte[1024];
            while ((len = inputStream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            out.flush();
        } catch (Exception var22) {
            var22.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    public static InputStream getInputStream(String urlPath) {
//        InputStream inputStream = null;
//        HttpURLConnection httpURLConnection;
//        try {
//            URL url = new URL(urlPath);
//            httpURLConnection = (HttpURLConnection) url.openConnection();
//            httpURLConnection.setConnectTimeout(3000);
//            httpURLConnection.setDoInput(true);
//            httpURLConnection.setRequestMethod("GET");
//            int responseCode = httpURLConnection.getResponseCode();
//            System.out.println("responseCode is:" + responseCode);
//            if (responseCode == HttpURLConnection.HTTP_OK) {
//                inputStream = httpURLConnection.getInputStream();
//            } else {
//                inputStream = httpURLConnection.getErrorStream();
//            }
//        } catch (MalformedURLException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return inputStream;
//    }

//    public static void writeFile(HttpServletResponse resp, InputStream inputStream) {
//        OutputStream out = null;
//        try {
//            out = resp.getOutputStream();
//            int len;
//            byte[] b = new byte[1024];
//            while ((len = inputStream.read(b)) != -1) {
//                out.write(b, 0, len);
//            }
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (out != null) {
//                    out.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

//    public static void exportFile(HttpServletResponse resp, File file) {
//        InputStream inputStream = null;
//        try {
//            inputStream = new FileInputStream(file);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }
//        writeFile(resp, inputStream);
//    }
}
