import 'dart:developer';

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_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.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/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.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/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';

class CmcHospitalListItem extends StatelessWidget {
  final HospitalsModel hospital;
  final VoidCallback onPress;
  final bool isLocationEnabled;

  const CmcHospitalListItem({
    super.key,
    required this.hospital,
    required this.onPress,
    this.isLocationEnabled = false,
  });

  @override
  Widget build(BuildContext context) {
    final appState = getIt.get<AppState>();
    final bool isArabic = appState.isArabic();
    final String hospitalName = isArabic ? (hospital.nameN ?? hospital.name ?? '') : (hospital.name ?? '');

    return InkWell(
      onTap: onPress,
      child: DecoratedBox(
        decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
          color: AppColors.whiteColor,
          borderRadius: 20.h,
          hasShadow: false,
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                spacing: 8.h,
                children: [
                  _buildHospitalName(hospitalName),
                  _buildDistanceInfo(),
                ],
              ),
            ),
            Transform.flip(
              flipX: isArabic,
              child: Utils.buildSvgWithAssets(
                icon: AppAssets.forward_arrow_icon_small,
                iconColor: AppColors.blackColor,
                width: 18.h,
                height: 13.h,
                fit: BoxFit.contain,
              ),
            ),
          ],
        ).paddingSymmetrical(16.h, 16.h),
      ),
    );
  }

  Widget _buildHospitalName(String hospitalName) {
    return Row(
      children: [
        Utils.buildSvgWithAssets(
          icon: (hospital.isHMC == true) ? AppAssets.hmc : AppAssets.hmg,
        ).paddingOnly(right: 10),
        Expanded(
          child: Text(
            hospitalName,
            style: TextStyle(
              fontWeight: FontWeight.w600,
              fontSize: 16,
              color: AppColors.blackColor,
            ),
          ),
        )
      ],
    );
  }

  Widget _buildDistanceInfo() {
    log("hospital: ${hospital.distanceInKilometers}");
    final distanceText = hospital.distanceInKilometers != null ? hospital.distanceInKilometers!.toStringAsFixed(1) : "0";

    return Row(
      spacing: 4.w,
      children: [
        Visibility(
          visible: (hospital.distanceInKilometers != null && hospital.distanceInKilometers! > 0),
          child: AppCustomChipWidget(
            labelText: "$distanceText km",
            icon: AppAssets.location_red,
            iconColor: AppColors.errorColor,
            backgroundColor: AppColors.secondaryLightRedColor,
            textColor: AppColors.errorColor,
          ),
        ),
        Visibility(
          visible: (hospital.distanceInKilometers == null || hospital.distanceInKilometers == 0),
          child: AppCustomChipWidget(
            labelText: LocaleKeys.noDistance.tr(),
            textColor: AppColors.blackColor,
          ),
        ),
        // Visibility(
        //   visible: !isLocationEnabled,
        //   child: AppCustomChipWidget(
        //     labelText: "Location turned off".needTranslation,
        //     deleteIcon: AppAssets.location_unavailable,
        //     deleteIconSize: Size(9.w, 12.h),
        //     textColor: AppColors.blackColor,
        //   ),
        // ),
      ],
    );
  }
}
