import '../../../../../../food_delivery_client.dart';

class WRatingBottomSheet extends StatefulWidget {
  const WRatingBottomSheet({super.key, required this.savedRatings});

  final List<String> savedRatings;

  Future<List<String>?> show(BuildContext context) {
    return showModalBottomSheet<List<String>>(
      context: context,
      builder: (context) => Wrap(children: [this]),
    );
  }

  @override
  State<WRatingBottomSheet> createState() => _WRatingBottomSheetState();
}

class _WRatingBottomSheetState extends State<WRatingBottomSheet> {
  List<String> rating = [];

  void updateRating(String value) {
    final list = List<String>.from(rating);
    if (list.contains(value)) {
      list.remove(value);
      setState(() {
        rating = list;
      });
    } else {
      list.add(value);
      setState(() {
        rating = list;
      });
    }
  }

  @override
  void initState() {
    rating = widget.savedRatings;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final List<String> value = [
      "${context.loc.atLeast} 4,9",
      "${context.loc.atLeast} 4,7",
      "${context.loc.atLeast} 4,5",
    ];

    return WCustomModalBottomSheet(
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(context.loc.rating, style: AppTextStyles.size20Medium),
              IconButton(
                onPressed: () {
                  context.pop(null);
                },
                icon: SvgPicture.asset(AppIcons.icClose, height: 15),
              ),
            ],
          ),
          15.verticalSpace,
          Wrap(
            spacing: 10,
            runSpacing: 15,
            children: List.generate(value.length, (index) {
              return WRatingButton(
                onTap: () {
                  updateRating(value[index]);
                },
                isActive: rating.contains(value[index]),
                rating: value[index],
              );
            }),
          ),
          25.verticalSpace,
          AppButton(
            name: context.loc.apply,
            onPressed: () {
              context.pop(rating);
            },
          ),
          20.verticalSpace,
        ],
      ).paddingSymmetric(horizontal: 15),
    );
  }
}

class WRatingButton extends StatelessWidget {
  const WRatingButton({
    super.key,
    required this.onTap,
    required this.isActive,
    required this.rating,
  });

  final VoidCallback onTap;
  final bool isActive;
  final String rating;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      borderRadius: AppUtils.kBorderRadius25,
      onTap: onTap,
      child: Ink(
        decoration: BoxDecoration(
          color: isActive
              ? AppColors.c34A853
              : AppColors.cE6E6E6.newWithOpacity(.6),
          borderRadius: AppUtils.kBorderRadius25,
        ),
        child: Row(
          spacing: 4,
          mainAxisSize: MainAxisSize.min,
          children: [
            Icon(Icons.star, color: AppColors.cYellow),
            Text(
              rating,
              style: AppTextStyles.size14Medium.copyWith(
                color: isActive ? AppColors.cFFFFFF : AppColors.c000000,
              ),
            ),
          ],
        ).paddingSymmetric(vertical: 6, horizontal: 10),
      ),
    );
  }
}
