package com.cloudycrew.cloudycar.signup;

import com.cloudycrew.cloudycar.ViewController;
import com.cloudycrew.cloudycar.controllers.UserController;
import com.cloudycrew.cloudycar.email.Email;
import com.cloudycrew.cloudycar.models.PhoneNumber;
import com.cloudycrew.cloudycar.models.User;
import com.cloudycrew.cloudycar.scheduling.ISchedulerProvider;
import com.cloudycrew.cloudycar.utils.ObservableUtils;

/**
 * Created by George on 2016-11-09.
 */

public class SignUpController extends ViewController<ISignUpView> {
    private UserController userController;
    private ISchedulerProvider schedulerProvider;

    public SignUpController(UserController userController, ISchedulerProvider schedulerProvider) {
        this.userController = userController;
        this.schedulerProvider = schedulerProvider;
    }

    public void registerUser(String username, String email, String phoneNumber) {
        try {
            User newUser = createUser(username, email, phoneNumber);
            registerUser(newUser);
        } catch (Exception e) {
            dispatchOnMalformedUserFailure();
        }
    }

    private void registerUser(User user) {
        ObservableUtils.fromAction(userController::createUser, user)
                       .subscribeOn(schedulerProvider.ioScheduler())
                       .observeOn(schedulerProvider.mainThreadScheduler())
                       .subscribe(nothing -> dispatchOnSuccessfulRegistration(),
                                  throwable -> dispatchOnDuplicateUsernameFailure());

    }

    private User createUser(String username, String email, String phoneNumber) {
        User newUser = new User(username);
        newUser.setEmail(new Email(email));
        newUser.setPhoneNumber(new PhoneNumber(email));

        return newUser;
    }


    private void dispatchOnMalformedUserFailure() {
        if (isViewAttached()) {
            getView().onMalformedUserFailure();
        }
    }

    private void dispatchOnDuplicateUsernameFailure() {
        if (isViewAttached()) {
            getView().onDuplicateUsernameFailure();
        }
    }

    private void dispatchOnSuccessfulRegistration() {
        if (isViewAttached()) {
            getView().onSuccessfulRegistration();
        }
    }
}

@@ -404,16 +404,17 @@
 Utils;%0A%0A
+%0A
 /**%0A * C
@@ -1757,21 +1757,27 @@
 eNumber(
-email
+phoneNumber
 ));%0A%0A   
@@ -1804,17 +1804,16 @@
 %0A    %7D%0A%0A
-%0A
     priv

Use actual phone number when creating new user