package com.live.mylivewallpaper.request;

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


import org.json.JSONObject;

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

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
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 List<ResultData> shiftList  =new ArrayList<>();

    private List<ResultData> ExploreList  =new ArrayList<>();
    private List<ResultData> TrendingList  =new ArrayList<>();
    private List<ResultData> DnamicList  =new ArrayList<>();


    private RetrofitManager() {

        musicApi = getRetrofit().create(MusicApi.class);
    }
    private synchronized Retrofit getRetrofit() {
        if (retrofit == null) {
            long DEFAULT_TIMEOUT = 5;
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder()
//                    .addNetworkInterceptor(new GzipInterceptor())
                    .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                    .addInterceptor(httpLoggingInterceptor)
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(base_Host)
                    .client(client)
//                    .addConverterFactory(ScalarsConverterFactory.create()) // 支持返回 String 类型
//                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }

    public static RetrofitManager getInstance() {
        if (REQUEST_MANAGER == null) {
            synchronized (RetrofitManager.class) { //锁,防止线程问题
                if (REQUEST_MANAGER == null) {
                    REQUEST_MANAGER = new RetrofitManager();
                }
            }
        }
        return REQUEST_MANAGER;
    }


    public   void getMp4(int id, String image,String path, OnVideoResultListener listener) {
        musicApi.getMP4(id,"5eV6snEwfY7Yv6Ub",image,"DL8","ViewLive" )
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new ObserverWrapper<>(new OnRequestListener<ResponseBody>() {
                    @Override
                    public void onFail(String errorMsg) {
                        listener.onVideoResult(false,null);
                    }

                    @Override
                    public void onSuccess(ResponseBody data) {
                        boolean b = Common.writeFile(data.byteStream(), path);
                        listener.onVideoResult(b,path);
                    }


                }));
    }
}
