package com.example.cuida.ui.auth;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.cuida.MainActivity;
import com.example.cuida.data.AppDatabase;
import com.example.cuida.data.dao.UserDao;
import com.example.cuida.data.model.User;
import com.example.cuida.databinding.ActivityLoginBinding;

public class LoginActivity extends AppCompatActivity {
    // gvjhbk
    private ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Check if user is already logged in
        // Check if user is already logged in and wants to be remembered
        SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
        boolean isLoggedIn = prefs.getBoolean("is_logged_in", false);
        boolean rememberMe = prefs.getBoolean("remember_me", false);

        if (isLoggedIn && rememberMe) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
            return;
        }

        binding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.loginButton.setOnClickListener(v -> login());
        binding.registerLink.setOnClickListener(v -> {
            startActivity(new Intent(this, RegisterActivity.class));
            finish();
        });

        binding.forgotPasswordLink.setOnClickListener(v -> {
            startActivity(new Intent(this, ForgotPasswordActivity.class));
        });
    }

    private void login() {
        String email = binding.emailEditText.getText().toString();
        String password = binding.passwordEditText.getText().toString();

        if (email.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "Preencha todos os campos", Toast.LENGTH_SHORT).show();
            return;
        }

        AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
        UserDao userDao = db.userDao();

        AppDatabase.databaseWriteExecutor.execute(() -> {
            User user = userDao.login(email, password);
            runOnUiThread(() -> {
                if (user != null) {
                    // Login Success
                    // Login Success
                    SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
                    boolean rememberMe = binding.checkboxRememberMe.isChecked();

                    prefs.edit().putBoolean("is_logged_in", true).apply();
                    prefs.edit().putBoolean("remember_me", rememberMe).apply();

                    prefs.edit().putString("user_name", user.name).apply();
                    prefs.edit().putString("user_email", user.email).apply();

                    Toast.makeText(this, "Bem-vindo " + user.name, Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                } else {
                    Toast.makeText(this, "Credenciais inválidas", Toast.LENGTH_SHORT).show();
                }
            });
        });
    }
}
