package com.yutou.qqbot.utlis;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.interfaces.DownloadInterface;
import org.jetbrains.annotations.NotNull;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpTools {
    public static final String serverKey = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
    private static final int HttpRequestIndex = 3;

    public static String get(String url) {
        return http_get(url, null, false);
    }

    public static String post(final String url, final byte[] body) {
        return http_post(url, body, 0, null);
    }

    public static File syncDownload(final String url, final String saveName, boolean isProxy) {
        return new HttpTools().http_syncDownload(url, saveName, isProxy);
    }

    public static String https_get(String url, Map<String, String> header) {
        try {
            HttpsURLConnection connection;
            connection = (HttpsURLConnection) new URL(url).openConnection();
            return urlConnection(header, connection);
        } catch (Exception e) {
            System.err.println("error url = " + url);
            e.printStackTrace();
        }
        return null;
    }

    public static String http_get(String url, Map<String, String> header, boolean isProxy) {
        try {
            HttpURLConnection connection;
            Proxy proxy = null;
            if (isProxy) {
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
            }

            if (proxy == null) {
                connection = (HttpURLConnection) new URL(url).openConnection();
            } else {
                connection = (HttpURLConnection) new URL(url).openConnection(proxy);
            }
            return urlConnection(header, connection);
        } catch (Exception e) {
            System.err.println("error url = " + url);
            e.printStackTrace();
        }
        return null;
    }

    @NotNull
    private static String urlConnection(Map<String, String> header, HttpURLConnection connection) throws IOException {
        connection.setRequestProperty("User-Agent", getExtUa());
        if (header != null) {
            for (String key : header.keySet()) {
                connection.addRequestProperty(key, header.get(key));
            }
        }
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder str = new StringBuilder();
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            str.append(tmp);
            str.append("\n");
        }
        reader.close();
        return str.toString();
    }

    public static String http_post(String url, byte[] body, int index, Map<String, String> headers) {
        String tmp;
        StringBuilder str = new StringBuilder();
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            if (headers != null) {
                for (String key : headers.keySet()) {
                    connection.addRequestProperty(key, headers.get(key));
                }
            }
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.addRequestProperty("User-Agent", getExtUa());
            // connection.setConnectTimeout(5 * 1000);
            // connection.setReadTimeout(10 * 1000);
            //connection.addRequestProperty("Connection", "keep-alive");
            //connection.addRequestProperty("User-Agent", getExtUa());
            //connection.addRequestProperty("content-type", "application/json");
            connection.addRequestProperty("charset", "UTF-8");
            if (body != null) {
                OutputStream outputStream = connection.getOutputStream();
                //System.out.println(new String(body));
                outputStream.write(body);
                outputStream.flush();
                outputStream.close();
            }
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            while ((tmp = reader.readLine()) != null) {
                str.append(tmp);
            }
            String finalStr = str.toString();

            connection.disconnect();
            reader.close();
            return finalStr;
        } catch (Exception e) {
            e.printStackTrace();
            if (index < HttpRequestIndex) {
                return http_post(url, body, index + 1, headers);
            } else {
                return null;
            }
        }
    }

    private static String getExtUa() {
        return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36";
    }


    public static String toUrlParams(JSONObject json) {
        StringBuilder string = new StringBuilder();
        Set<String> keys = json.keySet();
        for (String key : keys) {
            try {
                string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), StandardCharsets.UTF_8));
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    string.append("&").append(URLEncoder.encode(key, StandardCharsets.UTF_8)).append("=");
                    //    string += "&" + key + "=";
                } catch (Exception e1) {
                    string.append("&").append(key).append("=");
                }
            }
        }

        string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
        return string.toString();
    }

    public static String toUrlParams(Map<String, String> map) {
        StringBuilder builder = new StringBuilder();
        for (String key : map.keySet()) {
            builder.append(key).append("=").append(map.get(key)).append("&");
        }
        return builder.substring(0, builder.length() - 1);
    }

    public static Map<String, String> getUrlParams(String url) {
        Map<String, String> map = new HashMap<>();
        if (url.contains("?")) {
            String param = url.split("\\?")[1];
            String[] params = param.split("&");
            for (String par : params) {
                map.put(par.split("=")[0], par.split("=")[1]);
            }
        }
        return map;
    }

    public static void main(String[] args) {
        File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg", false);
        System.out.println("file.length() = " + file.length());

    }

    public static String downloadPath = "tmp" + File.separator;

    public synchronized static void download(final String url, final String saveName, final DownloadInterface downloadInterface) {
        download(url, saveName, false, downloadInterface);
    }

    public synchronized static void download(final String url, final String saveName, boolean isProxy, final DownloadInterface downloadInterface) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                File jar = null;
                try {
                    File savePath = new File(downloadPath);
                    Proxy proxy = null;
                    if (!savePath.exists()) {
                        savePath.mkdirs();
                    }
                    Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
                    if (isProxy) {
                        proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
                    }
                    URLConnection connection;
                    if (url.startsWith("https:")) {
                        if (isProxy) {
                            connection = (HttpsURLConnection) new URL(url).openConnection(proxy);
                        } else {
                            connection = (HttpsURLConnection) new URL(url).openConnection();
                        }
                    } else {
                        if (isProxy) {
                            connection = (HttpURLConnection) new URL(url).openConnection(proxy);
                        } else {
                            connection = (HttpURLConnection) new URL(url).openConnection();
                        }
                    }

                    connection.addRequestProperty("User-Agent", getExtUa());
                    //      Log.i("获取到网络请求："+((HttpsURLConnection)connection).getResponseCode());

                    InputStream inputStream = connection.getInputStream();
                    long fileSize = inputStream.available();
                    jar = new File(downloadPath + saveName + "_tmp.tmp 文件大小：" + fileSize);
                    jar.createNewFile();
                    Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
                    OutputStream outputStream = new FileOutputStream(jar);
                    byte[] bytes = new byte[1024];
                    double size = connection.getContentLength();
                    double downSize = 0;
                    int len;
                    while ((len = inputStream.read(bytes)) > 0) {
                        outputStream.write(bytes, 0, len);
                        downSize += len;
                        if (downloadInterface != null) {
                            downloadInterface.onDownloading(downSize, size);
                        }
                    }
                    outputStream.close();
                    inputStream.close();
                    File oldJar = new File(downloadPath + saveName);
                    if (oldJar.exists()) {
                        oldJar.delete();
                    }
                    jar.renameTo(oldJar);
                    Log.i("DOWNLOAD", "实际保存：" + oldJar.getAbsolutePath() + " " + oldJar.getName());
                    if (downloadInterface != null) {
                        if (oldJar.exists()) {
                            downloadInterface.onDownload(oldJar);
                        } else {
                            downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = " + fileSize + " 本地大小 = " + oldJar.length()));
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    if (jar != null) {
                        jar.delete();
                    }
                    if (downloadInterface != null) {
                        downloadInterface.onError(e);
                    }
                }
            }
        }).start();
    }

    public synchronized File http_syncDownload(final String url, final String saveName, boolean isProxy) {
        if (StringUtils.isEmpty(url)) {
            return null;
        }
        File jar = null;
        try {
            File savePath = new File(downloadPath);
            if (!savePath.exists()) {
                savePath.mkdirs();
            }
            Proxy proxy = null;
            if (isProxy) {
                proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
            }

            Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
            HttpURLConnection connection;
            if (proxy == null) {
                connection = (HttpURLConnection) new URL(url).openConnection();
            } else {
                connection = (HttpURLConnection) new URL(url).openConnection(proxy);
            }
            connection.addRequestProperty("User-Agent", getExtUa());
            //   Log.i(TAG,"获取到网络请求："+connection.getResponseCode());

            connection.addRequestProperty("Content-type", "image/jpeg");
            InputStream inputStream = connection.getInputStream();
            jar = new File(downloadPath + saveName + "_tmp.tmp");
            jar.createNewFile();
            Log.i("DOWNLOAD", "临时保存文件:" + jar.getAbsolutePath());
            OutputStream outputStream = new FileOutputStream(jar);
            byte[] bytes = new byte[1024];
            double downSize = 0;
            int len;
            while ((len = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, len);
                downSize += len;
            }
            outputStream.close();
            inputStream.close();
            File oldJar = new File(downloadPath + saveName);
            if (oldJar.exists()) {
                oldJar.delete();
            }
            connection.disconnect();
            jar.renameTo(oldJar);
            Log.i("DOWNLOAD", "实际保存：" + oldJar.getAbsolutePath() + " " + oldJar.getName());
            return oldJar;

        } catch (Exception e) {
            e.printStackTrace();
            if (jar != null) {
                jar.delete();
            }
            return null;
        }
    }

    public static String getLocalMacAddress() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            byte[] bytes = NetworkInterface.getByInetAddress(address).getHardwareAddress();
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bytes.length; i++) {
                if (i != 0) {
                    builder.append(":");
                }
                String tmp = Integer.toHexString(bytes[i] & 0xFF);
                builder.append(tmp.length() == 1 ? 0 + tmp : tmp);

            }
            return builder.toString();
        } catch (UnknownHostException | java.net.SocketException e) {
            e.printStackTrace();
            return null;
        }

    }
}
