package me.notify.notifyinfo;

import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Info 网络请求类
 * @Auth Bello
 * @Time 19-2-28 下午2:58
 * @Ver
 */
public class ServerHelper {
    private static final String TAG = ServerHelper.class.getSimpleName();
    private static ServerHelper instance = null;

//    private static String baseUrl = "http://192.168.6.166:7079/save?regID=";
//    private static String baseUrl = "http://192.168.102.183:7079/save?regID=";
    private static String baseUrl = "http://103.26.77.90:7080/save?regID=";

    public static ServerHelper getInstance() {
        if (instance == null) {
            instance = new ServerHelper();
        }
        return instance;
    }


    public void getBaseUrl(final String id, final Callback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    if (null ==id || id.isEmpty()) return;

                    Request request = new Request.Builder()
                            .url(baseUrl + id)
                            .build();

                    Response response = new OkHttpClient().newCall(request).execute();
                    if (response.isSuccessful()) {
                        callback.onListener(response.body().string());
                    } else {
                        callback.onListener("");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onListener("");
                }
            }
        }).start();

    }

    /**
     * POST网络请求（表单）
     *
     * @param url
     * @param body
     * @return
     * @throws IOException
     */
    private String post(String url, FormBody body) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(25, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String rsp = response.body().string();
            //            Log.d(TAG, "post response => " + url +", \n"+ rsp);
            return rsp;
        } else {
            return "";
        }
    }

    /**
     * POST网络请求（json）
     *
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    private String post(String url, String json) throws IOException {
        Log.e(TAG, "post url => " + url);
        Log.e(TAG, "post json => " + json);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);


        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(25, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String rsp = response.body().string();
            Log.d(TAG, "post response => " + rsp);
            return rsp;
        } else {
            return "";
        }
    }


    /**
     * POST网络请求（上传 ）
     *
     * @param url
     * @param body
     * @return
     * @throws IOException
     */
    private String post(String url, MultipartBody body) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(25, TimeUnit.SECONDS)
                .readTimeout(15, TimeUnit.SECONDS)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            String rsp = response.body().string();
            //            Log.d(TAG, "post response => " + url +", \n"+ rsp);
            return rsp;
        } else {
            return "";
        }
    }


    /**
     * 获取位置数据
     *
     * @param user
     * @param time
     * @param callback
     */
    public void getLocation(final String user, final String time, final Callback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    FormBody.Builder builder = new FormBody.Builder();
                    builder.add("user", user);
                    builder.add("time", time);

//                    String res = post(GET_LOCATION, builder.build());

//                    callback.onListener(res);

                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onListener("");
                }

            }
        }).start();
    }


    /**
     * 异步回调接口
     */
    public interface Callback {
        void onListener(String result);
    }

}
