import 'package:easy_localization/easy_localization.dart'
    show tr, StringTranslateExtension;
import 'package:flutter/material.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_appointments/models/facility_selection.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart' show HospitalsModel;
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/widgets/hospital_bottom_sheet/hospital_list_items.dart';
import 'package:hmg_patient_app_new/presentation/emergency_services/call_ambulance/widgets/type_selection_widget.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';

class HospitalBottomSheetBody extends StatelessWidget {

  final TextEditingController searchText ;
  final Debouncer  debouncer = Debouncer(milliseconds: 500);

  final int hmcCount;
  final int hmgCount;
  final List<HospitalsModel>? displayList;
  final FacilitySelection selectedFacility;
  final Function(FacilitySelection) onFacilityClicked;
  final Function(HospitalsModel) onHospitalClicked;
  final Function(String) onHospitalSearch;

  HospitalBottomSheetBody({super.key, required this.hmcCount, required this.hmgCount, this.displayList, required this.selectedFacility, required this.onFacilityClicked, required this.onHospitalClicked, required this.onHospitalSearch, required this.searchText});

  @override
  Widget build(BuildContext context) {

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        TextInputWidget(
          labelText: LocaleKeys.search.tr(),
          hintText: LocaleKeys.searchHospital.tr(),
          controller: searchText,
          onChange: (value) {
            debouncer.run((){
              onHospitalSearch(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),
        TypeSelectionWidget(
          selectedFacility:selectedFacility ,
          hmcCount: hmcCount.toString(),
          hmgCount: hmgCount.toString(),
          onitemClicked: (selectedValue){
            onFacilityClicked(selectedValue);
          },
        ),
        SizedBox(height: 21.h),
        SizedBox(
          height: MediaQuery.sizeOf(context).height * .4,
          child: ListView.separated(
              itemBuilder: (_, index)
              {
                var hospital = displayList?[index];
                return HospitalListItem(
                  hospitalData: Utils.convertToPatientDoctorAppointmentList(hospital),
                  isLocationEnabled: true,
              ).onPress(() {
                onHospitalClicked(hospital!);
                });},
              separatorBuilder: (_, __) => SizedBox(
                height: 16.h,
              ),
              itemCount: displayList?.length ?? 0,
            ))
      ],
    );
  }
}
