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/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/appointment_via_region_viewmodel.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/facility_selection.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
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/appointments/widgets/hospital_bottom_sheet/type_selection_widget.dart';
import 'package:hmg_patient_app_new/presentation/lab/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/theme/colors.dart' show AppColors;
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:provider/provider.dart';

class HospitalBottomSheetBody extends StatelessWidget {
  late BookAppointmentsViewModel appointmentsViewModel;
  late AppointmentViaRegionViewmodel regionalViewModel;
  final TextEditingController searchText = TextEditingController();

  HospitalBottomSheetBody({super.key});

  @override
  Widget build(BuildContext context) {
    appointmentsViewModel = Provider.of<BookAppointmentsViewModel>(context);
    regionalViewModel = Provider.of<AppointmentViaRegionViewmodel>(context);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          LocaleKeys.selectHospital.tr(),
          style: TextStyle(
            fontSize: 21,
            fontWeight: FontWeight.w600,
            color: AppColors.blackColor,
          ),
        ),
        Text(
          LocaleKeys.selectHospitalSubTitle.tr(),
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w500,
            color: AppColors.greyTextColor,
          ),
        ),
        SizedBox(height: 16.h),
        TextInputWidget(
          labelText: LocaleKeys.search.tr(),
          hintText: LocaleKeys.searchHospital.tr(),
          controller: searchText,
          onChange: (value) {
            appointmentsViewModel.filterHospitalListByString(value, regionalViewModel.selectedRegionId , regionalViewModel.selectedFacilityType ==
                FacilitySelection.HMG.name);
          },
          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(
        //   hmcCount: "0",
        //   hmgCount: "0",
        // ),
        // SizedBox(height: 21.h),
        SizedBox(
          height: MediaQuery.sizeOf(context).height * .4,
          child: ListView.separated(
              itemBuilder: (_, index)
              {
                var hospital = regionalViewModel.selectedFacilityType ==
                    FacilitySelection.HMG.name
                    ? appointmentsViewModel
                    .filteredHospitalList!
                    .registeredDoctorMap![
                regionalViewModel.selectedRegionId!]!
                    .hmgDoctorList![index]
                    : appointmentsViewModel
                    .filteredHospitalList
                    ?.registeredDoctorMap?[
                regionalViewModel.selectedRegionId!]
                    ?.hmcDoctorList?[index];
                return HospitalListItem(
                hospitalData: hospital,
                isLocationEnabled: appointmentsViewModel.isLocationEnabled(),
              ).onPress(() {
                regionalViewModel.setHospitalModel(hospital);
                regionalViewModel.setBottomSheetState(AppointmentViaRegionState.CLINIC_SELECTION);
                regionalViewModel.handleLastStep();
              });},
              separatorBuilder: (_, __) => SizedBox(
                    height: 16.h,
                  ),
              itemCount: (regionalViewModel.selectedFacilityType ==
                              FacilitySelection.HMG.name
                          ? (appointmentsViewModel.filteredHospitalList?.registeredDoctorMap?[
                                  regionalViewModel.selectedRegionId]?.hmgDoctorList)
                          : (appointmentsViewModel
                              .filteredHospitalList
                              ?.registeredDoctorMap?[
                                  regionalViewModel.selectedRegionId]?.hmcDoctorList))?.length ??
                  0),
        )
      ],
    );
  }
}
