package com.ycgis.macall.personalcenter.p.request;

import com.rs.macall.androidx.basemodel.request.OkHttpUtils;
import com.ycgis.macall.personalcenter.m.requestbean.BaseRequestModel;
import com.ycgis.macall.personalcenter.p.app.RuanseeApplication;
import com.ycgis.macall.personalcenter.p.request.interceptors.HttpLoggingInterceptor;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Author macall
 * @Create 2019/12/4 0004
 * @Describe
 */
public class RetrofitService {

    private static RetrofitApi retrofitService;

    public static void resetRetrofitApi() {
        retrofitService = new Retrofit.Builder()
                .baseUrl(RuanseeApplication.getAppCache().getBaseUrlPath())
                .client(OkHttpUtils.getClient(10, 10, 10,new HttpLoggingInterceptor()))
                .addConverterFactory(new NullOnEmptyConverterFactory())//处理接口返回没有响应体body
                // 添加Gson转换器
                .addConverterFactory(GsonConverterFactory.create())
//            .addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
//                    .setLenient()
//                    .create()
//            ))
                // 添加Retrofit到RxJava的转换器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(
                        RetrofitApi.class);
    }

//    private void init(){
//        retrofitService = new Retrofit.Builder()
//                .baseUrl(RuanseeApplication.getAppCache().getBaseUrlPath())
//                .addConverterFactory(new NullOnEmptyConverterFactory())//处理接口返回没有响应体body
//                // 添加Gson转换器
//                .addConverterFactory(GsonConverterFactory.create())
////            .addConverterFactory(GsonConverterFactory.create(new GsonBuilder()
////                    .setLenient()
////                    .create()
////            ))
//                // 添加Retrofit到RxJava的转换器
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                .client(OkHttpUtils.getClient(10, 10, 10))
//                .build()
//                .create(RetrofitApi.class);
//    }


    //获得RetrofitService对象
    public static RetrofitApi getBaseInstance() {
        try {
            if (retrofitService == null) {
                resetRetrofitApi();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resetRetrofitApi();
        }
        return retrofitService;
    }

    public static class NullOnEmptyConverterFactory extends Converter.Factory {

        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            final Converter<ResponseBody, ?> delegate = retrofit.nextResponseBodyConverter(this, type, annotations);
            return new Converter<ResponseBody, Object>() {
                @Override
                public Object convert(ResponseBody body) throws IOException {
                    if (body.contentLength() == 0) {
                        if (type == BaseRequestModel.class) {
                            BaseRequestModel baseRequestModel = new BaseRequestModel() {
                            };
                            baseRequestModel.setCode(-1);
                            baseRequestModel.setMsg("未知错误！");
                            return baseRequestModel;
                        }
                        return null;
                    }
                    return delegate.convert(body);
                }
            };
        }
    }


}
