import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/size_utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/hmg_services/hmg_services_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/models/resp_models/hospital_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/comprehensive_checkup/widgets/cmc_hospital_list_item.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';
import 'package:shimmer/shimmer.dart';

class CmcHospitalBottomSheetBody extends StatelessWidget {
  final Function(HospitalsModel) onHospitalSelected;

  const CmcHospitalBottomSheetBody({super.key, required this.onHospitalSelected});

  Widget _buildLoadingShimmer() {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: 4,
      separatorBuilder: (_, __) => SizedBox(height: 12.h),
      itemBuilder: (context, index) {
        return Shimmer.fromColors(
          baseColor: Colors.grey[300]!,
          highlightColor: Colors.grey[100]!,
          child: Container(
            height: 80.h,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10.r),
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final appState = getIt.get<AppState>();
    final bool isArabic = appState.isArabic();
    final bool isLocationEnabled = (appState.userLat != 0) && (appState.userLong != 0);

    return Consumer(
      builder: (BuildContext context, HmgServicesViewModel hmgServicesViewModel, Widget? child) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                LocaleKeys.choosePreferredHospitalForService.tr(context: context).toText14(
                      weight: FontWeight.w400,
                      color: AppColors.greyTextColor,
                      letterSpacing: -0.4,
                    ),
                SizedBox(height: 16.h),
                TextInputWidget(
                  labelText: LocaleKeys.search.tr(),
                  hintText: LocaleKeys.searchHospital.tr(),
                  onChange: (value) {
                    hmgServicesViewModel.filterHospitalsByString(value ?? '', isArabic);
                  },
                  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: 8.h),
            SizedBox(
              height: MediaQuery.of(context).size.height * 0.4,
              child: hmgServicesViewModel.isHospitalListLoading
                  ? _buildLoadingShimmer()
                  : hmgServicesViewModel.filteredHospitalsList.isEmpty
                      ? Center(
                          child: LocaleKeys.noHospitalsFound.tr(context: context).toText16(isBold: true, color: AppColors.greyTextColor),
                        )
                      : ListView.separated(
                          itemCount: hmgServicesViewModel.filteredHospitalsList.length,
                          separatorBuilder: (context, index) => SizedBox(height: 12.h),
                          itemBuilder: (context, index) {
                            final hospital = hmgServicesViewModel.filteredHospitalsList[index];
                            return CmcHospitalListItem(
                              hospital: hospital,
                              isLocationEnabled: isLocationEnabled,
                              onPress: () {
                                hmgServicesViewModel.setSelectedHospital(hospital);
                                onHospitalSelected(hospital);
                              },
                            );
                          },
                        ),
            ),
          ],
        );
      },
    );
  }
}
