package com.live.mylivewallpaper.request;

import com.live.mylivewallpaper.data.ResultData;
import com.live.mylivewallpaper.help.Common;
import com.live.mylivewallpaper.listener.OnVideoResultListener;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
//import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
//import retrofit2.converter.gson.GsonConverterFactory;
//import retrofit2.converter.scalars.ScalarsConverterFactory;

public class RetrofitManager {

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

    private static volatile RetrofitManager REQUEST_MANAGER;

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


    private RetrofitManager() {

    }


    private synchronized Retrofit getRetrofit(ProgressResponseBody.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 ProgressInterceptor(listener));

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

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


    public Disposable getMp4(int id, String image, String path,ProgressResponseBody.ProgressListener progressListener, OnVideoResultListener listener) {
        Disposable subscribe = getRetrofit(progressListener).create(MusicApi.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 = Common.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, ProgressResponseBody.ProgressListener progressListener,OnVideoResultListener listener) {
        Disposable subscribe = getRetrofit(progressListener).create(MusicApi.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 = Common.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);
//                    }
//
//                }));
//    }
}
