package com.example.ugotthis;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Register extends AppCompatActivity  {

    //Variable
    private final AppCompatActivity activity = Register.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
    }

    public void register(View view) {

        DatabaseHelper databaseHelper = new DatabaseHelper(activity);
        User user = new User();

        // User input fields.
        final EditText userName = (EditText) findViewById(R.id.editText_userNameR);
        final EditText email = (EditText) findViewById(R.id.editText_emailR);
        final EditText password = (EditText) findViewById(R.id.editText_PasswordR);
        final EditText rePassword = (EditText) findViewById(R.id.editText_rePasswordR);

        String newUser = userName.getText().toString();
        String newPassword = password.getText().toString();
        String reNewPassword = rePassword.getText().toString();
        String newEmail = email.getText().toString();

        // Validation
        if (userName.length()==0){
            userName.setError("Please enter Username !");
        }
        else {
            if (password.length()==0){
                password.setError("Please enter Password !");
            }
            else {

                if (rePassword.length()==0) {
                    rePassword.setError("Please enter Password !");
                }
                else {
                    if (email.length()==0){
                        email.setError("Please enter Email !");
                    }
                    else {
                        if (newPassword.equals(reNewPassword)) {

                            // Create an user object
                            user.setName(newUser);
                            user.setEmail(newEmail);
                            user.setPassword(newPassword);

                            // Send user to database helper
                            databaseHelper.addUser(user);


                            // Redirect user to MainActivity
                            Intent mainActivity = new Intent(Register.this, MainActivity.class);
                            startActivity(mainActivity);

                        }
                        else {
                            Toast.makeText(Register.this, "Password and rePassword doesn't match!", Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        }
    }
}
