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/utils/date_util.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/insurance/models/resp_models/patient_insurance_approval_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/insurance/insurance_approval_details_page.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:hmg_patient_app_new/widgets/routes/custom_page_route.dart';

import 'dart:ui' as ui;

class InsuranceApprovalCard extends StatelessWidget {
  InsuranceApprovalCard({super.key, required this.insuranceApprovalResponseModel, required this.isLoading, required this.appState});

  InsuranceApprovalResponseModel insuranceApprovalResponseModel;
  AppState appState;
  bool isLoading;

  /// Helper method to determine if approval is partially approved
  bool _isPartiallyApproved() {
    final description = insuranceApprovalResponseModel.approvalStatusDescption?.toLowerCase() ?? '';
    return description.contains('partial') || insuranceApprovalResponseModel.status == 8;
  }

  /// Helper method to get chip background color based on approval status
  Color _getChipBackgroundColor() {
    if (insuranceApprovalResponseModel.status == 9) {
      return AppColors.successColor.withValues(alpha: 0.1);
    } else if (_isPartiallyApproved()) {
      return AppColors.warningColorYellow.withValues(alpha: 0.1);
    } else {
      return AppColors.primaryRedColor.withValues(alpha: 0.1);
    }
  }

  /// Helper method to get chip text color based on approval status
  Color _getChipTextColor() {
    if (insuranceApprovalResponseModel.status == 9) {
      return AppColors.successColor;
    } else if (_isPartiallyApproved()) {
      return AppColors.warningColorYellow;
    } else {
      return AppColors.primaryRedColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: true,
      ),
      child: Padding(
        padding: EdgeInsets.all(14.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Wrap(
              alignment: WrapAlignment.start,
              direction: Axis.horizontal,
              spacing: 6.h,
              runSpacing: 6.h,
              children: [
                AppCustomChipWidget(
                  labelText: isLoading ? "Approved" : insuranceApprovalResponseModel.approvalStatusDescption,
                  backgroundColor: isLoading ? AppColors.successColor.withValues(alpha: 0.1) : _getChipBackgroundColor(),
                  textColor: isLoading ? AppColors.successColor : _getChipTextColor(),
                ).toShimmer2(isShow: isLoading),
                AppCustomChipWidget(
                  icon: isLoading ? AppAssets.walkin_appointment_icon : (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppAssets.walkin_appointment_icon : AppAssets.small_livecare_icon),
                  iconColor: isLoading
                      ? AppColors.textColor
                      : !insuranceApprovalResponseModel.isLiveCareAppointment!
                          ? AppColors.textColor
                          : AppColors.whiteColor,
                  labelText: isLoading
                      ? "Walk In"
                      : insuranceApprovalResponseModel.isLiveCareAppointment!
                          ? LocaleKeys.livecare.tr(context: context)
                          : LocaleKeys.walkin.tr(context: context),
                  backgroundColor: isLoading ? AppColors.greyColor : (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppColors.greyColor : AppColors.successColor),
                  textColor: isLoading ? AppColors.textColor : (!insuranceApprovalResponseModel.isLiveCareAppointment! ? AppColors.textColor : AppColors.whiteColor),
                ).toShimmer2(isShow: isLoading),
                AppCustomChipWidget(
                  labelText: isLoading
                      ? "OutPatient"
                      : appState.isArabic()
                          ? insuranceApprovalResponseModel.isInOutPatientDescriptionN!
                          : insuranceApprovalResponseModel.isInOutPatientDescription!,
                  backgroundColor: AppColors.warningColorYellow.withValues(alpha: 0.1),
                  textColor: AppColors.warningColorYellow,
                ).toShimmer2(isShow: isLoading),
              ],
            ).toShimmer2(isShow: isLoading),
            SizedBox(height: 16.h),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Utils.buildImgWithNetwork(
                  url: isLoading ? "https://hmgwebservices.com/Images/MobileImages/OALAY/1439.png" : insuranceApprovalResponseModel.doctorImageURL!,
                  iconColor: AppColors.blackBgColor,
                  width: 63.h,
                  height: 63.h,
                  fit: BoxFit.cover,
                ).circle(100).toShimmer2(isShow: isLoading),
                SizedBox(width: 16.h),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      (isLoading ? "John Smith" : insuranceApprovalResponseModel.doctorName ?? "").toText16(isBold: true).toShimmer2(isShow: isLoading),
                      SizedBox(height: 8.h),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 3.h,
                        runSpacing: 4.h,
                        children: [
                          // AppCustomChipWidget(labelText: isLoading ? "Approved" : insuranceApprovalResponseModel.approvalStatusDescption!,
                          //   backgroundColor: insuranceApprovalResponseModel.status == 9 ? AppColors.successColor.withValues(alpha: 0.1) : AppColors.primaryRedColor.withValues(alpha: 0.1),
                          //   textColor: insuranceApprovalResponseModel.status == 9 ? AppColors.successColor : AppColors.primaryRedColor,
                          // ).toShimmer2(isShow: isLoading),
                          AppCustomChipWidget(labelText: isLoading ? "Cardiology" : insuranceApprovalResponseModel.clinicName ?? "").toShimmer2(isShow: isLoading),
                          Directionality(
                            textDirection: ui.TextDirection.ltr,
                            child: AppCustomChipWidget(
                                    icon: AppAssets.doctor_calendar_icon,
                                    labelText: isLoading ? "Cardiology" : DateUtil.formatDateToDate(DateUtil.convertStringToDate(insuranceApprovalResponseModel.submitOn), false), isEnglishOnly: true,)
                                .toShimmer2(isShow: isLoading),
                          ),
                          isLoading
                              ? SizedBox.shrink()
                              : AppCustomChipWidget(
                                      labelText: isLoading ? LocaleKeys.approvalNo.tr(context: context) : "${LocaleKeys.approvalNo.tr(context: context)} ${insuranceApprovalResponseModel.approvalNo}", isEnglishOnly: true,)
                                  .toShimmer2(isShow: isLoading),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(
              height: 12.h,
            ),
            CustomButton(
              text: LocaleKeys.viewDetails.tr(context: context),
              onPressed: () async {
                Navigator.of(context).push(
                  CustomPageRoute(
                    page: InsuranceApprovalDetailsPage(insuranceApprovalResponseModel: insuranceApprovalResponseModel),
                  ),
                );
              },
              backgroundColor: AppColors.secondaryLightRedColor,
              borderColor: AppColors.secondaryLightRedColor,
              textColor: AppColors.primaryRedColor,
              fontSize: (isFoldable || isTablet) ? 12.f : 14.f,
              fontWeight: FontWeight.w600,
              borderRadius: 12.r,
              padding: EdgeInsets.symmetric(horizontal: 10.w),
              height: isTablet || isFoldable ? 46.h : 40.h,
              // height: 40.h,
              // icon: AppAssets.insurance,
              // iconColor: AppColors.primaryRedColor,
              iconSize: 16.h,
            ).toShimmer2(isShow: isLoading),
          ],
        ),
      ),
    );
  }
}
