package aashrai.android.gettowork.view.activity;

import aashrai.android.gettowork.GoToWorkApplication;
import aashrai.android.gettowork.R;
import aashrai.android.gettowork.adapter.PackageListAdapter;
import aashrai.android.gettowork.di.component.SettingsComponent;
import aashrai.android.gettowork.presenter.SettingsActivityPresenter;
import aashrai.android.gettowork.utils.AppListDecorator;
import aashrai.android.gettowork.view.SettingsView;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.Bind;
import com.jakewharton.rxbinding.widget.RxTextView;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class SettingsActivity extends BaseActivity
    implements SettingsView, Action1<CharSequence>, TextView.OnEditorActionListener {

  @Bind(R.id.rv_packages) RecyclerView packageList;
  @Bind(R.id.progress) ProgressBar progressBar;
  @Bind(R.id.et_search) EditText search;
  @Inject SettingsActivityPresenter presenter;
  PackageListAdapter adapter;
  CompositeSubscription compositeSubscription;
  boolean firstSearchFlag = true;
  private static final String TAG = "SettingsActivity";
  SettingsComponent settingsComponent;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    presenter.setView(this);
    configureSearch();
  }

  private void configureSearch() {
    search.setCompoundDrawablesWithIntrinsicBounds(null, null,
        ContextCompat.getDrawable(this, R.drawable.ic_search), null);
    search.setOnEditorActionListener(this);
    compositeSubscription = new CompositeSubscription();
    compositeSubscription.add(RxTextView.textChanges(search)
        .debounce(100, TimeUnit.MILLISECONDS)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(this));
  }

  @Override public void configureDagger() {
    settingsComponent =
        ((GoToWorkApplication) getApplication()).getApplicationComponent().getSettingsComponent();
    settingsComponent.inject(this);
  }

  @Override public int getLayoutId() {
    return R.layout.activity_settings;
  }

  @Override public void configureRecyclerView(PackageListAdapter adapter) {
    this.adapter = adapter;
    packageList.setLayoutManager(new LinearLayoutManager(this));
    packageList.addItemDecoration(new AppListDecorator());
    packageList.setAdapter(this.adapter);
  }

  @Override public void updatePackageListAdapter(List<ApplicationInfo> packageList) {
    Log.d(TAG, "updatePackageListAdapter: " + packageList.size());
    adapter.updatePackageList(packageList);
  }

  @Override public void startProgressBar() {
    progressBar.setVisibility(View.VISIBLE);
  }

  @Override public void stopProgressBar() {
    progressBar.setVisibility(View.GONE);
  }

  @Override public void call(CharSequence charSequence) {
    if (!firstSearchFlag) {
      presenter.onSearch(charSequence.toString());
    } else {
      firstSearchFlag = false;
    }
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    compositeSubscription.unsubscribe();
    presenter.onDestroy();
    settingsComponent = null;
  }

  @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE) {
      InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
      return true;
    }
    return false;
  }
}



@@ -705,33 +705,8 @@
 ew;%0A
-import android.util.Log;%0A
 impo
@@ -727,24 +727,24 @@
 w.KeyEvent;%0A
+
 import andro
@@ -1025,24 +1025,60 @@
 RxTextView;%0A
+import java.lang.ref.WeakReference;%0A
 import java.
@@ -1686,42 +1686,8 @@
 on;%0A
-  boolean firstSearchFlag = true;%0A
   pr
@@ -2396,20 +2396,71 @@
 bscribe(
-this
+new SearchBarSubscriber(new WeakReference%3C%3E(presenter))
 ));%0A  %7D%0A
@@ -3116,75 +3116,8 @@
 ) %7B%0A
-    Log.d(TAG, %22updatePackageListAdapter: %22 + packageList.size());%0A
     
@@ -3409,38 +3409,8 @@
 ) %7B%0A
-    if (!firstSearchFlag) %7B%0A  
     
@@ -3458,58 +3458,8 @@
 ));%0A
-    %7D else %7B%0A      firstSearchFlag = false;%0A    %7D%0A
   %7D%0A
@@ -3937,16 +3937,16 @@
 ;%0A    %7D%0A
-
     retu
@@ -3958,13 +3958,424 @@
 lse;%0A  %7D
+%0A%0A  private static class SearchBarSubscriber implements Action1%3CCharSequence%3E %7B%0A%0A    private final SettingsActivityPresenter presenter;%0A%0A    private SearchBarSubscriber(WeakReference%3CSettingsActivityPresenter%3E presenterWeakReference) %7B%0A      presenter = presenterWeakReference.get();%0A    %7D%0A%0A    @Override public void call(CharSequence charSequence) %7B%0A      presenter.onSearch(charSequence.toString());%0A    %7D%0A  %7D
 %0A%7D%0A%0A%0A

Remove unnecessary first search check