import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/api_consts.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/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/book_appointment/book_appointment_page.dart';
import 'package:hmg_patient_app_new/presentation/home/data/service_card_data.dart';
import 'package:hmg_patient_app_new/presentation/home/service_info_page.dart';
import 'package:hmg_patient_app_new/presentation/home_health_care/hhc_procedures_page.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../core/utils/utils.dart';
import '../../../theme/colors.dart';

class LargeServiceCard extends StatelessWidget {
  final ServiceCardData serviceCardData;
  final String image;
  final String icon;
  final String title;
  final String subtitle;
  final bool isPNG;

  const LargeServiceCard({
    super.key,
    required this.serviceCardData,
    this.image = "",
    this.icon = "",
    this.title = "",
    this.subtitle = "",
    this.isPNG = false,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 230.w,
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 24.r,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(24.r),
              topRight: Radius.circular(24.r),
            ),
            child: Utils.buildImgWithAssets(
              icon: serviceCardData.largeCardIcon,
              fit: BoxFit.cover,
              width: double.infinity,
              height: isFoldable ? 190.h : (isTablet ? 200.h : 180.h),
            ),
          ),
          Container(
            padding: EdgeInsets.all(16.w),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    isPNG
                        ? Utils.buildImgWithAssets(icon: serviceCardData.icon, width: 35.h, height: 35.h)
                        : Container(
                            height: 48.h,
                            width: 48.h,
                            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                              color: serviceCardData.backgroundColor!,
                              borderRadius: 12.r,
                              hasShadow: false,
                            ),
                            child: Padding(
                              padding: EdgeInsets.all(12.h),
                              child: Utils.buildSvgWithAssets(
                                icon: serviceCardData.icon,
                                iconColor: serviceCardData.iconColor,
                                fit: BoxFit.contain,
                                applyThemeColor: false,
                              ),
                            ),
                          ),
                    SizedBox(width: 12.w),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          serviceCardData.title.tr(context: context).toText14(
                                isBold: true,
                                color: AppColors.textColor,
                                maxlines: 1,
                                textOverflow: TextOverflow.ellipsis,
                              ),
                          serviceCardData.subtitle.tr(context: context).toText12(isBold: true, color: AppColors.textColorLight, maxLine: 2),
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(height: 24.h),
                CustomButton(
                  text: serviceCardData.isBold ? LocaleKeys.visitPharmacyOnline.tr(context: context) : LocaleKeys.bookNow.tr(context: context),
                  onPressed: () {
                    handleOnTap();
                  },
                  padding: EdgeInsets.zero,
                  backgroundColor: serviceCardData.isBold ? AppColors.successLightColor.withValues(alpha: 0.2) : AppColors.bgRedLightColor,
                  borderColor: serviceCardData.isBold ? AppColors.successLightColor.withValues(alpha: 0.01) : AppColors.bgRedLightColor,
                  textColor: serviceCardData.isBold ? AppColors.successColor : AppColors.primaryRedColor,
                  fontSize: 14.f,
                  fontWeight: FontWeight.w600,
                  borderRadius: 10.r,
                  height: 40.h,
                ),
              ],
            ),
          ),
        ],
      ),
    ).onPress(() {
      handleOnTap();
    });
  }

  void handleOnTap() {
    switch (serviceCardData.serviceName) {
      case "livecare":
        {
          getIt.get<MyAppointmentsViewModel>().getPatientFavouriteDoctors();
          getIt.get<BookAppointmentsViewModel>().onTabChanged(1);
          Navigator.of(getIt.get<NavigationService>().navigatorKey.currentContext!).push(
            CustomPageRoute(
              page: BookAppointmentPage(),
            ),
          );
        }
      case "home_health_care":
        {
          if (getIt.get<AppState>().isAuthenticated) {
            Navigator.of(getIt<NavigationService>().navigatorKey.currentContext!).push(
              CustomPageRoute(
                page: HhcProceduresPage(),
              ),
            );
          } else {
            Navigator.of(getIt<NavigationService>().navigatorKey.currentContext!).push(
              CustomPageRoute(
                page: ServiceInfoPage(
                    serviceName: LocaleKeys.homeHealthCare.tr(),
                    serviceHeader: LocaleKeys.homeHealthCareServiceHeader.tr(),
                    serviceDescription: LocaleKeys.homeHealthCareServiceDescription.tr(),
                    serviceImage: AppAssets.homeHealthCareService),
              ),
            );
          }
        }
      case "pharmacy":
        {
          Uri uri = Uri.parse(PHARMACY_REDIRECT_URL);
          launchUrl(uri, mode: LaunchMode.externalApplication);
        }
    }
  }
}

