public class RegisterActivity extends AppCompatActivity {
    private TextInputLayout textInputEmail;
    private TextInputLayout textInputPassword;
    private TextInputLayout textInputPhoneNumber;
    private Button btnSignUp;
     private FirebaseDatabase firebaseDatabase;
     private DatabaseReference databaseReference;
    private static final Pattern PHONE_PATTERN = Pattern.compile("\\d{10}|(?:\\d{3}-){2}\\d{4}|\\(\\d{3}\\)\\d{3}-?\\d{4}");
    private static final Pattern PASSWORD_PATTERN =
            Pattern.compile("^" +
                    //"(?=.*[0-9])" +         //at least 1 digit
                    //"(?=.*[a-z])" +         //at least 1 lower case letter
                    //"(?=.*[A-Z])" +         //at least 1 upper case letter
                    "(?=.*[a-zA-Z])" +      //any letter
                    "(?=.*[@#$%^&+=])" +    //at least 1 special character
                    "(?=\\S+$)" +           //no white spaces
                    ".{4,}" +               //at least 4 characters
                    "$");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        textInputEmail = findViewById(R.id.txtInputEmail);
        textInputPassword = findViewById(R.id.txtPassword);
        textInputPhoneNumber = findViewById(R.id.txtPhoneNumber);
        View mView = getLayoutInflater().inflate(R.layout.activity_register, null);
        btnSignUp = findViewById(R.id.btnSignUp);
        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
           firebaseDatabase = FirebaseDatabase.getInstance();
           databaseReference = firebaseDatabase.getReference();
           String email = textInputEmail.getEditText().getText().toString();
           String password = textInputPassword.getEditText().getText().toString();
           String phoneNumber = textInputPhoneNumber.getEditText().getText().toString();
           SignUpHelperClass helperClass = new SignUpHelperClass(email,phoneNumber, password);

           databaseReference.child(phoneNumber).setValue(helperClass);

            }

        });

    }

    public boolean validateEmail() {
        String email = textInputEmail.getEditText().getText().toString().trim();
        if (email.isEmpty()) {
            textInputEmail.requestFocus();
            textInputEmail.setError("Field can not be empty");
            return false;

        } else {
            if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
                textInputEmail.setError("Invalid email");
                return false;

            } else textInputEmail.setError(null);
            return true;
        }
    }

    public boolean validatePhoneNumber() {
        String phoneNumberInput = textInputPhoneNumber.getEditText().getText().toString().trim();
        if (phoneNumberInput.isEmpty()) {
            textInputPhoneNumber.setError("Field can not be empty");
            return false;
        } else if (!PHONE_PATTERN.matcher(phoneNumberInput).matches()) {
            textInputPhoneNumber.setError("Invalid phone number");
            return false;
        } else {
            textInputPhoneNumber.setError(null);
            return true;
        }
    }

    private boolean validatePassword() {
        String passwordInput = textInputPassword.getEditText().getText().toString();
        if (passwordInput.isEmpty()) {
            textInputPassword.setError("Field can not be empty");
            return false;
        } else if (!PASSWORD_PATTERN.matcher(passwordInput).matches()) {
            textInputPassword.setError("Password is too weak");
            return false;
        } else {
            textInputPassword.setError(null);
            return true;
        }
    }
}
