package com.kickstarter.presenters;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.kickstarter.KSApplication;
import com.kickstarter.libs.CurrentUser;
import com.kickstarter.libs.Presenter;
import com.kickstarter.libs.utils.StringUtils;
import com.kickstarter.presenters.inputs.SignupPresenterInputs;
import com.kickstarter.presenters.outputs.SignupPresenterOutputs;
import com.kickstarter.services.ApiClient;
import com.kickstarter.services.apiresponses.AccessTokenEnvelope;
import com.kickstarter.ui.activities.SignupActivity;

import javax.inject.Inject;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.subjects.PublishSubject;

public class SignupPresenter extends Presenter<SignupActivity> implements SignupPresenterInputs, SignupPresenterOutputs {

  // INPUTS
  private final PublishSubject<String> fullName = PublishSubject.create();
  private final PublishSubject<String> email = PublishSubject.create();
  private final PublishSubject<String> password = PublishSubject.create();
  private final PublishSubject<Boolean> sendNewsletter = PublishSubject.create();
  private final PublishSubject<View> signupClick = PublishSubject.create();

  // OUTPUTS
  private final PublishSubject<Void> signupSuccessSubject = PublishSubject.create();
  public final Observable<Void> signupSuccess() { return signupSuccessSubject.asObservable(); }

  @Inject ApiClient client;
  @Inject CurrentUser currentUser;

  public SignupPresenterInputs inputs() { return this; }
  public SignupPresenterOutputs outputs() { return this; }

  @Override
  public void fullName(@NonNull final String s) { fullName.onNext(s); }
  @Override
  public void email(@NonNull final String s) { email.onNext(s); }
  @Override
  public void password(@NonNull final String s) { password.onNext(s); }
  @Override
  public void sendNewsletter(@NonNull final Boolean b) { sendNewsletter.onNext(b); }
  @Override
  public void signupClick(@NonNull final View v) { signupClick.onNext(v); }

  @Override
  protected void onCreate(@NonNull final Context context, @Nullable Bundle savedInstanceState) {
    super.onCreate(context, savedInstanceState);
    ((KSApplication) context.getApplicationContext()).component().inject(this);

  }

  private static boolean isValid(@NonNull final String fullName, @NonNull final String email, @NonNull final String password) {
    return fullName.length() > 0 && StringUtils.isEmail(email) && password.length() > 0;
  }

  private void submit(@NonNull final String fullName, @NonNull final String email, @NonNull final String password) {
    client.signup(fullName, email, password, password, 1) // TODO: get newsletter value
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe(this::success); // TODO: errors
  }

  private void success(@NonNull final AccessTokenEnvelope envelope) {
    currentUser.login(envelope.user(), envelope.accessToken());
    signupSuccessSubject.onNext(null);
  }
}

@@ -316,24 +316,67 @@
 .Presenter;%0A
+import com.kickstarter.libs.utils.RxUtils;%0A
 import com.k
@@ -961,16 +961,504 @@
 puts %7B%0A%0A
+  final private class SignupData %7B%0A    @NonNull final String fullName;%0A    @NonNull final String email;%0A    @NonNull final String password;%0A%0A    public SignupData(@NonNull final String fullName, @NonNull final String email, @NonNull final String password) %7B%0A      this.fullName = fullName;%0A      this.email = email;%0A      this.password = password;%0A    %7D%0A%0A    public boolean isValid() %7B%0A      return fullName.length() %3E 0 && StringUtils.isEmail(email) && password.length() %3E 0;%0A    %7D%0A  %7D%0A%0A
   // INP
@@ -2279,16 +2279,20 @@
 ing s) %7B
+%0A   
  fullNam
@@ -2295,35 +2295,38 @@
 lName.onNext(s);
+%0A 
  %7D%0A
+%0A
   @Override%0A  pu
@@ -2902,226 +2902,346 @@
 %0A%0A  
-%7D%0A%0A  private static boolean isValid(@NonNull final String fullName, @NonNull final String email, @NonNull final String password) %7B%0A    return fullName.length() %3E 0 && StringUtils.isEmail(email) && password.length() %3E 0
+  final Observable%3CSignupData%3E signupData = Observable.combineLatest(fullName, email, password, SignupData::new);%0A%0A    addSubscription(%0A      RxUtils.takePairWhen(viewSubject, signupData)%0A        .observeOn(AndroidSchedulers.mainThread())%0A        .subscribe(vd -%3E %7B%0A          vd.first.setFormEnabled(vd.second.isValid());%0A        %7D)%0A    )
 ;%0A  

add subscription to validate signup data and enable button