package com.kickstarter.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.kickstarter.R;
import com.kickstarter.libs.ActivityRequestCodes;
import com.kickstarter.libs.BaseActivity;
import com.kickstarter.libs.Paginator;
import com.kickstarter.libs.SwipeRefresher;
import com.kickstarter.libs.qualifiers.RequiresPresenter;
import com.kickstarter.models.Activity;
import com.kickstarter.models.Project;
import com.kickstarter.models.User;
import com.kickstarter.presenters.ActivityFeedPresenter;
import com.kickstarter.ui.adapters.ActivityFeedAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;

@RequiresPresenter(ActivityFeedPresenter.class)
public final class ActivityFeedActivity extends BaseActivity<ActivityFeedPresenter> {
  private ActivityFeedAdapter adapter;
  public @Bind(R.id.recycler_view) RecyclerView recyclerView;
  protected @Bind(R.id.activity_feed_swipe_refresh_layout) SwipeRefreshLayout swipeRefreshLayout;
  private Paginator paginator;
  private SwipeRefresher swipeRefresher;

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

    adapter = new ActivityFeedAdapter(presenter);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    paginator = new Paginator(recyclerView, presenter.inputs::nextPage);
    swipeRefresher = new SwipeRefresher(this, swipeRefreshLayout, presenter.inputs::refresh, presenter.outputs::isFetchingActivities);

    presenter.outputs.activities()
      .compose(bindToLifecycle())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(this::showActivities);
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    paginator.stop();
  }

  public void showActivities(@NonNull final List<Activity> activities) {
    adapter.takeActivities(activities);
  }

  public void showEmptyFeed(@Nullable final User user) {
    adapter.takeEmptyFeed(user);
  }

  public void activityFeedLogin() {
    final Intent intent = new Intent(this, LoginToutActivity.class)
      .putExtra(getString(R.string.intent_forward), true)
      .putExtra(getString(R.string.intent_login_type), LoginToutActivity.REASON_GENERIC);
    startActivityForResult(intent, ActivityRequestCodes.ACTIVITY_FEED_ACTIVITY_LOGIN_TOUT_ACTIVITY_USER_REQUIRED);
  }

  public void discoverProjectsButtonOnClick() {
    final Intent intent = new Intent(this, DiscoveryActivity.class);
    startActivity(intent);
  }

  public void showProjectUpdate(@NonNull final Activity activity) {
    final Intent intent = new Intent(this, DisplayWebViewActivity.class)
      .putExtra(getString(R.string.intent_url), activity.projectUpdateUrl());
    startActivityWithTransition(intent, R.anim.slide_in_right, R.anim.fade_out_slide_out_left);
  }

  public void startProjectActivity(@NonNull final Project project) {
    final Intent intent = new Intent(this, ProjectActivity.class)
      .putExtra(getString(R.string.intent_project), project);
    startActivityWithTransition(intent, R.anim.slide_in_right, R.anim.fade_out_slide_out_left);
  }

  @Override
  protected void onActivityResult(final int requestCode, final int resultCode, @NonNull final Intent intent) {
    if (requestCode != ActivityRequestCodes.ACTIVITY_FEED_ACTIVITY_LOGIN_TOUT_ACTIVITY_USER_REQUIRED) {
      return;
    }
    if (resultCode != RESULT_OK) {
      return;
    }
  }
}

@@ -332,16 +332,54 @@
 rView;%0A%0A
+import com.kickstarter.KSApplication;%0A
 import c
@@ -396,16 +396,16 @@
 rter.R;%0A
-
 import c
@@ -480,32 +480,73 @@
 s.BaseActivity;%0A
+import com.kickstarter.libs.CurrentUser;%0A
 import com.kicks
@@ -662,32 +662,79 @@
 uiresPresenter;%0A
+import com.kickstarter.libs.utils.ObjectUtils;%0A
 import com.kicks
@@ -970,16 +970,45 @@
 .List;%0A%0A
+import javax.inject.Inject;%0A%0A
 import b
@@ -1438,16 +1438,53 @@
 Layout;%0A
+%0A  @Inject CurrentUser currentUser;%0A%0A
   privat
@@ -1667,24 +1667,89 @@
 anceState);%0A
+    ((KSApplication) getApplication()).component().inject(this);%0A
     setConte
@@ -2040,24 +2040,24 @@
 :nextPage);%0A
-
     swipeRef
@@ -2176,24 +2176,277 @@
 tivities);%0A%0A
+    // Only allow refreshing if there's a current user%0A    currentUser.observable()%0A      .map(ObjectUtils::isNotNull)%0A      .compose(bindToLifecycle())%0A      .observeOn(AndroidSchedulers.mainThread())%0A      .subscribe(swipeRefreshLayout::setEnabled);%0A%0A
     presente

Disable pull to refresh when no logged in user