package com.hanks.rxsearch;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jakewharton.rxbinding.widget.RxTextView;

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

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
import retrofit.http.GET;
import retrofit.http.Query;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.et_keyword) EditText et_keyword;
    @Bind(R.id.tv_result)  TextView tv_result;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        RestAdapter retrofit = new RestAdapter.Builder().setEndpoint("https://suggest.taobao.com")
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new GsonConverter(new Gson()))
                .build();

        final SearchService service = retrofit.create(SearchService.class);

        RxTextView.textChanges(et_keyword)
                // 上面的对 tv_result 的操作需要在主线程
                .subscribeOn(AndroidSchedulers.mainThread())
                .debounce(1000, TimeUnit.MILLISECONDS,AndroidSchedulers.mainThread())
                .filter(new Func1<CharSequence, Boolean>() {
                    @Override public Boolean call(CharSequence charSequence) {
                        // 清空搜索出来的结构
                        tv_result.setText("");
                        //当 EditText 中文字大于0的时候
                        return charSequence.length() > 0;
                    }
                })
                .switchMap(new Func1<CharSequence, Observable<Data>>() {
                    @Override public Observable<Data> call(CharSequence charSequence) {
                        // 搜索
                        return service.searchProdcut("utf-8", charSequence.toString());
                    }
                })
                // 网络操作在io线程
                .subscribeOn(Schedulers.io())
                //将 data 转换成 ArrayList<ArrayList<String>>
                .map(new Func1<Data, ArrayList<ArrayList<String>>>() {
                    @Override public ArrayList<ArrayList<String>> call(Data data) {
                        return data.result;
                    }
                })
                // 将 ArrayList<ArrayList<String>> 中每一项提取出来 ArrayList<String>
                .flatMap(new Func1<ArrayList<ArrayList<String>>, Observable<ArrayList<String>>>() {
                    @Override public Observable<ArrayList<String>> call(ArrayList<ArrayList<String>> arrayLists) {
                        return Observable.from(arrayLists);
                    }
                })
                .filter(new Func1<ArrayList<String>, Boolean>() {
                    @Override public Boolean call(ArrayList<String> strings) {
                        return strings.size() >= 2;
                    }
                })
                .map(new Func1<ArrayList<String>, String>() {
                    @Override public String call(ArrayList<String> strings) {
                        return "[商品名称:" + strings.get(0) + ", ID:" + strings.get(1) + "]\n";
                    }
                })
                //
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override public void call(String charSequence) {
                        showpop(charSequence);
                    }
                });

    }

    private void showpop(String result) {
        tv_result.append(result);
    }

    interface SearchService {

        @GET("/sug") Observable<Data> searchProdcut(@Query("code") String code, @Query("q") String keyword);
    }

    class Data {
        public ArrayList<ArrayList<String>> result;
    }

    //    https://suggest.taobao.com/sug?code=utf-8&q=%E6%89%8B%E6%9C%BA
}

@@ -152,32 +152,61 @@
 widget.TextView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport com.goo
@@ -1546,16 +1546,17 @@
 SECONDS,
+ 
 AndroidS
@@ -3569,233 +3569,815 @@
   //
-%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Action1%3CString%3E() %7B%0A                    @Override public void call(String charSequence) %7B%0A                        showpop(charSequence
+ %E5%8F%91%E7%94%9F%E9%94%99%E8%AF%AF%E5%90%8E%E4%B8%8D%E8%A6%81%E8%B0%83%E7%94%A8 onError%EF%BC%8C%E8%80%8C%E6%98%AF%E8%BD%AC%E5%88%B0 onErrorResumeNext%0A                .onErrorResumeNext(new Func1%3CThrowable, Observable%3C? extends String%3E%3E() %7B%0A                    @Override public Observable%3C? extends String%3E call(Throwable throwable) %7B%0A                        return Observable.just(%22error result%22);%0A                    %7D%0A                %7D)%0A                //%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Action1%3CString%3E() %7B%0A                    @Override public void call(String charSequence) %7B%0A                        showpop(charSequence);%0A                    %7D%0A                %7D, new Action1%3CThrowable%3E() %7B%0A                    @Override public void call(Throwable throwable) %7B%0A                        Toast.makeText(MainActivity.this, %22error%22, Toast.LENGTH_SHORT).show(
 );%0A 

use onErrorResumeNext