import 'package:flutter/material.dart';

import '../../../../../common/theme/theme.dart';
import '../../../../../domain/category/category.dart';

class ProductCategoryHeaderDelegate extends SliverPersistentHeaderDelegate {
  final List<Category> categories;
  final Category selectedCategory;
  final ValueChanged<Category> onCategoryChanged;

  ProductCategoryHeaderDelegate({
    required this.categories,
    required this.selectedCategory,
    required this.onCategoryChanged,
  });

  @override
  Widget build(
    BuildContext context,
    double shrinkOffset,
    bool overlapsContent,
  ) {
    return Container(
      color: AppColor.background,
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Container(
        height: 50,
        margin: const EdgeInsets.symmetric(horizontal: 16.0),
        child: ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: categories.length,
          itemBuilder: (context, index) {
            final category = categories[index];
            final isSelected = selectedCategory == category;

            return Container(
              margin: const EdgeInsets.only(right: 12.0),
              child: FilterChip(
                label: Text(category.name),
                selected: isSelected,
                onSelected: (selected) => onCategoryChanged(category),
                backgroundColor: AppColor.surface,
                selectedColor: AppColor.primary,
                checkmarkColor: AppColor.textWhite,
                labelStyle: AppStyle.md.copyWith(
                  color: isSelected ? AppColor.textWhite : AppColor.textPrimary,
                  fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                ),
                side: BorderSide(
                  color: isSelected ? AppColor.primary : AppColor.border,
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  @override
  double get maxExtent => 66.0;

  @override
  double get minExtent => 66.0;

  @override
  bool shouldRebuild(ProductCategoryHeaderDelegate oldDelegate) {
    return oldDelegate.categories != categories ||
        oldDelegate.selectedCategory != selectedCategory;
  }
}
