package com.bpa.scalage.helper;

import android.os.Looper;
import android.util.Log;

import com.bpa.scalage.config.ConfigName;
import com.bpa.scalage.message.MessageManager;
import com.bpa.scalage.mode.APIResult;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class APIHelper {

    private static String key="1bef8f66-1896-46ae-a724-101a709d5a26";

    /**
     * 获取加密后的签名
     * @param t
     * @return
     */
    public static String GetSign(Object t,String key){
        AtomicReference<String> retstr = new AtomicReference<>("");
        List<Field> fields = Arrays.asList(t.getClass().getFields());
        Collections.sort(fields, Comparator.comparing(Field::getName));
        fields.forEach(item->{
            if( item.getName().toUpperCase()!="sign".toUpperCase()){
                try {
                    if(item.get(t)!=null&&!item.get(t).toString().isEmpty()){
                        Type type = item.getGenericType();
                        String typeName = type.toString();
                        switch(typeName){
                            case "class java.util.Date":
                            case "class java.lang.String":
                            case "byte":
                            case "int":
                            case "float":
                            case "double":
                            case "long":
                            case "short":
                            case "boolean":
                                retstr.set(retstr.get() + item.getName() + "=" + item.get(t) + "&");
                                break;
                            default:
//                             String a= new GsonBuilder().serializeNulls().create().toJson(item.get(t));//保留为空的属性
                                String a= new Gson().toJson(item.get(t));
                                retstr.set(retstr.get() + item.getName() + "=" + a + "&");
                                break;
                        }
                    }
                } catch (Exception e) {
                }
            }
        });
        String tempSign=retstr.get().substring(0, retstr.get().length()-1)+"&key="+key;
        StringBuilder sb = new StringBuilder();
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] digest = md.digest(tempSign.toUpperCase().getBytes());
            for (byte b : digest) {
                sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1,3));
            }
        }catch (Exception e){

        }
        return sb.toString().toUpperCase();
    }

    public static String Post(String url, Map<String, String> parameters, Map<String, String> headers) throws IOException {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL requestUrl = new URL(url);
            connection = (HttpURLConnection) requestUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 设置header
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            // 设置参数
            if (parameters != null) {
                StringBuilder stringBuilder = new StringBuilder();
                for (Map.Entry<String, String> entry : parameters.entrySet()) {
                    stringBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")).append("&");
                }
                String params = stringBuilder.toString();
                byte[] postData = params.getBytes("UTF-8");
                connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
                try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                    outputStream.write(postData);
                }
            }
            // 获取响应结果
            int statusCode = connection.getResponseCode();
            if (statusCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
            } else {
                throw new IOException("服务器返回错误，状态码：" + statusCode);
            }
        }catch (Exception e){
            Log.e("Debug","POST 请求异常:"+e.toString());
        }finally {
            // 关闭连接和流
            if (reader != null) {
                reader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return buffer.toString();
    }
    public static String Post(String url, String body, Map<String, String> headers) throws IOException {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL requestUrl = new URL(url);
            connection = (HttpURLConnection) requestUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");// "Fiddler"
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 设置header
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            // 设置参数
            if (!body .isEmpty()) {
                connection.setRequestProperty("Content-Length", Integer.toString(body.getBytes().length));
                try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                    outputStream.write(body.getBytes());
                }
            }
            // 获取响应结果
            int statusCode = connection.getResponseCode();
            if (statusCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
            } else {
                throw new IOException("服务器返回错误，状态码：" + statusCode);
            }
        }catch (Exception e){
            Log.e("Debug","POST 请求异常:"+e.toString());
        }finally {
            // 关闭连接和流
            if (reader != null) {
                reader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return buffer.toString();
    }
    public static String Post(String url, Object body, Map<String, String> headers) throws IOException {
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuffer buffer = new StringBuffer();
        try {
            URL requestUrl = new URL(url);
            connection = (HttpURLConnection) requestUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");// "Fiddler"
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Charset", "UTF-8");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            // 设置header
            if (headers != null) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    connection.setRequestProperty(entry.getKey(), entry.getValue());
                }
            }
            // 设置参数
            if (body !=null) {
                byte[] bytes = new Gson().toJson(body).getBytes();
                connection.setRequestProperty("Content-Length", Integer.toString(bytes.length));
                try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                    outputStream.write(bytes);
                }
            }
            // 获取响应结果
            int statusCode = connection.getResponseCode();
            if (statusCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
            } else {
                throw new IOException("服务器返回错误，状态码：" + statusCode);
            }
        }catch (Exception e){
            Log.e("Debug","POST 请求异常:"+e.toString());
        }finally {
            // 关闭连接和流
            if (reader != null) {
                reader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return buffer.toString();
    }





    public static void Post(String url, Object body,IRunT<APIResult> callback,IRunT<String> errorNotify) {
        new Thread(()->{
            HttpURLConnection connection = null;
            InputStream inputStream = null;
            BufferedReader reader = null;
            StringBuffer buffer = new StringBuffer();
            try {
                URL requestUrl = new URL(url);
                connection = (HttpURLConnection) requestUrl.openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");// "Fiddler"
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("Charset", "UTF-8");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(5000);
                connection.setDoOutput(true);
                connection.setDoInput(true);

                if(body!=null){
                    //获取签名信息
                    String sign = GetSign(body,key);

                    //设置请求头
                    connection.setRequestProperty("key", key);
//                    connection.setRequestProperty("sign", sign);
                    Log.d("Debug", "body:"+new Gson().toJson(body));

                    //设置请求 body
                    byte[] bytes = new Gson().toJson(body).getBytes();
                    connection.setRequestProperty("Content-Length", Integer.toString(bytes.length));
                    try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                        outputStream.write(bytes);
                    }
                }

                // 获取响应结果
                int statusCode = connection.getResponseCode();
                if (statusCode == HttpURLConnection.HTTP_OK) {
                    inputStream = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        buffer.append(line);
                    }
                } else {
                    if(errorNotify!=null)errorNotify.Run("服务器返回错误，状态码：" + statusCode);
                    throw new IOException("服务器返回错误，状态码：" + statusCode);
                }
            }catch (Exception e){
                if(errorNotify!=null)errorNotify.Run("POST 请求异常:"+e.toString());
                Log.e("Error","POST 请求异常:"+e.toString());
                return;
            }finally {
                try{
                    // 关闭连接和流
                    if (reader != null)  reader.close();

                    if (inputStream != null)  inputStream.close();

                    if (connection != null)  connection.disconnect();

                }catch(Exception e){
                    Log.e("Error","POST 请求异常:"+e.toString());
                    return;
                }
            }

            try{
                APIResult res = new GsonBuilder().serializeNulls().create().fromJson(buffer.toString(), APIResult.class);
                if(res!=null)if(callback!=null) callback.Run(res);
            }catch(Exception e){
                if(errorNotify!=null)errorNotify.Run("POST 请求异常:"+e.toString());
                Log.e("Error","POST 请求异常:"+e.toString());
            }
//            if(callback!=null) callback.Run(buffer.toString());
        }).start();

    }

}
