import 'package:flutter/cupertino.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/adapters.dart';

import '../models/food.dart';
import 'card_component.dart';

class FoodComponent extends StatelessWidget {
  const FoodComponent({Key? key}) : super(key: key);

  List<Food> castDynamicToListFood(List<dynamic> dynamicList) {
    List<Food> foodList = [];
    for (Food element in dynamicList) {
      foodList.add(element);
    }

    return foodList;
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder(
        valueListenable: Hive.box("TODAY").listenable(),
        builder: (context, box, widget) {
          return ListView.builder(
              primary: false,
              shrinkWrap: true,
              itemCount: box.keys.length,
              itemBuilder: (context, i) {
                if (box.keyAt(i).toString() == "DATE") {
                  return const SizedBox.shrink();
                } else {
                  return CardComponent(
                      eatingMealName: box.keyAt(i).toString(),
                      selectedMeal:
                      castDynamicToListFood(box.getAt(i)));
                }
              });
        });
  }
}
