package com.example.cuida.ui.Inicial;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.example.cuida.R;
import com.example.cuida.ui.MainActivity;
import com.google.firebase.auth.FirebaseAuth;

public class iniciar_sessao extends AppCompatActivity {

    private ImageView imageView;

    private EditText emailEditText;
    private EditText palavraPasseEditText;
    private Button entrarButton;
    private Button criarContaButton;
    private TextView esqueciAPalavraPasseButton;

    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();

        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;
        });

        imageView = findViewById(R.id.imageView);
        emailEditText = findViewById(R.id.emailEditText);
        palavraPasseEditText = findViewById(R.id.palavraPasseEditText);
        entrarButton = findViewById(R.id.entarButton);
        criarContaButton = findViewById(R.id.criarContaButton);
        esqueciAPalavraPasseButton = findViewById(R.id.esqueciAPalavraPasseButton);


        entrarButton.setOnClickListener(v -> iniciarSessao());

        criarContaButton.setOnClickListener(v -> {
            Intent intent = new Intent(iniciar_sessao.this, Criar_conta.class);
            startActivity(intent);
        });

        esqueciAPalavraPasseButton.setOnClickListener(v -> {
            Intent intent = new Intent(iniciar_sessao.this, Esqueci_a_palavra_passe.class);
            startActivity(intent);
        });
    }

    private void iniciarSessao() {
        String email = emailEditText.getText().toString().trim();
        String password = palavraPasseEditText.getText().toString().trim();

        if (email.isEmpty()) {
            emailEditText.setError("O e-mail é obrigatório");
            emailEditText.requestFocus();
            return;
        }

        if (password.isEmpty()) {
            palavraPasseEditText.setError("A palavra-passe é obrigatória");
            palavraPasseEditText.requestFocus();
            return;
        }

        mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, task -> {
                    if (task.isSuccessful()) {
                        Log.d("FIREBASE_AUTH", "signInWithEmail:success");
                        Toast.makeText(iniciar_sessao.this, "Login bem-sucedido.", Toast.LENGTH_SHORT).show();
                        navegarParaMainActivity();
                    } else {
                        Log.w("FIREBASE_AUTH", "signInWithEmail:failure", task.getException());
                        Toast.makeText(iniciar_sessao.this, "Falha na autenticação. Verifique suas credenciais.", Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void navegarParaMainActivity() {
        Intent intent = new Intent(iniciar_sessao.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
}