<commit_before>package fr.miage.paris10.projetm1.helpu;

/**
 * Created by david on 24/01/2017.
 */

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.Bind;

public class SignupActivity extends AppCompatActivity {
    private static final String TAG = "SignupActivity";

    private FirebaseAuth mFirebaseAuth;
    private DatabaseReference mDatabaseReference;
    private Data data = new Data(this);

    @Bind(R.id.input_firstName) EditText _firstNameText;
    @Bind(R.id.input_lastName) EditText _lastNameText;
    @Bind(R.id.input_email) EditText _emailText;
    @Bind(R.id.spinner_level) Spinner _levelSpinner;
    @Bind(R.id.spinner_ufr) Spinner _ufrSpinner;
    @Bind(R.id.spinner_filliere) Spinner _filiereSpinner;
    @Bind(R.id.input_password) EditText _passwordText;
    @Bind(R.id.input_reEnterPassword) EditText _reEnterPasswordText;
    @Bind(R.id.btn_signup) Button _signupButton;
    @Bind(R.id.link_login) TextView _loginLink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        // Initialize FirebaseAuth
        mFirebaseAuth = FirebaseAuth.getInstance();
        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

        ButterKnife.bind(this);

        InputStream inputStream = getResources().openRawResource(R.raw.nanterrev2);
        data.insertData(inputStream);
        final Spinner spinFilliere = (Spinner) findViewById(R.id.spinner_filliere);
        final Spinner spinUfr = (Spinner) findViewById(R.id.spinner_ufr);
        ArrayList<String> listUfr= new ArrayList<String>();
        listUfr.add("Select UFR");
         listUfr.add("ARTS LETTRES LANGUES");
        listUfr.add("DROIT ECONOMIE GESTION");
        listUfr.add("SCIENCES ET TECHNIQUES DES ACTIVITES PHYSIQUES ET SPORTIVES");
        listUfr.add("SCIENCES HUMAINES ET SOCIALES");
        listUfr.add("SCIENCES TECHNOLOGIES ET SANTE");
        ArrayAdapter<String> adapterUfr=new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, listUfr);
        spinUfr.setAdapter(adapterUfr);
        spinUfr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(!parent.getItemAtPosition(position).toString().isEmpty())
                {
                    ArrayList<String> listFilliere=data.getAllFilliere(parent.getItemAtPosition(position).toString());
                    listFilliere.add(0,"Select Filliere");
                    ArrayAdapter<String> adapterFilliere=new ArrayAdapter<String>(SignupActivity.this, R.layout.spinner_layout, R.id.text, listFilliere);
                    spinFilliere.setAdapter(adapterFilliere);
                    adapterFilliere.notifyDataSetChanged();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });




        final Spinner spinLevel = (Spinner) findViewById(R.id.spinner_level);
        ArrayAdapter<CharSequence> adapterLevel = ArrayAdapter.createFromResource(this, R.array.level_array, android.R.layout.simple_spinner_item);
        adapterLevel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinLevel.setAdapter(adapterLevel);



        _signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                signup();

            }
        });

        _loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Finish the registration screen and return to the Login activity
                Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    public void signup() {
        Log.d(TAG, "Signup");

        if(!isOnline())
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
            builder.setMessage(R.string.internet_error_message)
                    .setTitle(R.string.signup_error_title);
            AlertDialog dialog = builder.create();
            dialog.show();
        }else{

            if (!validate()) {
                onSignupFailed();
                return;
            }

            // _signupButton.setEnabled(false);

            final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
                    R.style.AppTheme_Dark_Dialog);
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage("Creating Account...");
            progressDialog.show();


            new android.os.Handler().postDelayed(
                    new Runnable() {
                        public void run() {

                            String lastName = _lastNameText.getText().toString();
                            String firstName = _firstNameText.getText().toString();
                            String email = _emailText.getText().toString();
                            String level = _levelSpinner.getSelectedItem().toString();
                            String ufr = _ufrSpinner.getSelectedItem().toString();
                            String filiere = _filiereSpinner.getSelectedItem().toString();
                            String password = _passwordText.getText().toString();

                            register(email, password, lastName, firstName,level, ufr, filiere);

                            progressDialog.dismiss();

                        }

                    }, 3000);



        }
    }


    public void onSignupSuccess() {
        //_signupButton.setEnabled(true);
        setResult(RESULT_OK, null);
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "Sign up failed", Toast.LENGTH_LONG).show();

       // _signupButton.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String lastName = _lastNameText.getText().toString();
        String firstName = _firstNameText.getText().toString();<commit_msg>adding verification if a email already exists in data base in inscription<commit_after>package fr.miage.paris10.projetm1.helpu;

/**
 * Created by david on 24/01/2017.
 */

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
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 com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.ButterKnife;
import butterknife.Bind;

