package com.epicodus.meetuplist.ui;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.epicodus.meetuplist.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import butterknife.Bind;
import butterknife.ButterKnife;

public class CreateAccountActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String TAG = CreateAccountActivity.class.getSimpleName();

    private FirebaseAuth.AuthStateListener mAuthListener;

    @Bind(R.id.createUserButton) Button mCreateUserButton;
    @Bind(R.id.nameEditText) EditText mNameEditText;
    @Bind(R.id.emailEditText) EditText mEmailEditText;
    @Bind(R.id.passwordEditText) EditText mPasswordEditText;
    @Bind(R.id.confirmPasswordEditText) EditText mConfirmPasswordEditText;
    @Bind(R.id.loginTextView) TextView mLoginTextView;

    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();

        createAuthStateListener();

        mLoginTextView.setOnClickListener(this);
        mCreateUserButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        if (view == mLoginTextView) {
            Intent intent = new Intent(CreateAccountActivity.this, LogInActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        }

        if (view == mCreateUserButton) {
            createNewUser();
        }

    }

    private void createNewUser() {
        final String name = mNameEditText.getText().toString().trim();
        final String email = mEmailEditText.getText().toString().trim();
        String password = mPasswordEditText.getText().toString().trim();
        String confirmPassword = mConfirmPasswordEditText.getText().toString().trim();

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Log.d(TAG, "Authentication successful");
                        } else {
                            Toast.makeText(CreateAccountActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    private void createAuthStateListener() {
        mAuthListener = new FirebaseAuth.AuthStateListener() {

            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                final FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    Intent intent = new Intent(CreateAccountActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }
            }

        };
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }

}
@@ -2470,24 +2470,263 @@
 ().trim();%0A%0A
+        boolean validEmail = isValidEmail(email);%0A        boolean validName = isValidName(name);%0A        boolean validPassword = isValidPassword(password, confirmPassword);%0A        if (!validEmail %7C%7C !validName %7C%7C !validPassword) return;%0A%0A
         mAut
@@ -4300,9 +4300,992 @@
 %0A    %7D%0A%0A
+    private boolean isValidEmail(String email) %7B%0A        boolean isGoodEmail =%0A                (email != null && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches());%0A        if (!isGoodEmail) %7B%0A            mEmailEditText.setError(%22Please enter a valid email address%22);%0A            return false;%0A        %7D%0A        return isGoodEmail;%0A    %7D%0A%0A    private boolean isValidName(String name) %7B%0A        if (name.equals(%22%22)) %7B%0A            mNameEditText.setError(%22Please enter your name%22);%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A    private boolean isValidPassword(String password, String confirmPassword) %7B%0A        if (password.length() %3C 6) %7B%0A            mPasswordEditText.setError(%22Please create a password containing at least 6 characters%22);%0A            return false;%0A        %7D else if (!password.equals(confirmPassword)) %7B%0A            mPasswordEditText.setError(%22Passwords do not match%22);%0A            return false;%0A        %7D%0A        return true;%0A    %7D%0A%0A
 %7D

Add form input validation to registration page