package cn.ben.tvdemo.data.tvtype.source.remote;

import com.google.gson.Gson;

import java.util.List;

import cn.ben.tvdemo.constant.Constants;
import cn.ben.tvdemo.data.tvtype.TVTypes;
import cn.ben.tvdemo.data.tvtype.source.TVTypesDataSource;
import io.reactivex.Observable;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Query;

public class TVTypesRemoteDataSource implements TVTypesDataSource {
    private static final String LOAD_TV_TYPES_BASE_URL = "http://api.avatardata.cn/TVTime/";
    private volatile static TVTypesRemoteDataSource instance = null;

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

    private TVTypesRemoteDataSource() {
    }

    @Override
    public Observable<List<TVTypes.TVType>> getTVTypes() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(LOAD_TV_TYPES_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        TVTypesService service = retrofit.create(TVTypesService.class);
        Observable<TVTypes> call = service.getTVTypes(Constants.API_KEY);

        TVTypes tvTypes = new Gson().fromJson("{\n" +
                "    \"result\": [\n" +
                "        {\n" +
                "            \"id\": \"1\",\n" +
                "            \"name\": \"央视\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"2\",\n" +
                "            \"name\": \"卫视\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"3\",\n" +
                "            \"name\": \"数字\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"4\",\n" +
                "            \"name\": \"城市\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"5\",\n" +
                "            \"name\": \"CETV\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"id\": \"6\",\n" +
                "            \"name\": \"原创\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"error_code\": 0,\n" +
                "    \"reason\": \"Succes\"\n" +
                "}", TVTypes.class);
        return Observable.just(tvTypes.getResult());

//        return call
//                .map(new Function<TVTypes, List<TVTypes.TVType>>() {
//                    @Override
//                    public List<TVTypes.TVType> apply(TVTypes tvTypes) throws Exception {
//                        if (tvTypes.getError_code() != 0)
//                            throw new Exception(tvTypes.getReason());
//                        return tvTypes.getResult();
//                    }
//                });
    }

    @Override
    public void deleteAllTVTypes() {
    }

    @Override
    public void saveTVType(TVTypes.TVType tvType) {
    }

    @Override
    public void cleanup() {
        instance = null;
    }

    interface TVTypesService {
        @GET("Query")
        Observable<TVTypes> getTVTypes(@Query("key") String key);
    }
}

@@ -271,16 +271,56 @@
 rvable;%0A
+import io.reactivex.functions.Function;%0A
 import r
@@ -338,16 +338,16 @@
 trofit;%0A
-
 import r
@@ -1605,1309 +1605,8 @@
 );%0A%0A
-        TVTypes tvTypes = new Gson().fromJson(%22%7B%5Cn%22 +%0A                %22    %5C%22result%5C%22: %5B%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%221%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22%E5%A4%AE%E8%A7%86%5C%22%5Cn%22 +%0A                %22        %7D,%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%222%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22%E5%8D%AB%E8%A7%86%5C%22%5Cn%22 +%0A                %22        %7D,%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%223%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22%E6%95%B0%E5%AD%97%5C%22%5Cn%22 +%0A                %22        %7D,%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%224%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22%E5%9F%8E%E5%B8%82%5C%22%5Cn%22 +%0A                %22        %7D,%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%225%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22CETV%5C%22%5Cn%22 +%0A                %22        %7D,%5Cn%22 +%0A                %22        %7B%5Cn%22 +%0A                %22            %5C%22id%5C%22: %5C%226%5C%22,%5Cn%22 +%0A                %22            %5C%22name%5C%22: %5C%22%E5%8E%9F%E5%88%9B%5C%22%5Cn%22 +%0A                %22        %7D%5Cn%22 +%0A                %22    %5D,%5Cn%22 +%0A                %22    %5C%22error_code%5C%22: 0,%5Cn%22 +%0A                %22    %5C%22reason%5C%22: %5C%22Succes%5C%22%5Cn%22 +%0A                %22%7D%22, TVTypes.class);%0A        return Observable.just(tvTypes.getResult());%0A%0A//
     
@@ -1621,18 +1621,16 @@
 rn call%0A
-//
         
@@ -1690,18 +1690,16 @@
 e%3E%3E() %7B%0A
-//
         
@@ -1720,18 +1720,16 @@
 verride%0A
-//
         
@@ -1810,18 +1810,16 @@
 ption %7B%0A
-//
         
@@ -1868,18 +1868,16 @@
 ) != 0)%0A
-//
         
@@ -1938,18 +1938,16 @@
 son());%0A
-//
         
@@ -1990,18 +1990,16 @@
 sult();%0A
-//
         
@@ -2012,18 +2012,16 @@
       %7D%0A
-//
         

correct remote