import 'package:basic_utils/basic_utils.dart';
import 'package:ernaehrung/pages/nav_pages/subpages/today_page/sub_components/today_card/sub_components/card_data_food.dart';
import 'package:ernaehrung/pages/nav_pages/subpages/today_page/sub_components/today_card/sub_components/card_title.dart';
import 'package:flutter/material.dart';
import '../../../../../../helper/cast_helper.dart';
import '../../../../../../models/food.dart';
import '../../../../../search_food/search_food.dart';

class TodayCardComponent extends StatelessWidget {
  final String title;
  final Color color;
  final List<Food> foods;

  const TodayCardComponent(this.title, this.foods, {super.key, Color? color})
      : color = color ?? Colors.black;

  Route createRoute(String cardName) {
    return PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) =>
          SearchFoodPage(cardName),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        const begin = Offset(0.0, 1.0);
        const end = Offset.zero;
        const curve = Curves.ease;

        var tween =
        Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

        return SlideTransition(
          position: animation.drive(tween),
          child: child,
        );
      },
    );
  }

  int countCalories(List<Food> foods){
    int summary = 0;
    for(int i=0; i<foods.length;i++){
      summary += foods[i].calories is int ?  foods[i].calories as int : (foods[i].calories as double).round();
    }

    return summary;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        decoration: BoxDecoration(
          color: color,
          borderRadius: const BorderRadius.all(Radius.circular(12)),
        ),
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 16),
        child: Column(
          children: [
            CardTitleComponent(
              StringUtils.capitalize(title),
              "${countCalories(castDynamicToListFood(foods))} Kalorien",
            ),
            CardDataFoodComponent(
              foods,
              color,
            ),
            Padding(
              padding: const EdgeInsets.only(left: 8, right: 8,bottom: 8),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  minimumSize: const Size.fromHeight(40),
                  backgroundColor: const Color(0xFFffffff),
                  foregroundColor: color,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(12),
                  ),
                ),
                onPressed: () async {
                  Navigator.of(context).push(createRoute(title));
                },
                child: const Text(
                    '+ Gericht hinzufügen',
                    style:TextStyle(
                      fontSize: 17
                    ) ,
                ),
              ),
            ),

          ],
        ),
    );

  }
}
