package com.free.video.wallpaper.httpcontent;

import com.free.video.wallpaper.listener.OnNetDataApi;
import com.free.video.wallpaper.helper.Commontool;
import com.free.video.wallpaper.listener.OnVideoresultListener;

import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;


public class RetrofitHelper {

    private String base_Host = "https://neutrolabgames.com/";

    private static volatile RetrofitHelper REQUEST_MANAGER;

    private Retrofit retrofit;
//    public MediaType JSON = MediaType.get("application/json; charset=utf-8");
    private OnNetDataApi onNetDataApi;
    private  Retrofit.Builder builder;
    private OkHttpClient.Builder clientBuilder;


    private RetrofitHelper() {

    }


    private synchronized Retrofit getRetrofit(NetBody.ProgressListener listener) {
        if (builder == null) {
//            retrofit = new Retrofit.Builder()
//                    .baseUrl(base_Host)
//                    .client(client)
////                    .addConverterFactory(ScalarsConverterFactory.create()) // 支持返回 String 类型
////                    .addConverterFactory(GsonConverterFactory.create())
//                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                    .build();

          builder = new Retrofit.Builder()
                    .baseUrl(base_Host)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create());

        }
        if(clientBuilder == null){
            long DEFAULT_TIMEOUT = 5;
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            clientBuilder = new OkHttpClient.Builder()
//                    .addNetworkInterceptor(new GzipInterceptor())
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        }
        clientBuilder.addInterceptor(new NetLanjie(listener));

        return builder.client(clientBuilder.build()).build();
    }

    public static RetrofitHelper getInstance() {
        if (REQUEST_MANAGER == null) {
            synchronized (RetrofitHelper.class) {
                if (REQUEST_MANAGER == null) {
                    REQUEST_MANAGER = new RetrofitHelper();
                }
            }
        }
        return REQUEST_MANAGER;
    }


    public Disposable getMp4(int id, String image, String path, NetBody.ProgressListener progressListener, OnVideoresultListener listener) {
        Disposable subscribe = getRetrofit(progressListener).create(OnNetDataApi.class).getMP4(id, "5eV6snEwfY7Yv6Ub", image, "DL8", "ViewLive")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(ResponseBody body) throws Exception {
                        boolean b = Commontool.writeFile(body.byteStream(), path);
                        listener.onVideoResult(b, path);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        listener.onVideoResult(false, throwable.getMessage());
                    }
                });
        return subscribe;
    }


    public Disposable getShiftMp4(int id, String image, String path, NetBody.ProgressListener progressListener, OnVideoresultListener listener) {
        Disposable subscribe = getRetrofit(progressListener).create(OnNetDataApi.class).getShiftMP4(id, "5eV6snEwfY7Yv6Ub", image, "DL8", "ViewShiftLive")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(ResponseBody body) throws Exception {
                        boolean b = Commontool.writeFile(body.byteStream(), path);
                        listener.onVideoResult(b, path);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        listener.onVideoResult(false, throwable.getMessage());
                    }
                });
        return subscribe;
    }


//    public   void getDnamicMp4(int id, String image,String path, OnVideoResultListener listener) {
//        musicApi.getDMP4(id,"5eV6snEwfY7Yv6Ub",image,"DL8","ViewTimerLive","1" )
//                .subscribeOn(Schedulers.io())
//                .unsubscribeOn(Schedulers.io())
//                .observeOn(Schedulers.io())
//                .subscribe(new ObserverWrapper<>(new OnRequestListener<ResponseBody>() {
//                    @Override
//                    public void onFail(String errorMsg) {
//                        listener.onVideoResult(false,errorMsg);
//                    }
//
//                    @Override
//                    public void onSuccess(ResponseBody data) {
//                        boolean b = Common.writeFile(data.byteStream(), path);
//                        listener.onVideoResult(b,path);
//                    }
//
//                }));
//    }
}
