import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/debouncer.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/my_invoices/my_invoices_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:provider/provider.dart';

class InvoiceFilterBottomSheet extends StatelessWidget {
  final Function(String) onItemSelected;
  final String title;

  const InvoiceFilterBottomSheet({
    super.key,
    required this.onItemSelected,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    final debouncer = Debouncer(milliseconds: 500);

    return Consumer<MyInvoicesViewModel>(
      builder: (context, vm, child) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextInputWidget(
              labelText: LocaleKeys.search.tr(context: context),
              hintText: "${LocaleKeys.search.tr(context: context)} $title",
              controller: vm.filterSearchController,
              onChange: (value) {
                debouncer.run(() {
                  vm.searchFilterItems(value ?? "");
                });
              },
              isEnable: true,
              prefix: null,
              autoFocus: false,
              isBorderAllowed: false,
              keyboardType: TextInputType.text,
              isAllowLeadingIcon: true,
              selectionType: SelectionTypeEnum.search,
              padding: EdgeInsets.symmetric(
                vertical: ResponsiveExtension(10).h,
                horizontal: ResponsiveExtension(15).h,
              ),
            ),
            SizedBox(height: 24.h),
            SizedBox(
              height: MediaQuery.sizeOf(context).height * 0.4,
              child: vm.filterDisplayList.isEmpty
                  ? Center(
                      child: Text(
                        "No $title found",
                        style: TextStyle(
                          fontSize: 16,
                          color: AppColors.greyTextColor,
                        ),
                      ),
                    )
                  : ListView.separated(
                      itemBuilder: (_, index) {
                        final item = vm.filterDisplayList[index];
                        return FilterListItem(
                          itemName: item,
                        ).onPress(() {
                          vm.selectFilterItem(item);
                          onItemSelected(item);
                        });
                      },
                      separatorBuilder: (_, __) => SizedBox(height: 16.h),
                      itemCount: vm.filterDisplayList.length,
                    ),
            ),
          ],
        );
      },
    );
  }
}

class FilterListItem extends StatelessWidget {
  final String itemName;

  const FilterListItem({super.key, required this.itemName});

  AppState get appState => getIt.get<AppState>();

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: false,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Text(
              itemName,
              style: TextStyle(
                fontWeight: FontWeight.w600,
                fontSize: 16,
                color: AppColors.blackColor,
              ),
            ),
          ),
          Transform.flip(
            flipX: appState.isArabic(),
            child: Utils.buildSvgWithAssets(
              icon: AppAssets.forward_arrow_icon_small,
              iconColor: AppColors.blackColor,
              width: 18.h,
              height: 13.h,
              fit: BoxFit.contain,
            ),
          ),
        ],
      ).paddingSymmetrical(16.h, 16.h),
    );
  }
}
