import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:go_router/go_router.dart';
import 'package:grocery_app/src/common_widget/network_image.dart';
import 'package:grocery_app/src/core/routes/routes.dart';
import 'package:grocery_app/src/logic/provider/home_provider.dart';
import 'package:grocery_app/src/ui/bestdeal/bestdeal_screen.dart';
import 'package:grocery_app/src/ui/data_notfound.dart';
import 'package:grocery_app/src/ui/fruitvegidetail/fruit_veggie_detail.dart';
import 'package:grocery_app/src/ui/header.dart';
import 'package:grocery_app/utils/constants/color_constant.dart';
import 'package:grocery_app/utils/constants/shared_pref_utils.dart';
import 'package:grocery_app/utils/constants/string_constant.dart';
import 'package:grocery_app/utils/extensions/extensions.dart';
import 'package:grocery_app/utils/extensions/uicontext.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:provider/provider.dart';
import 'package:skeletonizer/skeletonizer.dart';
import 'package:url_launcher/url_launcher.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      final productProvider =
          Provider.of<ProductProvider>(context, listen: false);
      productProvider.determinePosition();

      productProvider.getBanners(context);
      productProvider.getHomeProduct(context, "", '', '', '', '');
      productProvider.getBestDealProduct(context, '');
      productProvider.getAllcategory(context);
      //  productProvider.getCategoryByLevel();

      getUserDetails();
    });
  }

  getUserDetails() async {
    APPSTRING.userName = (await SharedPrefUtils.getFirstName())!;

    APPSTRING.emailName = (await SharedPrefUtils.getUserEmail())!;
    APPSTRING.userProfile = (await SharedPrefUtils.getUserProfile())!;
    APPSTRING.userLastName = (await SharedPrefUtils.getLastName())!;
  }

  final TextEditingController _searchController = TextEditingController();
  OverlayEntry? _overlayEntry;
  final LayerLink _layerLink = LayerLink();

  @override
  void dispose() {
    _searchController.dispose();
    super.dispose();
  }

  void _showOverlay(BuildContext context) {
    _clearOverlay();
    _overlayEntry = _createOverlayEntry(context);
    Overlay.of(context).insert(_overlayEntry!);
  }

  OverlayEntry _createOverlayEntry(BuildContext context) {
    final searchProvider = Provider.of<ProductProvider>(context, listen: false);
    RenderBox renderBox = context.findRenderObject() as RenderBox;
    Size size = renderBox.size;

    return OverlayEntry(
      builder: (context) => Positioned(
        width: size.width,
        child: CompositedTransformFollower(
          link: _layerLink,
          offset: Offset(0, size.height + 5),
          child: Material(
            elevation: 4.0,
            borderRadius: BorderRadius.circular(8),
            child: ListView(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              children: searchProvider.suggestions.map((suggestion) {
                return ListTile(
                  title: Row(
                    children: [
                      AppNetworkImage(
                        height: 25,
                        width: 25,
                        imageUrl: suggestion.productImages!.first.url,
                        backGroundColor: APPCOLOR.bgGrey,
                        radius: 10,
                      ),
                      Text(suggestion.name),
                    ],
                  ),
                  onTap: () {
                    _searchController.text = suggestion.name;
                    searchProvider.getHomeProduct(
                        context, "", suggestion.name, '', '', '');
                    _clearOverlay();
                  },
                );
              }).toList(),
            ),
          ),
        ),
      ),
    );
  }

  void _clearOverlay() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Padding(
          padding: context.bodyAllPadding.copyWith(
            top: 0,
          ),
          child: SingleChildScrollView(
            child: Column(
              children: [
                Header(),
                const SizedBox(
                  height: 15,
                ),

                Row(
                  children: [
                    Expanded(
                      // ✅ Move Expanded Here
                      child: Consumer<ProductProvider>(
                          builder: (context, provider, child) {
                        return CompositedTransformTarget(
                          link: _layerLink,
                          child: Container(
                            height: 50,
                            decoration: BoxDecoration(
                              color: APPCOLOR.bgGrey,
                              borderRadius: BorderRadius.circular(5),
                            ),
                            child: TextFormField(
                              controller: _searchController,
                              onChanged: (value) {
                                provider.searchProducts(value, context);

                                if (value.isNotEmpty) {
                                  _showOverlay(context);
                                } else {
                                  _clearOverlay();
                                }
                              },
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                fillColor: Colors.transparent,
                                suffixIcon: InkWell(
                                  onTap: () {
                                    print(
                                        "klfklhjklfklhg   ${_searchController.text}");

                                    provider.getHomeProduct(context, "",
                                        _searchController.text, '', '', '');
                                  },
                                  child: Icon(MdiIcons.magnify),
                                ),
                                hintText: 'Search',
                                hintStyle: context.customRegular(
                                    APPCOLOR.grey666666, 18),
                                isCollapsed: true,
                                contentPadding: const EdgeInsets.symmetric(
                                  vertical: 10,
                                  horizontal: 10,
                                ),
                              ),
                            ),
                          ),
                        );
                      }),
                    ),
                    const SizedBox(width: 10),
                    InkWell(
                      onTap: () {
                        showSortBottomSheet(context);
                      },
                      child: Container(
                        height: 50,
                        width: 50,
                        decoration: BoxDecoration(
                          color: APPCOLOR.lightGreen,
                          borderRadius: BorderRadius.circular(5),
                        ),
                        child: Center(
                          child: Icon(
                            MdiIcons.tuneVariant,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),

                // Row(
                //   children: [
                //     Consumer<ProductProvider>(
                //         builder: (context, provider, child)
                //         {
                //       return
                //       CompositedTransformTarget(
                //            link: _layerLink,
                //         child: Expanded(
                //           child: Container(
                //             height: 50,
                //             decoration: BoxDecoration(
                //               color: APPCOLOR.bgGrey,
                //               borderRadius: BorderRadius.circular(5),
                //             ),
                //             child: TextFormField(
                //                controller: _searchController,
                //               onChanged: (value) {

                //                  provider.searchProducts(value);
                //                 if (value.isNotEmpty) {
                //                   _showOverlay(context);
                //                 } else {
                //                   _clearOverlay();
                //                 }

                //                // provider.searchValue = value;

                //               },
                //               decoration: InputDecoration(
                //                 border: InputBorder.none,
                //                 fillColor: Colors.transparent,
                //                 suffixIcon: InkWell(
                //                     onTap: () {
                //                       provider.getHomeProduct(context, "",
                //                           provider.searchValue, '', '', '');
                //                     },
                //                     child: Icon(MdiIcons.magnify)),
                //                 hintText: 'Search',
                //                 hintStyle: context.customRegular(
                //                     APPCOLOR.grey666666, 18),
                //                 isCollapsed: true,
                //                 contentPadding: const EdgeInsets.symmetric(
                //                     vertical: 10, horizontal: 10),
                //               ),
                //             ),
                //           ),
                //         ),
                //       );
                //     }),

                //     const SizedBox(
                //       width: 10,
                //     ),
                //     InkWell(
                //       onTap: () {
                //         showSortBottomSheet(context);
                //       },
                //       child: Container(
                //         height: 50,
                //         width: 50,
                //         decoration: BoxDecoration(
                //           color: APPCOLOR.lightGreen,
                //           borderRadius: BorderRadius.circular(5),
                //         ),
                //         child: Center(
                //           child: Icon(
                //             MdiIcons.tuneVariant,
                //             color: Colors.white,
                //           ),
                //         ),
                //       ),
                //     ),
                //   ],
                // ),

                const SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Text(
                        "Shop By Category",
                        style: context.customExtraBold(Colors.black, 18),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        // Navigator.of(context).push(MaterialPageRoute(
                        //   builder: (context) {
                        //     return const FruitVeggieDetail();
                        //   },
                        // ));

                        context.push(MyRoutes.FRUITVEGGIEDETAIL);
                      },
                      child: Text(
                        "See All",
                        style: context.customMedium(APPCOLOR.lightGreen, 16),
                      ),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 15,
                ),
                categoriesProduct(),
                const SizedBox(
                  height: 15,
                ),
                bannerview(),
                const SizedBox(
                  height: 15,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Text(
                        "Best Deal",
                        style: context.customExtraBold(Colors.black, 18),
                      ),
                    ),
                    InkWell(
                      onTap: () {
                        Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) {
                            return const BestDealScreen();
                          },
                        ));
                      },
                      child: Text(
                        "See All",
                        style: context.customMedium(APPCOLOR.lightGreen, 16),
                      ),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 15,
                ),
                bestDeal(),
                const SizedBox(
                  height: 20,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget bestDeal() {
    return Consumer<ProductProvider>(builder: (context, provider, child) {
      return provider.isBestdealingloading
          ? SizedBox(
              height: MediaQuery.of(context).size.height * 0.28,
              child: ListView.builder(
                itemCount: 5,
                scrollDirection: Axis.horizontal,
                itemBuilder: (context, index) {
                  double cardWidth = MediaQuery.of(context).size.width * 0.4;
                  return Padding(
                    padding:
                        const EdgeInsets.only(right: 10, bottom: 5, top: 5),
                    child: Skeletonizer(
                      enabled: provider.isBestdealingloading,
                      child: Container(
                        width: cardWidth,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(15),
                        ),
                        child: Padding(
                          padding: const EdgeInsets.all(5),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Center(
                                child: Container(
                                  height:
                                      MediaQuery.of(context).size.height * 0.12,
                                  width: cardWidth * 0.9,
                                  decoration: BoxDecoration(
                                    color: Colors.grey[300],
                                    borderRadius: BorderRadius.circular(15),
                                  ),
                                ),
                              ),
                              SizedBox(height: 10),
                              Container(
                                height: 15,
                                width: cardWidth * 0.8,
                                color: Colors.grey[300],
                              ),
                              SizedBox(height: 5),
                              Container(
                                height: 12,
                                width: cardWidth * 0.5,
                                color: Colors.grey[300],
                              ),
                              SizedBox(height: 5),
                              const Spacer(),
                              Row(
                                children: [
                                  Container(
                                    height: 12,
                                    width: 40,
                                    color: Colors.grey[300],
                                  ),
                                  const Spacer(),
                                  Container(
                                    height: 25,
                                    width: 40,
                                    decoration: BoxDecoration(
                                      color: Colors.grey[300],
                                      borderRadius: BorderRadius.circular(5),
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),
                  );
                },
              ),
            )
          : provider.bestdeal.isEmpty
              ? Center(
                  child: DataNotFound(
                  imagePath: 'assets/images/cart.jpg',
                  message: "",
                  width: 300.w,
                  height: 300.h,
                ))
              : SizedBox(
                  height: MediaQuery.of(context).size.height * 0.28,
                  child: ListView.builder(
                    itemCount: provider.bestdeal.length,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (context, index) {
                      var bestdealproduct = provider.bestdeal[index];
                      double cardWidth =
                          MediaQuery.of(context).size.width * 0.4;

                      return InkWell(
                        onTap: () {
                          context.push(
                            MyRoutes.PRODUCTDETAILS,
                            //  extra: bestdealproduct.id

                            extra: {
                              "id": bestdealproduct.id,
                              "quantity": 1,
                              "price": bestdealproduct.discountPrice,
                            },
                          );
                        },
                        child: Padding(
                          padding: const EdgeInsets.only(
                              right: 10, bottom: 5, top: 5),
                          child: Container(
                            width: cardWidth,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(15),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.grey.withOpacity(0.1),
                                  blurRadius: 1,
                                  offset: const Offset(5, 5),
                                ),
                              ],
                            ),
                            child: Padding(
                              padding: const EdgeInsets.all(5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Center(
                                    child: Container(
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.12,
                                      width: cardWidth * 0.9,
                                      decoration: BoxDecoration(
                                        color: APPCOLOR.bgGrey,
                                        borderRadius: BorderRadius.circular(15),
                                      ),
                                      child: Stack(
                                        alignment: Alignment.center,
                                        children: [
                                          AppNetworkImage(
                                            height: MediaQuery.of(context)
                                                    .size
                                                    .height *
                                                0.08,
                                            width: cardWidth * 0.7,
                                            imageUrl: bestdealproduct
                                                    .productImages
                                                    ?.first
                                                    ?.url ??
                                                "",
                                            backGroundColor: Colors.transparent,
                                          ),
                                          Positioned(
                                            right: 1,
                                            bottom: 0,
                                            child: Container(
                                              padding: EdgeInsets.symmetric(
                                                  horizontal: 6, vertical: 2),
                                              decoration: BoxDecoration(
                                                color: Colors.red,
                                                borderRadius:
                                                    BorderRadius.circular(5),
                                              ),
                                              child: Center(
                                                child: Text(
                                                    "${calculateDiscountPercentage(double.parse(bestdealproduct.basePrice), double.parse(bestdealproduct.discountPrice))}% OFF",
                                                    style: TextStyle(
                                                        color: Colors.white,
                                                        fontSize: 12)),
                                              ),
                                            ),
                                          )
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.01),
                                  Text(
                                    bestdealproduct.name ?? "",
                                    textAlign: TextAlign.left,
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                    style: context.customMedium(
                                        APPCOLOR.balck1A1A1A, 14),
                                  ),
                                  SizedBox(
                                      height:
                                          MediaQuery.of(context).size.height *
                                              0.005),
                                  Text(
                                    bestdealproduct.unit ?? "",
                                    textAlign: TextAlign.left,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: context.customMedium(
                                        Colors.grey.withOpacity(0.8), 12),
                                  ),
                                  // SizedBox(
                                  //     height:
                                  //         MediaQuery.of(context).size.height *
                                  //             0.005),
                                  const Spacer(),
                                  Row(
                                    children: [
                                      Row(
                                        children: [
                                          Text(
                                            "₹${bestdealproduct.discountPrice ?? ""}  ",
                                            textAlign: TextAlign.left,
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            style: context.customSemiBold(
                                                Colors.black, 11),
                                          ),
                                          Text(
                                            "₹${bestdealproduct.basePrice ?? ""}",
                                            textAlign: TextAlign.left,
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                            style: context
                                                .customMedium(
                                                    Colors.grey
                                                        .withOpacity(0.8),
                                                    12)
                                                .copyWith(
                                                    decoration: TextDecoration
                                                        .lineThrough),
                                          ),
                                        ],
                                      ),
                                      const Spacer(),
                                      Align(
                                        alignment: Alignment.centerRight,
                                        child: GestureDetector(
                                          onTap: () async {
                                            if (await SharedPrefUtils
                                                    .getToken() !=
                                                null) {
                                              await provider.addToCart(context,
                                                  bestdealproduct.id!, 1);
                                            } else {
                                              context.push(MyRoutes.SIGNUP);
                                            }
                                          },
                                          child: Container(
                                            height: MediaQuery.of(context)
                                                    .size
                                                    .height *
                                                0.035,
                                            width: MediaQuery.of(context)
                                                    .size
                                                    .width *
                                                0.1,
                                            decoration: BoxDecoration(
                                              color: APPCOLOR.lightGreen,
                                              borderRadius:
                                                  BorderRadius.circular(5),
                                            ),
                                            child: Center(
                                              child: provider.isLoading[
                                                          bestdealproduct.id] ??
                                                      false
                                                  ? Padding(
                                                      padding:
                                                          const EdgeInsets.all(
                                                              8.0),
                                                      child: SizedBox(
                                                        height: 10,
                                                        width: 10,
                                                        child:
                                                            CircularProgressIndicator(
                                                                color: Colors
                                                                    .white,
                                                                strokeWidth: 2),
                                                      ),
                                                    )
                                                  : Text(
                                                      'Add',
                                                      style:
                                                          context.customRegular(
                                                              Colors.white, 12),
                                                    ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                );
    });
  }

  int calculateDiscountPercentage(double basePrice, double discountPrice) {
    print(
        "Base Price (Before Discount): $basePrice, Discount Price (After Discount): $discountPrice");

    if (basePrice <= 0 || discountPrice <= 0 || discountPrice > basePrice) {
      print("Error: Invalid price values.");
      return 0;
    }

    double discountAmount = basePrice - discountPrice;
    double discountPercentage = (discountAmount / basePrice) * 100;

    return discountPercentage.round();
  }

  Widget bannerview() {
    return Consumer<ProductProvider>(builder: (context, provider, child) {
      return provider.isBannerLoading
          ? Skeletonizer(
              enabled: provider.isBannerLoading,
              child: Container(
                height: 180,
                width: double.infinity,
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(15),
                ),
              ),
            )
          : provider.banner.isEmpty
              ? SizedBox.shrink()
              : CarouselSlider(
                  options: CarouselOptions(
                    height: 180,
                    autoPlay: true,
                    enlargeCenterPage: true,
                    viewportFraction: 1,
                    aspectRatio: 16 / 9,
                    initialPage: 0,
                    enableInfiniteScroll: true,
                    reverse: false,
                    autoPlayInterval: Duration(seconds: 3),
                    autoPlayAnimationDuration: Duration(milliseconds: 800),
                    autoPlayCurve: Curves.fastOutSlowIn,
                    enlargeFactor: 0.3,
                  ),
                  items: provider.banner.map((banner) {
                    return Builder(
                      builder: (BuildContext context) {
                        return Container(
                          width: MediaQuery.of(context).size.width,
                          decoration: BoxDecoration(
                            color: Colors.greenAccent.withOpacity(0.1),
                            borderRadius: BorderRadius.circular(15),
                          ),
                          child: Stack(
                            children: [
                              Positioned(
                                top: 15,
                                left: 15,
                                child: SizedBox(
                                  width: 200,
                                  child: Text(
                                    banner.altText ?? "Special Event",
                                    style: context.customExtraBold(
                                        Colors.black, 18),
                                  ),
                                ),
                              ),
                              Positioned(
                                bottom: 15,
                                left: 15,
                                child: GestureDetector(
                                  onTap: () {
                                    _launchUrl(banner.redirectUrl);
                                  },
                                  child: Container(
                                    height: 40,
                                    width: 100,
                                    decoration: BoxDecoration(
                                      color: APPCOLOR.lightGreen,
                                      borderRadius: BorderRadius.circular(5),
                                    ),
                                    child: Center(
                                      child: Text(
                                        'Shop now',
                                        style: context.customRegular(
                                            Colors.white, 14),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              Positioned(
                                right: 15,
                                bottom: 15,
                                child: AppNetworkImage(
                                  height: 130,
                                  width: 150,
                                  imageUrl: banner.imageUrl ??
                                      'https://e7.pngegg.com/pngimages/742/816/png-clipart-coca-cola-can-illustration-coca-cola-soft-drink-surge-pepsi-coke-sweetness-cola-thumbnail.png',
                                  backGroundColor: Colors.transparent,
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    );
                  }).toList(),
                );
    });
  }

  Future<void> _launchUrl(url) async {
    print("jdhfjkgh  ${url}");
    final Uri uri = Uri.parse(url);
    if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) {
      throw 'Could not launch $url';
    }
  }

  Widget categoriesProduct() {
    return Consumer<ProductProvider>(builder: (context, provider, child) {
      return provider.isHomeLoadingg
          ? GridView.builder(
              shrinkWrap: true,
              itemCount: 8,
              physics: const NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 4,
                crossAxisSpacing: 5,
                mainAxisSpacing: 5,
                childAspectRatio: MediaQuery.of(context).size.width /
                    (MediaQuery.of(context).size.height / 1.2),
              ),
              itemBuilder: (context, index) {
                return Skeletonizer(
                  enabled: provider.isHomeLoadingg,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Container(
                        height: 80,
                        width: 80,
                        decoration: BoxDecoration(
                          color: Colors.grey[300],
                          borderRadius: BorderRadius.circular(5),
                        ),
                      ),
                      const SizedBox(height: 10),
                      Container(
                        height: 15,
                        width: 60,
                        color: Colors.grey[300],
                      ),
                    ],
                  ),
                );
              },
            )
          : provider.homeproducts.isEmpty
              ? Center(child: Text(''))
              : GridView.builder(
                  shrinkWrap: true,
                  itemCount: provider.homeproducts.length,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                    crossAxisSpacing: 5,
                    mainAxisSpacing: 5,
                    childAspectRatio: MediaQuery.of(context).size.width /
                        (MediaQuery.of(context).size.height / 1.2),
                  ),
                  itemBuilder: (context, index) {
                    var product = provider.homeproducts[index];
                    return InkWell(
                      onTap: () {
                        context.push(
                          MyRoutes.PRODUCTDETAILS,
                          extra: {
                            "id": product.id,
                            "quantity": 1,
                            "price": product.discountPrice,
                          },
                        );
                      },
                      child: SizedBox(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Container(
                              decoration: BoxDecoration(
                                color: APPCOLOR.bgGrey,
                                borderRadius: BorderRadius.circular(5),
                              ),
                              child: AppNetworkImage(
                                height: 80,
                                width: 80,
                                imageUrl: product.productImages!.first.url,
                                backGroundColor: APPCOLOR.bgGrey,
                                radius: 10,
                              ),
                            ),
                            const SizedBox(height: 10),
                            Text(
                              product.name ?? "",
                              textAlign: TextAlign.center,
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: context.customMedium(
                                  APPCOLOR.balck1A1A1A, 14),
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                );
    });
  }

  void showSortBottomSheet(BuildContext context) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(50)),
      ),
      builder: (context) {
        return Consumer<ProductProvider>(
          builder: (context, sortProvider, child) {
            return Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text(
                    "Sort By",
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),
                  Divider(
                    thickness: 0.2,
                  ),
                  _buildSortOption(context, sortProvider, 0, "All"),
                  _buildSortOption(context, sortProvider, 1, "Relevance"),
                  _buildSortOption(context, sortProvider, 2, "Popularity"),
                  _buildSortOption(
                      context, sortProvider, 3, "Price: Low to High"),
                ],
              ),
            );
          },
        );
      },
    );
  }

  Widget _buildSortOption(
      BuildContext context, ProductProvider provider, int index, String title) {
    bool isSelected = provider.selectedIndex == index;

    return ListTile(
      title: Text(title),
      trailing: isSelected
          ? Icon(Icons.check_circle, color: Colors.green)
          : Icon(Icons.radio_button_unchecked, color: Colors.grey),
      onTap: () {
        switch (index) {
          case 0:
            provider.getHomeProduct(context, '', '', "", "", '');
            break;
          case 1:
            provider.getHomeProduct(context, '', '', "", "", "featured");
            break;
          case 2:
            provider.getHomeProduct(context, '', '', "", "", "popularity");
            break;
          case 3:
            //     t(BuildContext context, String id, String search,
            // String minPrice, String maxprice, orderby)
            provider.getHomeProduct(context, '', '', "100", "100000000", '');
            break;
          default:
            provider.getHomeProduct(context, '', '', "", "", '');
        }

        //  provider.getHomeProduct(context, '', '', "100", "100000000");
        provider.setSelectedIndex(context, index);
        Navigator.pop(context); // Close the bottom sheet
      },
    );
  }
}
