package com.m2sdl.undercooked;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.m2sdl.undercooked.domain.Booking;
import com.m2sdl.undercooked.services.FirebaseBackendService;

public class ViewBookingsActivity extends AppCompatActivity {
    private final FirebaseBackendService db = new FirebaseBackendService();

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

    @Override
    protected void onResume() {
        super.onResume();
        double latitude = getIntent().getDoubleExtra("lat", 0);
        double longitude = getIntent().getDoubleExtra("lon", 0);

        LinearLayout bookingContainer = findViewById(R.id.booking_container);
        loadBookings(latitude, longitude, bookingContainer);
    }

    private void loadBookings(double lat, double lon, LinearLayout container) {
        db.getBookings(lat, lon, container, this::addBookingToUI);
    }

    @SuppressLint("SetTextI18n")
    private void addBookingToUI(Booking booking, LinearLayout container) {
        View bookingView = getLayoutInflater().inflate(R.layout.booking_item, container, false);

        TextView dateText = bookingView.findViewById(R.id.bookingDate);
        TextView peopleText = bookingView.findViewById(R.id.bookingPeople);

        java.text.SimpleDateFormat sdf =
                new java.text.SimpleDateFormat("dd/MM/yyyy", java.util.Locale.getDefault());

        String formattedDate = sdf.format(new java.util.Date(booking.getDate()));

        dateText.setText("Date : " + formattedDate);
        peopleText.setText("Personnes : " + booking.getNumberOfPeople());

        container.addView(bookingView);
    }
}