public class SignupActivity extends AppCompatActivity {
    private static final String TAG = "SignupActivity";

    private FirebaseAuth mFirebaseAuth;
    private DatabaseReference mDatabaseReference;
    private Data data = new Data(this);

    @Bind(R.id.input_firstName) EditText _firstNameText;
    @Bind(R.id.input_lastName) EditText _lastNameText;
    @Bind(R.id.input_email) EditText _emailText;
    @Bind(R.id.spinner_level) Spinner _levelSpinner;
    @Bind(R.id.spinner_ufr) Spinner _ufrSpinner;
    @Bind(R.id.spinner_filliere) Spinner _filiereSpinner;
    @Bind(R.id.input_password) EditText _passwordText;
    @Bind(R.id.input_reEnterPassword) EditText _reEnterPasswordText;
    @Bind(R.id.btn_signup) Button _signupButton;
    @Bind(R.id.link_login) TextView _loginLink;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        // Initialize FirebaseAuth
        mFirebaseAuth = FirebaseAuth.getInstance();
        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

        ButterKnife.bind(this);

        InputStream inputStream = getResources().openRawResource(R.raw.nanterrev2);
        data.insertData(inputStream);
        final Spinner spinFilliere = (Spinner) findViewById(R.id.spinner_filliere);
        final Spinner spinUfr = (Spinner) findViewById(R.id.spinner_ufr);
        ArrayList<String> listUfr= new ArrayList<String>();
        listUfr.add("Select UFR");
         listUfr.add("ARTS LETTRES LANGUES");
        listUfr.add("DROIT ECONOMIE GESTION");
        listUfr.add("SCIENCES ET TECHNIQUES DES ACTIVITES PHYSIQUES ET SPORTIVES");
        listUfr.add("SCIENCES HUMAINES ET SOCIALES");
        listUfr.add("SCIENCES TECHNOLOGIES ET SANTE");
        ArrayAdapter<String> adapterUfr=new ArrayAdapter<String>(this, R.layout.spinner_layout, R.id.text, listUfr);
        spinUfr.setAdapter(adapterUfr);
        spinUfr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(!parent.getItemAtPosition(position).toString().isEmpty())
                {
                    ArrayList<String> listFilliere=data.getAllFilliere(parent.getItemAtPosition(position).toString());
                    listFilliere.add(0,"Select Filliere");
                    ArrayAdapter<String> adapterFilliere=new ArrayAdapter<String>(SignupActivity.this, R.layout.spinner_layout, R.id.text, listFilliere);
                    spinFilliere.setAdapter(adapterFilliere);
                    adapterFilliere.notifyDataSetChanged();
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });




        final Spinner spinLevel = (Spinner) findViewById(R.id.spinner_level);
        ArrayAdapter<CharSequence> adapterLevel = ArrayAdapter.createFromResource(this, R.array.level_array, android.R.layout.simple_spinner_item);
        adapterLevel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinLevel.setAdapter(adapterLevel);



        _signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                signup();

            }
        });

        _loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Finish the registration screen and return to the Login activity
                Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    public void signup() {
        Log.d(TAG, "Signup");

        if(!isOnline())
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(SignupActivity.this);
            builder.setMessage(R.string.internet_error_message)
                    .setTitle(R.string.signup_error_title);
            AlertDialog dialog = builder.create();
            dialog.show();
        }else{

            if (!validate()) {
                onSignupFailed();
                return;
            }

            // _signupButton.setEnabled(false);

            final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
                    R.style.AppTheme_Dark_Dialog);
            progressDialog.setIndeterminate(true);
            progressDialog.setMessage("Creating Account...");
            progressDialog.show();


            new android.os.Handler().postDelayed(
                    new Runnable() {
                        public void run() {

                            String lastName = _lastNameText.getText().toString();
                            String firstName = _firstNameText.getText().toString();
                            String email = _emailText.getText().toString();
                            String level = _levelSpinner.getSelectedItem().toString();
                            String ufr = _ufrSpinner.getSelectedItem().toString();
                            String filiere = _filiereSpinner.getSelectedItem().toString();
                            String password = _password<PASSWORD>.getText().toString();

                            register(email, password, lastName, firstName,level, ufr, filiere);

                            progressDialog.dismiss();

                        }

                    }, 3000);



        }
    }


    public void onSignupSuccess() {
        //_signupButton.setEnabled(true);
        setResult(RESULT_OK, null);
        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "This email already exists", Toast.LENGTH_LONG).show();

       // _signupButton.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String lastName = _lastNameText.getText().toString();
        String firstName = _firstNameText.getText().toString();
{'fraction_non_alphanumeric': 0.09875058461949622, 'fraction_numerical': 0.002538918955034409, 'mean_word_length': 2.5937575030012003, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793520', 'n_tokens_mistral': 4158, 'n_tokens_neox': 3976, 'n_words': 723}