package com.example.cuida.ui.medication;

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.FragmentMedicationBinding;

public class MedicationFragment extends Fragment {

    private FragmentMedicationBinding binding;
    private MedicationViewModel medicationViewModel;

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

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

        MedicationAdapter adapter = new MedicationAdapter(medication -> {
            medicationViewModel.update(medication);
        });
        binding.recyclerMedication.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recyclerMedication.setAdapter(adapter);

        medicationViewModel.getAllMedications().observe(getViewLifecycleOwner(), medications -> {
            adapter.setMedications(medications);
        });

        return binding.getRoot();
    }

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