package com.example.pap_teste;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.pap_teste.models.Restaurant;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private RecyclerView rvFeatured, rvNearYou;
    private RestaurantAdapter featuredAdapter, nearYouAdapter;
    private List<Restaurant> featuredList = new ArrayList<>();
    private List<Restaurant> nearYouList = new ArrayList<>();
    private FirebaseFirestore db;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        rvFeatured = view.findViewById(R.id.rvFeatured);
        rvNearYou = view.findViewById(R.id.rvNearYou);

        db = FirebaseFirestore.getInstance();

        setupRecyclers();
        loadRestaurants();

        return view;
    }

    private void setupRecyclers() {
        featuredAdapter = new RestaurantAdapter(featuredList, true, this::onRestaurantClick);
        rvFeatured.setAdapter(featuredAdapter);
        rvFeatured.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));

        nearYouAdapter = new RestaurantAdapter(nearYouList, false, this::onRestaurantClick);
        rvNearYou.setAdapter(nearYouAdapter);
        rvNearYou.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    private void loadRestaurants() {
        db.collection("restaurants").get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                featuredList.clear();
                nearYouList.clear();
                for (QueryDocumentSnapshot document : task.getResult()) {
                    Restaurant r = document.toObject(Restaurant.class);
                    r.setId(document.getId());
                    featuredList.add(r);
                    nearYouList.add(r);
                }
                featuredAdapter.notifyDataSetChanged();
                nearYouAdapter.notifyDataSetChanged();
            }
        });
    }

    private void onRestaurantClick(Restaurant restaurant) {
        Intent intent = new Intent(getActivity(), RestaurantDetailActivity.class);
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_ID, restaurant.getId());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_NAME, restaurant.getName());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_IMAGE, restaurant.getImageURL());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_CUISINE, restaurant.getCuisine());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_RATING, restaurant.getRating());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_DESC, restaurant.getDescription());
        intent.putExtra(RestaurantDetailActivity.EXTRA_RESTAURANT_ADDRESS, restaurant.getAddress());
        startActivity(intent);
    }
}
