package com.example.pap_teste;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

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 androidx.recyclerview.widget.RecyclerView;
import com.example.pap_teste.models.FoodCategory;
import java.util.ArrayList;
import java.util.List;

public class ClientDashboardActivity extends AppCompatActivity {

        private String email, displayName, role;
        private TextView txtGreeting;
        private androidx.activity.result.ActivityResultLauncher<Intent> profileLauncher;

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

                txtGreeting = findViewById(R.id.txtClientGreeting);

                email = getIntent().getStringExtra(MainActivity.EXTRA_EMAIL);
                displayName = getIntent().getStringExtra(MainActivity.EXTRA_DISPLAY_NAME);
                role = getIntent().getStringExtra(MainActivity.EXTRA_ROLE);

                profileLauncher = registerForActivityResult(
                                new androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult(),
                                result -> {
                                        if (result.getResultCode() == RESULT_OK) {
                                                android.widget.Toast.makeText(this,
                                                                "Perfil atualizado. Reinicie para ver mudanças.",
                                                                android.widget.Toast.LENGTH_SHORT).show();
                                        }
                                });

                updateGreeting();
                setupCategories();
                setupActions();
        }

        private void updateGreeting() {
                txtGreeting.setText(String.format("Olá, %s", displayName != null ? displayName : "convidado"));
        }

        private void setupCategories() {
                RecyclerView rv = findViewById(R.id.rvCategories);
                List<FoodCategory> cats = new ArrayList<>();
                cats.add(new FoodCategory("Carnes", R.drawable.cat_carnes));
                cats.add(new FoodCategory("Massas", R.drawable.cat_massas));
                cats.add(new FoodCategory("Sushi", R.drawable.cat_sushi));
                cats.add(new FoodCategory("Pizzas", R.drawable.ic_launcher_background));
                cats.add(new FoodCategory("Sobremesas", R.drawable.ic_launcher_background));

                FoodCategoryAdapter adapter = new FoodCategoryAdapter(cats);
                rv.setAdapter(adapter);
        }

        private void setupActions() {
                findViewById(R.id.cardProfile).setOnClickListener(v -> {
                        Intent intent = new Intent(this, ProfileDashboardActivity.class);
                        intent.putExtra(MainActivity.EXTRA_EMAIL, email);
                        intent.putExtra(MainActivity.EXTRA_DISPLAY_NAME, displayName);
                        profileLauncher.launch(intent);
                });

                findViewById(R.id.btnVoltar).setOnClickListener(v -> finish());

                findViewById(R.id.btnCheckIn).setOnClickListener(v -> {
                        Intent intent = new Intent(this, CheckInAntecipadoActivity.class);
                        intent.putExtra("restaurant_email", "sabor_arte@restaurante.com");
                        startActivity(intent);
                });

                findViewById(R.id.btnPartilhar).setOnClickListener(
                                v -> startActivity(new Intent(this, PartilharReservaActivity.class)));

                findViewById(R.id.btnNovaReserva)
                                .setOnClickListener(v -> startActivity(new Intent(this, NovaReservaActivity.class)));

                findViewById(R.id.btnExplorar).setOnClickListener(
                                v -> startActivity(new Intent(this, ExplorarRestaurantesActivity.class)));
        }
}
