package com.tofly.ghrq_route.common.util.net.http;

import android.content.Context;
import android.util.Log;

import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.common.ui.view.PipeAnalyzeResultBottomSheet;
import com.tofly.ghrq_route.common.util.net.NetUtil;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by zombiexq on 2016/6/13.
 */
public class HttpUtil {
    private String httpProtocol = "http://";

    private Context context;

    public HttpUtil(Context context) {
        this.context = context;
    }


    private OkHttpClient getOkHttpClient() {
        return BaseApp.getInstance().okHttpClient;
    }

    /**
     * HTTP的post方法
     *
     * @param url
     * @param formBody
     * @return
     */
    public String execute_post(String url, RequestBody formBody) {
        String result;
        if (NetUtil.getNetworkState(context) != NetUtil.NETWORN_NONE) {
            OkHttpClient okHttpClient = getOkHttpClient();
            Request request = new Request.Builder().
                    url(url)
                    .post(formBody)
                    .build();

            try {
                Response response = okHttpClient.newCall(request).execute();
                Log.w("TAG", "execute_post: response = " + response);
                if (response.isSuccessful()){
                    result = response.body().string();
                }else {
                    result = "请求失败"+response.code();
                }
                response.body().close();
            } catch (Exception e) {
                   result = "请求错误";
            }

        } else {
            result = "网络连接错误";
        }
        Log.w("TAG", "execute_post: 返回的result +" + result);
        return result;
    }


    /**
     * HTTP的get方法
     *
     * @param url
     * @return
     */
    public String execute_get(String url) {
        String result;
        if (NetUtil.getNetworkState(context)!=NetUtil.NETWORN_NONE){
            OkHttpClient okHttpClient = getOkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = null;
            try {
                response = okHttpClient.newCall(request).execute();
                if (response.isSuccessful()){
//                    Headers responseHeaders = response.headers();
                    result = response.body().string();
                    Log.w("TAG", "execute_get: result = " +result );
                }else {
                    result = "请求失败"+response.code();
                }
                response.body().close();
            } catch (Exception e) {
                result = "请求发生错误";
            }
        } else {
            result = "网络连接错误";
        }
        Log.w("TAG", "execute_get:返回的result = " +result );
        return result;
    }

    public void execute_get(String url, final PipeAnalyzeResultBottomSheet.GisCallback gisCallback) {
        StringBuilder result = new StringBuilder();
        if (NetUtil.getNetworkState(context) != NetUtil.NETWORN_NONE) {
            OkHttpClient okHttpClient = getOkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            try {
                Response response = okHttpClient.newCall(request).execute();

//                Log.w("TAG", "execute_get: response = " + response);
                if (response.isSuccessful()) {
//                    Headers responseHeaders = response.headers();
                    result.append(response.body().string());
                    if (gisCallback != null) {
                        gisCallback.success(result.toString());
                    }
//                    Log.w("TAG", "execute_get: result = " + result.toString());
                } else {
                    result.append("请求失败");
                    result.append(response.code());
                    if (gisCallback != null) {
                        gisCallback.error(result.toString());
                    }
                }
                response.body().close();
            } catch (Exception e) {
                result.append("请求发生错误\n");
                result.append(e.getMessage());
                if (gisCallback != null) {
                    gisCallback.error(result.toString());
                }
            }
        } else {
            result.append("网络连接错误");
            if (gisCallback != null) {
                gisCallback.netError(result.toString());
            }
        }
    }

}
