package com.example.pap_teste;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
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.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.HashMap;
import java.util.Map;

public class ProfileDashboardActivity extends AppCompatActivity {

    private EditText inputName;
    private String email, documentId;
    private DatabaseReference databaseReference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_profile_dashboard);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.profileRoot), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        email = getIntent().getStringExtra(MainActivity.EXTRA_EMAIL);
        String currentName = getIntent().getStringExtra(MainActivity.EXTRA_DISPLAY_NAME);

        if (email != null) {
            documentId = email.replace(".", "_").replace("@", "_at_");
        }

        databaseReference = FirebaseDatabase.getInstance().getReference().child("users");

        inputName = findViewById(R.id.inputProfileName);
        if (currentName != null) {
            inputName.setText(currentName);
        }

        Button btnSave = findViewById(R.id.btnSaveProfile);
        Button btnBack = findViewById(R.id.btnVoltar);
        Button btnFavs = findViewById(R.id.btnFavoritos);
        Button btnRes = findViewById(R.id.btnMinhasReservas);

        btnBack.setOnClickListener(v -> finish());

        btnSave.setOnClickListener(v -> saveProfile());

        btnFavs.setOnClickListener(v -> {
            Toast.makeText(this, "A abrir Favoritos...", Toast.LENGTH_SHORT).show();
            // startActivity(new Intent(this, FavoritosActivity.class));
        });

        btnRes.setOnClickListener(v -> {
            Toast.makeText(this, "A abrir Reservas...", Toast.LENGTH_SHORT).show();
            // startActivity(new Intent(this, DetalhesReservasActivity.class));
        });
    }

    private void saveProfile() {
        if (documentId == null)
            return;

        String newName = inputName.getText().toString().trim();
        if (TextUtils.isEmpty(newName)) {
            Toast.makeText(this, "Indique um nome.", Toast.LENGTH_SHORT).show();
            return;
        }

        Map<String, Object> updates = new HashMap<>();
        updates.put("displayName", newName);

        databaseReference.child(documentId).updateChildren(updates)
                .addOnSuccessListener(aVoid -> {
                    Toast.makeText(this, "Perfil atualizado!", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                })
                .addOnFailureListener(
                        e -> Toast.makeText(this, "Falha ao atualizar perfil.", Toast.LENGTH_SHORT).show());
    }
}
