package com.kickstarter.presenters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.kickstarter.libs.ApiCapabilities;
import com.kickstarter.libs.Presenter;
import com.kickstarter.libs.RxUtils;
import com.kickstarter.models.Project;
import com.kickstarter.ui.activities.CheckoutActivity;
import com.kickstarter.ui.activities.ThanksActivity;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subjects.PublishSubject;
import timber.log.Timber;

public class CheckoutPresenter extends Presenter<CheckoutActivity> {
  private final PublishSubject<Void> loginSuccess = PublishSubject.create();

  private Project project;

  @Override
  protected void onCreate(final Context context, final Bundle savedInstanceState) {
    super.onCreate(context, savedInstanceState);

    RxUtils.takeWhen(viewSubject, loginSuccess)
      .take(1)
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(this::checkoutNext);
  }

  public void takeProject(final Project project) {
    this.project = project;
  }

  public void takeLoginSuccess() {
    loginSuccess.onNext(null);
  }

  public void takeSignupUriRequest() {
    view().startLoginToutActivity();
  }

  public void takeCheckoutThanksUriRequest() {
    view().startThanksActivity(project);
  }

  private void checkoutNext(final CheckoutActivity activity) {
    final String javascript = "root.checkout_next();";
    if (ApiCapabilities.evaluateJavascript()) {
      activity.webView.evaluateJavascript(javascript, null);
    } else {
      activity.webView.loadUrl("javascript:" + javascript);
    }
  }
}

@@ -858,57 +858,222 @@
 ils.
-takeWhen(viewSubject, loginSuccess)%0A      .take(1
+combineLatestPair(viewChange, loginSuccess)%0A      .filter(viewChangeAndLoginSuccess -%3E viewChangeAndLoginSuccess.first != null)%0A      .take(1)%0A      .map(viewChangeAndLoginSuccess -%3E viewChangeAndLoginSuccess.first
 )%0A  

Revert checkout behavior - view might not exist on login success