package com.kaviz.heal.Activities.Authentication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
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.kaviz.heal.Activities.UserDetailsActivity;
import com.kaviz.heal.Model.User;
import com.kaviz.heal.databinding.ActivityRegisterBinding;
import java.util.Objects;

public class RegisterActivity extends AppCompatActivity {

    ActivityRegisterBinding binding;

    //firebase objects
    private FirebaseAuth auth;

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

        //initialize objects
        auth = FirebaseAuth.getInstance();

        //for navigate registration activity
        binding.alreadyUserText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(RegisterActivity.this, MainActivity.class));

            }
        });

        //for handle register button
        binding.registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                registerButtonProcess();

            }
        });

    }
    //for handle register button process
    private void registerButtonProcess() {

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

        if (email.isEmpty()) {

            binding.emailText.setError("Enter Email");

        } else if (password.isEmpty()) {

            binding.passwordText.setError("Enter Password");

        } else {

            binding.progressBar.setVisibility(View.VISIBLE);
            binding.registerButton.setEnabled(false);
            //for register user
            registerUser(new User(email, password));

        }

    }

    //for register user
    private void registerUser(User user) {

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

                if (task.isSuccessful()) {

                    showToast("Successfully Registered!");
                    Intent intent = new Intent(RegisterActivity.this, UserDetailsActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);

                } else {

                    showToast("Error - "+ Objects.requireNonNull(task.getException()).getMessage());

                }

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

            }
        });

    }

    // For hide the status bar.
    private void makeFullScreen() {

        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Hide the app bar.
        if (getSupportActionBar() != null) {

            getSupportActionBar().hide();

        }

    }

    private void showToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

}