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

class WDeliveryHeader extends StatelessWidget {
  const WDeliveryHeader({super.key});

  @override
  Widget build(BuildContext context) {
    List<String> _titles = [
      context.loc.convenience,
      context.loc.alcohol,
      context.loc.petSupplies,
      context.loc.more,
    ];
    /*
    Siz uchun
    Eng ommabop,
    Reyting
    Yetkazib berish vaqti,
    Aksiyalar


     */
    List titles = [
      '',
      context.loc.pickedForYou,
      context.loc.mostPopular,
      context.loc.rating,
      context.loc.deliveryTime,
      context.loc.deals,
    ];
    return BlocBuilder<HomeBloc, HomeState>(
      builder: (context, state) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Column(
              children: [
                15.verticalSpace,
                Row(
                  spacing: 12,
                  children: [
                    WCategoriesHeaderItem(
                      onTap: () {
                        context.push(
                          Routes.restaurantsByCategory,
                          extra: context.loc.american,
                        );
                      },
                      text: context.loc.american,
                      imageUrl: AppImages.imgAmerican,
                    ),
                    WCategoriesHeaderItem(
                      onTap: () {
                        context.push(
                          Routes.restaurantsByCategory,
                          extra: context.loc.grocery,
                        );
                      },
                      text: context.loc.grocery,
                      imageUrl: AppImages.imgGrocery,
                    ),
                  ],
                ),
                8.verticalSpace,
                GridView.builder(
                  shrinkWrap: true,
                  itemCount: _titles.length,
                  padding: EdgeInsets.zero,
                  scrollDirection: Axis.vertical,
                  physics: const NeverScrollableScrollPhysics(),
                  keyboardDismissBehavior:
                      ScrollViewKeyboardDismissBehavior.onDrag,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                    crossAxisSpacing: 5,
                    mainAxisSpacing: 10,
                    mainAxisExtent: 118,
                    // childAspectRatio: 78 / 100,
                  ),
                  itemBuilder: (context, index) => WCategoryItem(
                    onTap: () {
                      if (index == 3) {
                        CategoriesPage().show(context);
                      } else {
                        context.push(
                          Routes.restaurantsByCategory,
                          extra: _titles[index],
                        );
                      }
                    },
                    imgUrl: index != 3 ? _images[index] : null,
                    text: _titles[index],
                    child: index == 3
                        ? SizedBox(
                            height: 55,
                            width: 55,
                            child: Icon(Icons.more_horiz),
                          )
                        : null,
                  ),
                ),
              ],
            ).paddingSymmetric(horizontal: 15),
            15.verticalSpace,

            SizedBox(
              height: 40,
              child: ListView.separated(
                padding: EdgeInsets.symmetric(horizontal: 15),
                scrollDirection: Axis.horizontal,
                itemCount: titles.length,
                physics: const AlwaysScrollableScrollPhysics(),
                separatorBuilder: (context, index) => 10.horizontalSpace,
                itemBuilder: (context, index) => InkWell(
                  onTap: () async {
                    if (index != 3 && index != 0) {
                      context.read<HomeBloc>().add(
                        HomeEvent.filtered(titles[index]),
                      );
                    }

                    if (index == 3) {
                      final response = await WRatingBottomSheet(
                        savedRatings: state.rating ?? [],
                      ).show(context);

                      if (response != null) {
                        context.read<HomeBloc>().add(
                          HomeEvent.rated(response ?? []),
                        );
                      }
                    }
                  },
                  borderRadius: AppUtils.kBorderRadius25,
                  child: Ink(
                    padding: EdgeInsets.zero,
                    decoration: BoxDecoration(
                      color: AppColors.cEEEEEE,
                      borderRadius: AppUtils.kBorderRadius25,
                    ),
                    child: Row(
                      children: [
                        if (index != 0 && state.filters.contains(titles[index]))
                          SvgPicture.asset(
                            AppIcons.icCheck1,
                          ).paddingOnly(right: 5),
                        if (index == 0)
                          Badge(
                            isLabelVisible:
                                state.filters.isNotEmpty ||
                                state.rating.isNotEmpty,
                            backgroundColor: AppColors.c34A853,
                            child: SvgPicture.asset(AppIcons.icFilter),
                          ).paddingSymmetric(vertical: 6),

                        if (index == 3 && state.rating.isNotEmpty)
                          SizedBox(
                            height: 25,
                            width: 25,
                            child: DecoratedBox(
                              decoration: BoxDecoration(
                                color: AppColors.c34A853,
                                borderRadius: AppUtils.kBorderRadius16,
                              ),
                              child: Center(
                                child: Text(
                                  "${state.rating.length}",
                                  style: AppTextStyles.size14Regular.copyWith(
                                    color: AppColors.cFFFFFF,
                                  ),
                                ),
                              ),
                            ),
                          ).paddingOnly(right: 8),
                        Text(titles[index], style: AppTextStyles.size14Medium),
                        if (index == 3) Icon(Icons.keyboard_arrow_down),
                      ],
                    ).paddingSymmetric(vertical: 0, horizontal: 15),
                  ),
                ),
              ),
            ),

            10.verticalSpace,
            WDivider(),
          ],
        );
      },
    );
  }
}

List _images = [
  AppImages.imgConvenience,
  AppImages.imgAlcohol,
  AppImages.imgPetSupplies,
];
