import 'package:assorted_layout_widgets/assorted_layout_widgets.dart';
import 'package:basic_utils/basic_utils.dart';
import 'package:ernaehrung/android/config/cast_helper.dart';
import 'package:ernaehrung/android/config/statistics.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hive/hive.dart';
import '../config/format_helper.dart';
import '../models/food.dart';

class SearchedFoodComponent extends StatefulWidget {
  final Food food;
  final String cardName;
  const SearchedFoodComponent(this.food, this.cardName, {Key? key}) : super(key: key);

  @override
  State<SearchedFoodComponent> createState() => _SearchFoodComponentState();
}

class _SearchFoodComponentState extends State<SearchedFoodComponent> {

  void storeFood() async {
    showSuccessToast();
    StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
    final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!);
    if (!mealplanBox.isOpen){
      Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
    }
    addValuesToList(mealplanBox, widget.cardName, [widget.food]);
  }

  void showSuccessToast(){
    Fluttertoast.showToast(
      msg: "${getToastFoodNameString(widget.food)} erfolgreich zu ${StringUtils.capitalize(widget.cardName)} hinzugefügt",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
      timeInSecForIosWeb: 5,
      backgroundColor: Colors.green,
      textColor: Colors.black,
    );
  }

  void addValuesToList(box, String key, List<Food> newValues){
    List<Food> existingList = castDynamicToListFood(box.get(key));
    for(int i = 0; i < newValues.length;i++){
      if(!existingList.contains(newValues[i])){
        existingList.add(newValues[i]);
      }
    }
    box.put(key, existingList);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
        decoration:
            const BoxDecoration(color: Color.fromARGB(234, 234, 123, 5)),
        child: WrapSuper(
          spacing: 32,
          wrapType: WrapType.balanced,
          wrapFit: WrapFit.larger,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  width: MediaQuery.of(context).size.width * 0.6,
                  child: Text(
                    widget.food.name,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    softWrap: false,
                    style: const TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0),
                  ),
                ),
                SizedBox(
                  width: MediaQuery.of(context).size.width * 0.6,
                  child: Text(
                      "${widget.food.fatg} 100g "
                      "${widget.food.calories} 100g "
                      "${widget.food.carbohydrateg.toString()} 100g",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    softWrap: false,
                    style: const TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                        fontSize: 20.0),
                  ),
                ),
              ],
            ),
            ElevatedButton(
              onPressed: () async {
                storeFood();
              },
              style: ElevatedButton.styleFrom(
                foregroundColor: Colors.white,
                backgroundColor: Colors.green,
                shadowColor: Colors.greenAccent,
                elevation: 3,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(32.0)),
                maximumSize: Size(
                    MediaQuery.of(context).size.width * 0.2, 36), //////// HERE
              ),
              child: const Text('+'),
            )
          ],
        ));
  }
}
