package com.duben.microtribe.net;


import android.content.Context;
import android.text.TextUtils;

import com.duben.library.net.neterror.BaseSubscriber;
import com.duben.library.net.neterror.Throwable;
import com.duben.microtribe.BuildConfig;
import com.duben.microtribe.MintsApplication;
import com.duben.microtribe.manager.AppHttpManager;
import com.duben.microtribe.mvp.model.BaseResponse;
import com.duben.microtribe.utils.AESUtils;
import com.orhanobut.logger.Logger;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.http.Body;
import retrofit2.http.POST;
import rx.Observable;

/**
 * 描述：V6服务器交互
 * 作者：孟崔广
 * 时间：2023/6/1 16:14
 */
public interface V6Service {

    String HYTECH_MAIN_IP = "http://api6.mints-tech.cn/camera-api/";

    @POST("appNa/registerBefore")
    Observable<Object> saveV6TerminalInfoBefore(@Body Map<String, Object> vo);

    class Factory {
        public static V6Service create() {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();

            builder.retryOnConnectionFailure(false);
            builder.connectTimeout(20, TimeUnit.SECONDS);
            builder.readTimeout(20, TimeUnit.SECONDS);
            builder.writeTimeout(20, TimeUnit.SECONDS);

            if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor(message -> {
                    if (TextUtils.isEmpty(message)) return;
                    String s = message.substring(0, 1);
                    String request = message.substring(0, 4);
                    if ("{".equals(s) || "[".equals(s)) {
                        Logger.json(message);
                    } else if (request.contains("-->") || request.contains("<--")) {
                        Logger.d("Method" + message);
                    } else {
                        Logger.d("params:" + message);
                    }
                });
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);
                builder.interceptors().add(logging);
            }

            OkHttpInterceptor okHttpInterceptor = new OkHttpInterceptor(AESUtils.getDefaultKey());
            builder.interceptors().add(okHttpInterceptor);

            Retrofit retrofit = new Retrofit.Builder()
                    .client(builder.build())
                    .baseUrl(HYTECH_MAIN_IP)
                    .addConverterFactory(V6GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            return retrofit.create(V6Service.class);
        }
    }
}
