package com.casic.dcms.utils;

import android.util.Log;

import com.casic.dcms.utils.callback.IHttpRequestListener;

import org.jetbrains.annotations.NotNull;

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

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class HttpRequestHelper {
    private static final String TAG = "HttpRequestHelper";

    public static void doRequest(String function, String requestValue, IHttpRequestListener listener) {
        String ip = (String) SaveKeyValues.getValue(Constant.IP_KEY, "http://111.198.10.15:11409");
//        String ip = (String) SaveKeyValues.getValue(Constant.IP_KEY, "http://223.82.47.232:11401");
        Observable.create(new Observable.OnSubscribe<ResponseBody>() {
            @Override
            public void call(Subscriber<? super ResponseBody> subscriber) {
                HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(@NotNull String message) {
                        Log.d(TAG, "log: " + message);
                    }
                });
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                OkHttpClient client = new OkHttpClient.Builder()
                        .addInterceptor(interceptor)
                        .readTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .connectTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .build();

                Request request = new Request.Builder()
                        .addHeader("token", AuthenticationHelper.getToken())
                        .url(ip + function + requestValue)
                        .get()
                        .build();//创建Request 对象
                try {
                    Response response = client.newCall(request).execute();
                    subscriber.onNext(response.body());
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                listener.onFailure(e);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                listener.onSuccess(responseBody);
            }
        });
    }

    public static void doRequest(String url, IHttpRequestListener listener) {
        Observable.create(new Observable.OnSubscribe<ResponseBody>() {
            @Override
            public void call(Subscriber<? super ResponseBody> subscriber) {
                HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(@NotNull String message) {
                        Log.d(TAG, "log: " + message);
                    }
                });
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                OkHttpClient client = new OkHttpClient.Builder()
                        .addInterceptor(interceptor)
                        .readTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .connectTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .writeTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
                        .build();

                Request request = new Request.Builder()
                        .addHeader("token", AuthenticationHelper.getToken())
                        .url(url)
                        .get()
                        .build();//创建Request 对象
                try {
                    Response response = client.newCall(request).execute();
                    subscriber.onNext(response.body());
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseBody>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                listener.onFailure(e);
            }

            @Override
            public void onNext(ResponseBody responseBody) {
                listener.onSuccess(responseBody);
            }
        });
    }
}
