package com.example.cuida.ui.appointments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.cuida.databinding.FragmentAppointmentsBinding;

public class AppointmentsFragment extends Fragment {

    private FragmentAppointmentsBinding binding;
    private AppointmentsViewModel appointmentsViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        appointmentsViewModel = new ViewModelProvider(this).get(AppointmentsViewModel.class);

        binding = FragmentAppointmentsBinding.inflate(inflater, container, false);

        // Future Appointments
        AppointmentAdapter futureAdapter = new AppointmentAdapter();
        binding.recyclerAppointmentsFuture.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerAppointmentsFuture.setAdapter(futureAdapter);

        appointmentsViewModel.getFutureAppointments().observe(getViewLifecycleOwner(), appointments -> {
            futureAdapter.setAppointments(appointments);
        });

        // Past Appointments
        AppointmentAdapter pastAdapter = new AppointmentAdapter();
        binding.recyclerAppointmentsPast.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerAppointmentsPast.setAdapter(pastAdapter);

        appointmentsViewModel.getPastAppointments().observe(getViewLifecycleOwner(), appointments -> {
            pastAdapter.setAppointments(appointments);
        });

        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}
