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_export.dart';
import 'package:hmg_patient_app_new/core/utils/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/emergency_services/models/resp_model/ProjectAvgERWaitingTime.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/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';
import 'package:provider/provider.dart';

import 'dart:ui' as ui;

class NearestERItem extends StatelessWidget {
  final ProjectAvgERWaitingTime nearestERItem;
  final bool isLoading;


  const NearestERItem({ super.key,
    required this.nearestERItem,
    required this.isLoading
  }) ;

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        customBorder: BorderRadius.only(
          topLeft: Radius.circular(24.h),
          topRight: Radius.circular(24.h),
        ),
      ),
      child: Padding(
        padding: EdgeInsets.all(16.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                (isLoading || nearestERItem.projectImageURL?.isEmpty == true)
                    ? Container(
                  width: 24.h,
                  height: 24.h,
                  decoration: BoxDecoration(
                    color: Colors.grey.shade300,
                    shape: BoxShape.circle,
                  ),
                ).toShimmer2(isShow: isLoading)
                    : Utils.buildImgWithNetwork(
                  url: nearestERItem.projectImageURL ?? '',
                  iconColor: Colors.transparent,
                ).circle(24.h).toShimmer2(isShow: isLoading),
                const SizedBox(width: 12),
                Expanded(
                  child: (nearestERItem.projectName?.toText16(
                    color: AppColors.textColor,
                    isBold: true
                  ) ??
                      SizedBox.shrink()).toShimmer2(isShow: isLoading),
                ),
                // TODO: Add hospital icon logic here if needed
              ],
            ),
            SizedBox(height: 8.h),
            Row(
              spacing: 8.h,
              children: [
                Directionality(
                  textDirection: ui.TextDirection.ltr,
                  child: AppCustomChipWidget(
                    labelText: "${nearestERItem.distanceInKilometers} km",
                    icon: AppAssets.location,
                    iconHasColor: false,
                    labelPadding: EdgeInsetsDirectional.only(start: 4.h, end: 0.h),
                    padding: EdgeInsets.all(8.h),
                    isEnglishOnly: true,
                  ).toShimmer2(isShow: isLoading),
                ),
                Directionality(
                  textDirection: ui.TextDirection.ltr,
                  child: AppCustomChipWidget(
                    labelText: "${LocaleKeys.waitingTime.tr(context: context)} ${nearestERItem.getTime()} ${LocaleKeys.mins.tr(context: context)}",
                    icon: AppAssets.waiting_time_clock,
                    iconHasColor: false,
                    labelPadding: EdgeInsetsDirectional.only(start: 4.h, end: 0.h),
                    padding: EdgeInsets.all(8.h),
                    isEnglishOnly: true,
                  ).toShimmer2(isShow: isLoading),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                Expanded(
                  child: CustomButton(
                    text: LocaleKeys.getDirections.tr(context: context),
                    iconSize: 16.h,
                    icon: AppAssets.location,
                    onPressed: () {
                      context.read<EmergencyServicesViewModel>().openDirections(destLat:  double.parse(nearestERItem.latitude??"0.0"), destLng: double.parse(nearestERItem.longitude??"0.0") );
                    },
                    backgroundColor: AppColors.secondaryLightRedColor,
                    borderColor: AppColors.secondaryLightRedColor,
                    textColor: AppColors.primaryRedColor,
                    iconColor: AppColors.primaryRedColor,
                    height: 46.h,
                    fontSize: 14.f,
                    isBold: true,
                  ).toShimmer2(isShow: isLoading),
                ),
                SizedBox(width: 8.h),
                SizedBox(
                  height: 46.h,
                  width: 40.h,
                  child: CustomButton(
                    text: '',
                    iconSize: 18.h,
                    icon: AppAssets.call_fill,
                    onPressed: () {
                      context.read<EmergencyServicesViewModel>().openDialer( nearestERItem.phonenumber??"");
                    },
                    backgroundColor: AppColors.greyColor,
                    iconColor: AppColors.textColor,
                    borderColor: AppColors.greyColor,
                    height: 46.h,
                  ).toShimmer2(isShow: isLoading),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
