package io.reark.rxgithubapp.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

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

import io.reark.reark.utils.Preconditions;
import io.reark.reark.utils.RxViewBinder;
import io.reark.reark.utils.TextWatcherObservable;
import io.reark.rxgithubapp.R;
import io.reark.rxgithubapp.pojo.GitHubRepository;
import io.reark.rxgithubapp.viewmodels.RepositoriesViewModel;
import io.reark.rxgithubapp.viewmodels.RepositoriesViewModel.ProgressStatus;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;

/**
 * Created by ttuo on 06/01/15.
 */
public class RepositoriesView extends FrameLayout {

    private TextView statusText;

    private Observable<String> searchStringObservable;

    private RecyclerView repositoriesListView;

    private RepositoriesAdapter repositoriesAdapter;

    public RepositoriesView(Context context) {
        super(context, null);
    }

    public RepositoriesView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        EditText editText = (EditText) findViewById(R.id.repositories_search);
        searchStringObservable = TextWatcherObservable.create(editText);

        statusText = (TextView) findViewById(R.id.repositories_status_text);

        repositoriesAdapter = new RepositoriesAdapter(Collections.emptyList());

        repositoriesListView = (RecyclerView) findViewById(R.id.repositories_list_view);
        repositoriesListView.setHasFixedSize(true);
        repositoriesListView.setLayoutManager(new LinearLayoutManager(getContext()));
        repositoriesListView.setAdapter(repositoriesAdapter);
    }

    private void setRepositories(@NonNull List<GitHubRepository> repositories) {
        Preconditions.checkNotNull(repositories, "Repository List Text cannot be null.");
        Preconditions.checkState(repositoriesAdapter != null, "List Adapter should not be null.");

        repositoriesAdapter.set(repositories);
    }

    private void setNetworkRequestStatus(@NonNull ProgressStatus networkRequestStatus) {
        Preconditions.checkNotNull(networkRequestStatus, "Network Request Status cannot be null.");

        String networkStatusText = "";
        switch (networkRequestStatus) {
            case LOADING:
                networkStatusText = "Loading..";
                break;
            case ERROR:
                networkStatusText = "Error occurred";
                break;
            case IDLE:
                networkStatusText = "";
                break;
        }
        setNetworkRequestStatusText(networkStatusText);
    }

    private void setNetworkRequestStatusText(@NonNull String networkRequestStatusText) {
        Preconditions.checkNotNull(networkRequestStatusText, "Network Status Text cannot be null.");
        Preconditions.checkState(statusText != null, "Status Text View should not be null.");

        statusText.setText(networkRequestStatusText);
    }


    public static class ViewBinder extends RxViewBinder {
        private RepositoriesView view;
        private RepositoriesViewModel viewModel;

        public ViewBinder(@NonNull final RepositoriesView view,
                          @NonNull final RepositoriesViewModel viewModel) {
            Preconditions.checkNotNull(view, "View cannot be null.");
            Preconditions.checkNotNull(viewModel, "ViewModel cannot be null.");

            this.view = view;
            this.viewModel = viewModel;
        }

        @Override
        protected void bindInternal(@NonNull final CompositeSubscription s) {
            s.add(viewModel.getRepositories()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(view::setRepositories));
            s.add(viewModel.getNetworkRequestStatusText()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(view::setNetworkRequestStatus));
            s.add(view.searchStringObservable
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(viewModel::setSearchString));
            s.add(Observable.create(
                    subscriber -> {
                        view.repositoriesAdapter.setOnClickListener(
                                this::repositoriesAdapterOnClick);
                        subscriber.add(Subscriptions.create(() ->
                                view.repositoriesAdapter.setOnClickListener(null)));
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe());
        }

        private void repositoriesAdapterOnClick(View clickedView) {
            final int itemPosition = view.repositoriesListView.getChildAdapterPosition(clickedView);
            GitHubRepository repository = view.repositoriesAdapter.getItem(itemPosition);
            viewModel.selectRepository(repository);
        }
    }
}

@@ -421,46 +421,8 @@
 ist;
-%0Aimport java.util.concurrent.TimeUnit;
 %0A%0Aim
@@ -2629,38 +2629,170 @@
     
+setNetworkRequestStatusText(getLoadingStatus
 String
- 
+(
 network
-StatusText = %22%22;
+RequestStatus));%0A    %7D%0A%0A    private String getLoadingStatusString(ProgressStatus networkRequestStatus) %7B
 %0A   
@@ -2870,35 +2870,22 @@
         
-networkStatusText =
+return
  %22Loadin
@@ -2890,39 +2890,16 @@
 ing..%22;%0A
-                break;%0A
         
@@ -2930,35 +2930,22 @@
         
-networkStatusText =
+return
  %22Error 
@@ -2971,81 +2971,39 @@
     
-    break;%0A            case IDLE:%0A                networkStatusText = %22%22;
+case IDLE:%0A            default:
 %0A   
@@ -3019,13 +3019,17 @@
     
-b
 re
-ak
+turn %22%22
 ;%0A  
@@ -3040,64 +3040,8 @@
   %7D%0A
-        setNetworkRequestStatusText(networkStatusText);%0A
     

Use helper method when setting loading status