package com.mjsheng.myappstore.network;

import android.os.Environment;
import android.util.Log;


import com.mjsheng.myappstore.network.api.AppLimitApi;
import com.mjsheng.myappstore.network.api.BrankPicApi;
import com.mjsheng.myappstore.network.api.CategoryPicApi;
import com.mjsheng.myappstore.network.api.DeselectBrowserIDApi;
import com.mjsheng.myappstore.network.api.DeselectIDApi;
import com.mjsheng.myappstore.network.api.ForceDownloadApi;
import com.mjsheng.myappstore.network.api.NetAndLaunchApi;
import com.mjsheng.myappstore.network.api.ProjectPicApi;
import com.mjsheng.myappstore.network.api.RankAppApi;
import com.mjsheng.myappstore.network.api.SystemSettingApi;
import com.mjsheng.myappstore.network.api.UpdateApi;
import com.mjsheng.myappstore.network.api.UploadAppInfoApi;

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

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.CallAdapter;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者 mjsheng
 * 日期 2018/5/7 04:10
 * 邮箱 501802639@qq.com
 * 来自：
 */

public class Network {

    //    public static final String ROOT_URL = "http://www.as.xueshibao.com.cn/api/";
//    public static final String ROOT_URL = "https://www.jiaoguanyi.com/api/";//正式
    public static final String ROOT_URL = "http://www.jiaoguanyi.cn/api/";//测试


    private static UploadAppInfoApi uploadAppInfoApi;
    private static DeselectIDApi deselectIDApi;
    private static DeselectBrowserIDApi deselectBrowserIDApi;
    private static RankAppApi rankAppApi;
    private static CategoryPicApi categoryPicApi;
    private static ProjectPicApi projectPicApi;
    private static BrankPicApi brankPicApi;
    private static AppLimitApi appLimitApi;
    private static SystemSettingApi systemSettingApi;
    private static UpdateApi updateApi;
    private static NetAndLaunchApi netAndLaunchApi;
    private static ForceDownloadApi forceDownloadApi;

    private static final long cacheSize = 1024 * 1024 * 20;// 缓存文件最大限制大小20M
    private static String cacheDirectory = Environment.getExternalStorageDirectory() + "/okttpcaches"; // 设置缓存文件路径
    private static Cache cache = new Cache(new File(cacheDirectory), cacheSize);  //
    private static final OkHttpClient cacheClient;

    static {
        //如果无法生存缓存文件目录，检测权限使用已经加上，检测手机是否把文件读写权限禁止了
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(8, TimeUnit.SECONDS); // 设置连接超时时间
        builder.writeTimeout(8, TimeUnit.SECONDS);// 设置写入超时时间
        builder.readTimeout(8, TimeUnit.SECONDS);// 设置读取数据超时时间
        builder.retryOnConnectionFailure(true);// 设置进行连接失败重试
        //        builder.cache(cache);// 设置缓存
        cacheClient = builder.build();
    }

    private static OkHttpClient okHttpClient = new OkHttpClient();
    //    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();
    private static CallAdapter.Factory rxJavaCallAdapterFactory = RxJava2CallAdapterFactory.create();
    private static Converter.Factory gsonConverterFactory = GsonConverterFactory.create();

    public static UploadAppInfoApi getUploadAppInfoApi() {
        if (uploadAppInfoApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            uploadAppInfoApi = retrofit.create(UploadAppInfoApi.class);
        }
        Log.e("mjsheng", "UploadAppInfoApi");
        return uploadAppInfoApi;
    }

    public static DeselectIDApi getDeselectIDApi() {
        if (deselectIDApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            deselectIDApi = retrofit.create(DeselectIDApi.class);
        }
        Log.e("mjsheng", "DeselectIDApi");
        return deselectIDApi;
    }

    public static DeselectBrowserIDApi getDeselectBrowserIDApi() {
        if (deselectBrowserIDApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            deselectBrowserIDApi = retrofit.create(DeselectBrowserIDApi.class);
        }
        Log.e("mjsheng", "DeselectIDApi");
        return deselectBrowserIDApi;
    }

    public static RankAppApi getRankAppApi() {
        if (rankAppApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            rankAppApi = retrofit.create(RankAppApi.class);
        }
        Log.e("mjsheng", "RankAppApi");
        return rankAppApi;
    }

    public static CategoryPicApi getCategoryPicApi() {
        if (categoryPicApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            categoryPicApi = retrofit.create(CategoryPicApi.class);
        }
        Log.e("mjsheng", "CategoryPicApi");
        return categoryPicApi;
    }

    public static ProjectPicApi getProjectPicApi() {
        if (projectPicApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            projectPicApi = retrofit.create(ProjectPicApi.class);
        }
        Log.e("mjsheng", "projectPicApi");
        return projectPicApi;
    }

    public static BrankPicApi getBrankPicApi() {
        if (brankPicApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            brankPicApi = retrofit.create(BrankPicApi.class);
        }
        Log.e("mjsheng", "brankPicApi");
        return brankPicApi;
    }

    public static AppLimitApi getAppLimitApi() {
        if (appLimitApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            appLimitApi = retrofit.create(AppLimitApi.class);
        }
        Log.e("mjsheng", "appLimitApi");
        return appLimitApi;
    }

    public static SystemSettingApi getSystemSettingApi() {
        if (systemSettingApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            systemSettingApi = retrofit.create(SystemSettingApi.class);
        }
        Log.e("mjsheng", "systemSettingApi");
        return systemSettingApi;
    }

    public static UpdateApi getUpdateApi() {
        if (updateApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            updateApi = retrofit.create(UpdateApi.class);
        }
        Log.e("mjsheng", "systemSettingApi");
        return updateApi;
    }
    public static NetAndLaunchApi getNetAndLaunchApi() {
        if (netAndLaunchApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            netAndLaunchApi = retrofit.create(NetAndLaunchApi.class);
        }
        Log.e("mjsheng", "netAndLaunchApi");
        return netAndLaunchApi;
    }
    public static ForceDownloadApi getForceDownloadApi() {
        if (forceDownloadApi == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .client(cacheClient)
                    .baseUrl(ROOT_URL)
                    .addConverterFactory(gsonConverterFactory)
                    .addCallAdapterFactory(rxJavaCallAdapterFactory)
                    .build();
            forceDownloadApi = retrofit.create(ForceDownloadApi.class);
        }
        Log.e("mjsheng", "forceDownloadApi");
        return forceDownloadApi;
    }
}
