package com.hesiii.i_hear.Activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
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.database.FirebaseDatabase;
import com.hesiii.i_hear.Models.User;
import com.hesiii.i_hear.databinding.ActivitySignUpBinding;

public class SignUpActivity extends AppCompatActivity {
    private ActivitySignUpBinding binding;
    private FirebaseAuth auth;
    private FirebaseDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySignUpBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        auth = FirebaseAuth.getInstance();
        database = FirebaseDatabase.getInstance();

        binding.submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                submitButtonProcess();

            }
        });

    }

    private void submitButtonProcess() {

        String email = binding.emailText.getText().toString();
        String password = binding.passwordText.getText().toString();
        String name = binding.fullNameText.getText().toString();

        if (email.isEmpty()) {

            Toast.makeText(this, "Enter Email", Toast.LENGTH_SHORT).show();

        } else if (password.isEmpty()) {

            Toast.makeText(this, "Enter Password", Toast.LENGTH_SHORT).show();

        } else if (name.isEmpty()) {

            Toast.makeText(this, "Enter Name", Toast.LENGTH_SHORT).show();

        } else {

            binding.progressBar.setVisibility(View.VISIBLE);
            binding.submitButton.setEnabled(false);
            signUpProcess(email, password, name);

        }

    }

    private void signUpProcess(String email, String password, String name) {

        auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {

                            writeDataToDB(email, name);

                        } else {

                            Toast.makeText(SignUpActivity.this, "Error - "+task.getException().getMessage(), Toast.LENGTH_LONG).show();
                            binding.progressBar.setVisibility(View.GONE);
                            binding.submitButton.setEnabled(true);

                        }

                    }
                });

    }

    private void writeDataToDB(String email, String name) {

        User user = new User(auth.getCurrentUser().getUid(), email, name);

        database.getReference().child("Users").child(auth.getCurrentUser().getUid())
                .setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {

                        if (task.isSuccessful()) {

                            Toast.makeText(SignUpActivity.this, "Successfully Registered!", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(SignUpActivity.this, HomeActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);

                        } else {

                            Toast.makeText(SignUpActivity.this, "Error - "+task.getException().getMessage(), Toast.LENGTH_LONG).show();

                        }

                        binding.progressBar.setVisibility(View.GONE);
                        binding.submitButton.setEnabled(true);

                    }
                });

    }
}