import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.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/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/emergency_services/emergency_services_view_model.dart';
import 'package:hmg_patient_app_new/features/hospital/hospital_selection_view_model.dart';
import 'package:hmg_patient_app_new/features/qr_parking/qr_parking_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/my_doctors_page.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/search_doctor_by_name.dart';
import 'package:hmg_patient_app_new/presentation/emergency_services/emergency_services_page.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/health_calculators_page.dart';
import 'package:hmg_patient_app_new/presentation/insurance/insurance_home_page.dart';
import 'package:hmg_patient_app_new/presentation/lab/lab_orders_page.dart';
import 'package:hmg_patient_app_new/presentation/medical_file/patient_sickleaves_list_page.dart';
import 'package:hmg_patient_app_new/presentation/my_invoices/my_invoices_list.dart';
import 'package:hmg_patient_app_new/presentation/parking/paking_page.dart';
import 'package:hmg_patient_app_new/presentation/prescriptions/prescriptions_list_page.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:provider/provider.dart';

import '../../../core/utils/utils.dart';
import '../../../theme/colors.dart';
import '../../emergency_services/call_ambulance/widgets/HospitalBottomSheetBody.dart';
import '../../radiology/radiology_orders_page.dart' show RadiologyOrdersPage;

class SmallServiceCard extends StatelessWidget {
  final String serviceName;
  final String icon;
  final String title;
  final String subtitle;
  final Color? iconColor;
  final Color? textColor;
  final Color? backgroundColor;
  final bool isBold;

  SmallServiceCard({
    super.key,
    this.icon = "",
    this.serviceName = "",
    this.title = "",
    this.subtitle = "",
    this.backgroundColor,
    this.iconColor,
    this.textColor,
    this.isBold = false,
  });

  @override
  Widget build(BuildContext context) {
    final resolvedBgColor = backgroundColor ?? AppColors.whiteColor;
    final resolvedIconColor = iconColor ?? AppColors.blackColor;
    final resolvedTextColor = textColor ?? AppColors.blackColor;
    return Container(
      height: 92.h,
      width: 92.h,
      padding: EdgeInsets.all(4.0),
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: resolvedBgColor,
        borderRadius: 16,
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Utils.buildSvgWithAssets(icon: icon, iconColor: resolvedIconColor, width: 32.h, height: 32.h),
          SizedBox(height: 6.h),
          if (isFoldable || isTablet) ...[
            title.tr(context: context).toText10(color: resolvedTextColor, isBold: true, isCenter: true, letterSpacing: -0.2, maxlines: 1),
            subtitle.tr(context: context).toText10(color: resolvedTextColor, isBold: true, isCenter: true, letterSpacing: -0.2, maxlines: 1),
          ] else ...[
            title.tr(context: context).toText12(color: resolvedTextColor, isBold: true, isCenter: true, letterSpacing: -0.2, maxLine: 1),
            subtitle.tr(context: context).toText12(color: resolvedTextColor, isBold: true, isCenter: true, letterSpacing: -0.2, maxLine: 1),
          ]
        ],
      ),
    ).onPress(() {
      switch (serviceName) {
        case "lab_results":
          Navigator.of(context).push(
            CustomPageRoute(
              page: LabOrdersPage(),
            ),
          );
          break;
        case "radiology_results":
          Navigator.of(context).push(
            CustomPageRoute(
              page: RadiologyOrdersPage(),
            ),
          );
          break;
        case "my_invoice":
          Navigator.of(context).push(
            CustomPageRoute(
              page: MyInvoicesList(),
            ),
          );
          break;
        case "prescriptions":
          Navigator.of(context).push(
            CustomPageRoute(
              page: PrescriptionsListPage(),
            ),
          );
          break;
        case "insurance_update":
          Navigator.of(context).push(
            CustomPageRoute(
              page: InsuranceHomePage(),
            ),
          );
          break;

        case "my_doctors":
          Navigator.of(context).push(
            CustomPageRoute(
              page: MyDoctorsPage(),
            ),
          );
          break;

        case "sick_leaves":
          Navigator.of(context).push(
            CustomPageRoute(
              page: PatientSickleavesListPage(),
            ),
          );
          break;
        case "emergency":
          context.read<EmergencyServicesViewModel>().flushData();
          context.read<EmergencyServicesViewModel>().getTransportationOrders(
                showLoader: false,
              );
          context.read<EmergencyServicesViewModel>().getRRTOrders(
                showLoader: false,
              );
          Navigator.of(context).push(
            CustomPageRoute(
              page: EmergencyServicesPage(),
              settings: const RouteSettings(name: '/EmergencyServicesPage'),
            ),
          );
          break;

        case "indoor_navigation":
          openIndoorNavigationBottomSheet(context);

        case "search_doctor":
          Navigator.of(context).push(
            CustomPageRoute(
              page: SearchDoctorByName(),
            ),
          );
          break;
        case "health_calculators_and_converts":
          Navigator.of(context).push(
            CustomPageRoute(
              page: HealthCalculatorsPage(type: HealthCalConEnum.calculator),
            ),
          );
          break;
        case "health_converters":
          Navigator.of(context).push(
            CustomPageRoute(
              page: HealthCalculatorsPage(type: HealthCalConEnum.converter),
            ),
          );
          break;
        case "parking_guide":
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (_) => ChangeNotifierProvider(
                create: (_) => getIt<QrParkingViewModel>(),
                child: const ParkingPage(),
              ),
            ),
          );
          break;
        default:
          // Handle unknown service
          break;
      }
    });
  }

  void openIndoorNavigationBottomSheet(BuildContext context) {
    showCommonBottomSheetWithoutHeight(
      title: LocaleKeys.selectHospital.tr(context: context),
      context,
      child: ChangeNotifierProvider(
        create: (context) => HospitalSelectionBottomSheetViewModel(getIt()),
        child: Consumer<HospitalSelectionBottomSheetViewModel>(
          builder: (_, vm, __) => HospitalBottomSheetBody(
            searchText: vm.searchController,
            displayList: vm.displayList,
            onFacilityClicked: (value) {
              vm.setSelectedFacility(value);
              vm.getDisplayList();
            },
            onHospitalClicked: (hospital) {
              Navigator.pop(context);
              vm.openPenguin(hospital);
            },
            onHospitalSearch: (value) {
              vm.searchHospitals(value ?? "");
            },
            selectedFacility: vm.selectedFacility,
            hmcCount: vm.hmcCount,
            hmgCount: vm.hmgCount,
          ),
        ),
      ),
      isFullScreen: false,
      isCloseButtonVisible: true,
      hasBottomPadding: false,
      backgroundColor: AppColors.bottomSheetBgColor,
      callBackFunc: () {
        context.read<EmergencyServicesViewModel>().clearSearchText();
      },
    );
  }
}
