import 'dart:io';

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/contact_us/contact_us_view_model.dart';
import 'package:hmg_patient_app_new/features/contact_us/models/resp_models/get_hmg_locations.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:map_launcher/map_launcher.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:ui' as ui;

class FindUsItemCard extends StatelessWidget {
  FindUsItemCard({super.key, required this.getHMGLocationsModel});

  late AppState appState;
  late ContactUsViewModel contactUsViewModel;
  GetHMGLocationsModel getHMGLocationsModel;

  @override
  Widget build(BuildContext context) {
    appState = getIt.get<AppState>();
    contactUsViewModel = getIt.get<ContactUsViewModel>();
    return DecoratedBox(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.r,
        hasShadow: false,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              spacing: 8.h,
              children: [hospitalName, distanceInfo],
            ),
          ),
        ],
      ).paddingSymmetrical(16.h, 16.h),
    );
  }

  Widget get hospitalName => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          (getHMGLocationsModel.distanceInKilometers != 0 && contactUsViewModel.hasLocationEnabled)
              ? Column(
                  children: [
                    Directionality(
                      textDirection: ui.TextDirection.ltr,
                      child: AppCustomChipWidget(
                        labelText: "${getHMGLocationsModel.distanceInKilometers ?? ""} km",
                        labelPadding: EdgeInsetsDirectional.only(start: -4.h, end: 8.w),
                        icon: AppAssets.location_red,
                        isEnglishOnly: true,
                        // iconColor: AppColors.primaryRedColor,
                        // backgroundColor: AppColors.secondaryLightRedColor,
                        // textColor: AppColors.errorColor,
                      ),
                    ),
                    SizedBox(
                      height: 16.h,
                    ),
                  ],
                )
              : SizedBox.shrink(),
          Row(
            children: [
              Utils.buildImgWithNetwork(
                url: getHMGLocationsModel.projectImageURL ?? "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png",
                iconColor: AppColors.transparent,
                width: 40.h,
                height: 40.h,
                fit: BoxFit.cover,
              ).circle(100).toShimmer2(isShow: false).paddingOnly(right: 10),
              Expanded(
                child: Text(
                  getHMGLocationsModel.locationName!,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                    fontSize: 16,
                    color: AppColors.blackColor,
                  ),
                ),
              )
            ],
          ),
        ],
      );

  Widget get distanceInfo => Row(
        children: [
          Expanded(
            flex: 7,
            child: CustomButton(
              text: LocaleKeys.getDirections.tr(),
              onPressed: () async {
                await MapLauncher.showMarker(
                  mapType: MapType.google,
                  coords: Coords(double.parse(getHMGLocationsModel.latitude ?? "0.0"), double.parse(getHMGLocationsModel.longitude ?? "0.0")),
                  title: getHMGLocationsModel.locationName ?? "Hospital",
                ).catchError((err) {
                  MapLauncher.showMarker(
                    mapType: Platform.isIOS ? MapType.apple : MapType.google,
                    coords: Coords(double.parse(getHMGLocationsModel.latitude ?? "0.0"), double.parse(getHMGLocationsModel.longitude ?? "0.0")),
                    title: getHMGLocationsModel.locationName ?? "Hospital",
                  );
                });
              },
              backgroundColor: AppColors.transparent,
              borderColor: AppColors.textColor,
              textColor: AppColors.blackColor,
              borderWidth: 1.h,
              fontSize: (isFoldable || isTablet) ? 12.f : 14.f,
              fontWeight: FontWeight.w600,
              borderRadius: 12.r,
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              height: 40.h,
              icon: AppAssets.directions_icon,
              iconColor: AppColors.blackColor,
              iconSize: 16.h,
            ),
          ),
          SizedBox(width: 8.w),
          Expanded(
            flex: 1,
            child: Container(
              height: (isFoldable || isTablet) ? 50.h : 40.h,
              decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                color: AppColors.transparent,
                borderRadius: 10.h,
                side: BorderSide(
                  color: AppColors.textColor,
                  width: 1.2,
                ),
              ),
              child: Transform.flip(
                flipX: appState.isArabic(),
                child: Utils.buildSvgWithAssets(
                  icon: AppAssets.call_fill,
                  iconColor: AppColors.textColor,
                  width: 5.w,
                  height: 5.h,
                  fit: BoxFit.scaleDown,
                ),
              ),
            ).onPress(() {
              launchUrl(Uri.parse("tel://" + "${getHMGLocationsModel.phoneNumber}"));
            }),
          ),
        ],
      );
}
