package com.yutou.jianr_mg.plugins.Activitys.Utils;


import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;

import com.yutou.jianr_mg.plugins.Interfaces.PluginHttpInterface;


public class PluginHttpTools {
    private static final String TAG = PluginHttpTools.class.getName();
    private static PluginHttpTools network;

    private PluginHttpTools() {
    }

    public static PluginHttpTools init() {
        if (network == null) {
            network = new PluginHttpTools();
        }
        return network;
    }

    public void httpGet(final String url, final PluginHttpInterface networkInterface) {
        System.out.println("HTTP>>>>"+url);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    connection.connect();
                    String tmp, str = "";
                    while ((tmp = reader.readLine()) != null) {
                        str += tmp;
                    }
                    reader.close();
                    networkInterface.httpGetData(str, connection.getResponseCode());
                    Log.i(TAG + "[" + url + "]", "body:" + str + " (" + connection.getResponseCode() + ")");
                } catch (Exception e) {
                    e.printStackTrace();
                    networkInterface.httpError(e);
                    Log.e(TAG, url + "\n" + e);
                }
            }
        }).start();

    }
    public static void post(final String url, final JSONObject body, final PluginHttpInterface networkInterface) throws Exception{
        System.out.println(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(2);
                String tmp, str = "";
                try {
                    System.out.println(3);
                    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                    connection.connect();
                    System.out.println(4);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    while ((tmp = reader.readLine()) != null) {
                        str += tmp;
                    }
                    Log.i(TAG, "[域名]" + url + " = [body]" + body.toString() + " -> [接收] " + str);

                    //   Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")");
                    if (networkInterface != null)
                        networkInterface.httpGetData(str, connection.getResponseCode());

                } catch (Exception e) {
                    if (networkInterface != null)
                        networkInterface.httpError(e);
                    e.printStackTrace();

                }
            }
        }).start();

    }


    public String toGetSplice(JSONObject json) {
        if (json == null || json.toString().equals("{}")) {
            return "";
        }
        String string = "";
        Iterator<String> keys = json.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            try {
                string += "&" + key + "=" + json.getString(key);
            } catch (JSONException e) {
                e.printStackTrace();
                try {
                    string += "&" + URLEncoder.encode(key, "UTF-8") + "=";
                    //    string += "&" + key + "=";
                } catch (Exception e1) {
                    string += "&" + key + "=";
                }
            }
        }
        string = string.substring(1, string.length()).replaceAll(" ", "");
        return string;
    }


    private static int reindex = 0;


}