class FadedLargeServiceCard extends StatelessWidget {
  final ServiceCardData serviceCardData;
  final String image;
  final String icon;
  final String title;
  final String subtitle;
  final bool isPNG;

  const FadedLargeServiceCard({
    super.key,
    required this.serviceCardData,
    this.image = "",
    this.icon = "",
    this.title = "",
    this.subtitle = "",
    this.isPNG = false,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 411.h,
      width: 252.w,
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.transparent, borderRadius: 24.r),
      child: Column(
        children: [
          Stack(
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(24.r),
                child: Transform.flip(
                    flipX: getIt.get<AppState>().isArabic(),
                    child: Utils.buildImgWithAssets(icon: serviceCardData.largeCardIcon, fit: BoxFit.cover, width: 520.w, height: 250.h)),
              ),
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                height: 250.h,
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(24.r),
                    gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter,
                      stops: [0.536, 1.0], // 53.6% and 100%
                      colors: [
                        Color(0xFFF8F8F8).withValues(alpha: 0), // Transparent
                        Color(0xFFF8F8F8), // Solid #F8F8F8
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
          SizedBox(height: 16.h),
          Spacer(),
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              isPNG
                  ? Utils.buildImgWithAssets(icon: serviceCardData.icon, width: 32.h, height: 32.h).circle(100.h)
                  : Container(
                      height: 32.h,
                      width: 32.h,
                      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                        color: serviceCardData.backgroundColor!,
                        borderRadius: 30.r,
                        hasShadow: false,
                      ),
                      child: Padding(
                        padding: EdgeInsets.all(8.h),
                        child: Transform.flip(
                          flipX: getIt.get<AppState>().isArabic(),
                          child: Utils.buildSvgWithAssets(
                              icon: serviceCardData.icon, iconColor: serviceCardData.iconColor, fit: BoxFit.contain, applyThemeColor: false),
                        ),
                      ),
                    ),
              SizedBox(width: 12.w),
              serviceCardData.title.tr(context: context).toText18(isBold: true, color: AppColors.textColor).expanded,
            ],
          ),
          SizedBox(height: 10.h),
          serviceCardData.subtitle.tr(context: context).toText14(isBold: true, color: AppColors.blackBgColor, letterSpacing: 0, maxlines: 2),
          SizedBox(height: 12.h),
          CustomButton(
            text: serviceCardData.isBold ? LocaleKeys.visitPharmacyOnline.tr(context: context) : LocaleKeys.bookNow.tr(context: context),
            onPressed: () {
              handleOnTap();
            },
            padding: EdgeInsets.zero,
            backgroundColor: serviceCardData.isBold ? AppColors.successLightColor.withValues(alpha: 0.2) : AppColors.blackBgColor,
            borderColor: serviceCardData.isBold ? AppColors.successLightColor.withValues(alpha: 0.01) : AppColors.blackBgColor,
            textColor: serviceCardData.isBold ? AppColors.successColor : AppColors.whiteColor,
            fontSize: 14.f,
            fontWeight: FontWeight.w600,
            borderRadius: 10.r,
            height: 40.h,
          ),
        ],
      ),
    ).onPress(() {
      handleOnTap();
    });
  }

  void handleOnTap() {
    switch (serviceCardData.serviceName) {
      case "livecare":
        {
          getIt.get<BookAppointmentsViewModel>().onTabChanged(1);
          getIt.get<BookAppointmentsViewModel>().setIsLiveCareSelectedFromHomePage(true);
          Navigator.of(getIt.get<NavigationService>().navigatorKey.currentContext!).push(
            CustomPageRoute(
              page: BookAppointmentPage(),
            ),
          );
        }
      case "home_health_care":
        {
          if (getIt.get<AppState>().isAuthenticated) {
            Navigator.of(getIt<NavigationService>().navigatorKey.currentContext!).push(
              CustomPageRoute(
                page: HhcProceduresPage(),
              ),
            );
          } else {
            Navigator.of(getIt<NavigationService>().navigatorKey.currentContext!).push(
              CustomPageRoute(
                page: ServiceInfoPage(
                    serviceName: LocaleKeys.homeHealthCare.tr(),
                    serviceHeader: LocaleKeys.homeHealthCareServiceHeader.tr(),
                    serviceDescription: LocaleKeys.homeHealthCareServiceDescription.tr(),
                    serviceImage: AppAssets.homeHealthCareService),
              ),
            );
          }
        }
      case "pharmacy":
        {
          Uri uri = Uri.parse(PHARMACY_REDIRECT_URL);
          launchUrl(uri, mode: LaunchMode.externalApplication);
        }
    }
  }
}
