import 'package:cargocalculaterapp/constants/constants.dart';
import 'package:cargocalculaterapp/core/extension/build_context_extension.dart';
import 'package:cargocalculaterapp/core/functions/base_finctions.dart';
import 'package:cargocalculaterapp/core/utils/app_utils.dart';
import 'package:cargocalculaterapp/features/home/presentation/bloc/home_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class StatusListWidget extends StatelessWidget {
  const StatusListWidget({super.key, required this.selectedStatus});

  final String selectedStatus;

  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      pinned: true,
      delegate: _StatusHeaderDelegate(selectedStatus: selectedStatus),
    );
  }
}

class _StatusHeaderDelegate extends SliverPersistentHeaderDelegate {
  final String selectedStatus;

  _StatusHeaderDelegate({required this.selectedStatus});

  @override
  double get minExtent => 72;

  @override
  double get maxExtent => 72;

  @override
  Widget build(
    BuildContext context,
    double shrinkOffset,
    bool overlapsContent,
  ) {
    return Material(
      color: context.color.grayBackground,
      child: SizedBox(
        height: 72,
        child: ListView.separated(
          scrollDirection: Axis.horizontal,
          padding: const EdgeInsets.all(16),
          itemBuilder: (context, index) {
            final key = AppConst.statusList.keys.toList()[index];
            return InkWell(
              borderRadius: AppUtils.kBorderRadius16,
              onTap: () {
                context.read<HomeBloc>().add(OrderStatusEvent(status: key));
              },
              child: Ink(
                padding: AppUtils.kPaddingHor16,
                decoration: BoxDecoration(
                  borderRadius: AppUtils.kBorderRadius16,
                  color:
                      key == selectedStatus
                          ? context.color.primaryColor
                          : context.color.statusBackground,
                  border: Border.all(
                    color:
                        key == selectedStatus
                            ? context.color.primaryColor
                            : context.color.lightSecondary,
                  ),
                ),
                child: Center(
                  child: Text(
                    Functions.getTranslatedItem(
                      AppConst.statusList.values.toList()[index],
                      context,
                    ),
                    textAlign: TextAlign.center,
                    style:
                        key == selectedStatus
                            ? context.text.statusText
                            : context.text.statusText.copyWith(
                              color: context.color.textColor,
                            ),
                  ),
                ),
              ),
            );
          },
          separatorBuilder: (_, __) => AppUtils.kBoxWidth16,
          itemCount: AppConst.statusList.length,
        ),
      ),
    );
  }

  @override
  bool shouldRebuild(covariant _StatusHeaderDelegate oldDelegate) {
    return true;
  }
}
