package com.example.vdcscore;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.textfield.TextInputEditText;
import com.google.firebase.auth.FirebaseAuth;

public class CriarContaActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_criarconta);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        TextInputEditText editPassword2;
        TextInputEditText editEmail;
        TextInputEditText editUserName;
        TextInputEditText editConfirmPassword;
        Button btnCreateAccount;
        TextView txtGoLogin;

        editEmail = findViewById(R.id.editEmail);
        editUserName = findViewById(R.id.editUserName);
        editPassword2 = findViewById(R.id.editPassword2);
        editConfirmPassword = findViewById(R.id.editConfirmPassword);
        btnCreateAccount = findViewById(R.id.btnCreateAccount);
        txtGoLogin = findViewById(R.id.txtGoLogin);

        // Criar conta
        btnCreateAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = editEmail.getText().toString().trim();
                String userName = editUserName.getText().toString().trim();
                String pass = editPassword2.getText().toString().trim();
                String conf = editConfirmPassword.getText().toString().trim();

                if (email.isEmpty() || userName.isEmpty() || pass.isEmpty() || conf.isEmpty()) {
                    Toast.makeText(CriarContaActivity.this, "Preencha todos os campos!", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!pass.equals(conf)) {
                    Toast.makeText(CriarContaActivity.this, "As passwords não coincidem!", Toast.LENGTH_SHORT).show();
                    return;
                }

                FirebaseAuth auth = FirebaseAuth.getInstance();
                auth.createUserWithEmailAndPassword(email, pass).addOnCompleteListener(CriarContaActivity.this,
                        task -> {
                            if (task.isSuccessful()) {
                                // Atualizar nome de utilizador
                                com.google.firebase.auth.FirebaseUser user = auth.getCurrentUser();
                                if (user != null) {
                                    com.google.firebase.auth.UserProfileChangeRequest profileUpdates = new com.google.firebase.auth.UserProfileChangeRequest.Builder()
                                            .setDisplayName(userName)
                                            .build();

                                    user.updateProfile(profileUpdates).addOnCompleteListener(updateTask -> {
                                        Intent intent = new Intent(CriarContaActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();
                                    });
                                } else {
                                    Intent intent = new Intent(CriarContaActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            } else {
                                String errorMessage = com.example.vdcscore.utils.FirebaseErrorUtils.getErrorMessagePt(task.getException());
                                Toast.makeText(CriarContaActivity.this, errorMessage, Toast.LENGTH_LONG).show();
                            }

                        });
            }
        });

        // Voltar ao login
        txtGoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CriarContaActivity.this, LoginActivity.class);
                startActivity(intent);
            }

        });
    }
}