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/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/medical_file/medical_file_view_model.dart';
import 'package:hmg_patient_app_new/features/medical_file/models/patient_sickleave_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/medical_file/patient_sickleaves_list_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/common_bottom_sheet.dart';
import 'package:hmg_patient_app_new/widgets/loader/bottomsheet_loader.dart';
import 'package:hmg_patient_app_new/widgets/routes/custom_page_route.dart';
import 'package:open_filex/open_filex.dart';
import 'package:provider/provider.dart';

import 'dart:ui' as ui;

class PatientSickLeaveCard extends StatelessWidget {
  PatientSickLeaveCard({super.key, required this.patientSickLeavesResponseModel, this.isLoading = false, this.isSickLeaveListPage = false});

  late MedicalFileViewModel medicalFileViewModel;
  PatientSickLeavesResponseModel patientSickLeavesResponseModel;
  bool isLoading;
  bool isSickLeaveListPage = false;

  @override
  Widget build(BuildContext context) {
    AppState _appState = getIt.get<AppState>();
    medicalFileViewModel = Provider.of<MedicalFileViewModel>(context, listen: false);
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24, hasShadow: true),
      child: Padding(
        padding: EdgeInsets.all(16.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
            //   children: [
            //     AppCustomChipWidget(
            //       labelText: isLoading ? "" : getStatusText(context),
            //       backgroundColor: getStatusColor().withOpacity(0.15),
            //       textColor: getStatusColor(),
            //     ).toShimmer2(isShow: isLoading, width: 100.h),
            //   ],
            // ),
            // SizedBox(height: 16.h),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Utils.buildImgWithNetwork(
                  url: isLoading ? "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png" : patientSickLeavesResponseModel.doctorImageURL!,
                  iconColor: AppColors.transparent,
                  width: 30.h,
                  height: 30.h,
                  fit: BoxFit.cover,
                ).circle(100).toShimmer2(isShow: isLoading),
                SizedBox(width: 16.h),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      (isLoading ? "" : patientSickLeavesResponseModel.doctorName!).toText16(isBold: true).toShimmer2(isShow: isLoading),
                      SizedBox(height: 8.h),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 3.h,
                        runSpacing: 4.h,
                        children: [
                          Directionality(
                            textDirection: ui.TextDirection.ltr,
                            child: AppCustomChipWidget(
                              icon: AppAssets.doctor_calendar_icon,
                              labelText: DateUtil.formatDateToDate(DateUtil.convertStringToDate(patientSickLeavesResponseModel.appointmentDate), false),
                              labelPadding: EdgeInsetsDirectional.only(start: -4.h, end: 8.h),
                              isEnglishOnly: true,
                            ).toShimmer2(isShow: isLoading),
                          ),
                          AppCustomChipWidget(labelText: isLoading ? "Pending Activation" : patientSickLeavesResponseModel.clinicName!).toShimmer2(isShow: isLoading),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                isLoading
                    ? Container().toShimmer2(isShow: true, height: 40.h, width: 100.h, radius: 12.h)
                    : Expanded(
                        flex: 6,
                        child: CustomButton(
                          text: LocaleKeys.downloadReport.tr(context: context),
                          onPressed: () async {
                            LoaderBottomSheet.showLoader();
                            await medicalFileViewModel.getPatientSickLeavePDF(patientSickLeavesResponseModel, _appState.getAuthenticatedUser()!).then((val) async {
                              LoaderBottomSheet.hideLoader();
                              if (medicalFileViewModel.patientSickLeavePDFBase64.isNotEmpty) {
                                String path = await Utils.createFileFromString(medicalFileViewModel.patientSickLeavePDFBase64, "pdf");
                                try {
                                  OpenFilex.open(path);
                                } catch (ex) {
                                  showCommonBottomSheetWithoutHeight(
                                    context,
                                    child: Utils.getErrorWidget(loadingText: "Cannot open file"),
                                    callBackFunc: () {},
                                    isFullScreen: false,
                                    isCloseButtonVisible: true,
                                  );
                                }
                              }
                            });
                          },
                          backgroundColor: AppColors.secondaryLightRedColor,
                          borderColor: AppColors.secondaryLightRedColor,
                          textColor: AppColors.primaryRedColor,
                          fontSize: 14.f,
                          isBold: true,
                          borderRadius: 12.r,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 40.h,
                          icon: AppAssets.download,
                          iconColor: AppColors.primaryRedColor,
                          iconSize: 14.h,
                        ).toShimmer2(isShow: isLoading),
                      ),
                SizedBox(width: 8.h),
                isSickLeaveListPage ? SizedBox.shrink() : Expanded(
                  flex: 1,
                  child: Container(
                    height: 40.h,
                    width: 40.h,
                    decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                      color: AppColors.textColor,
                      borderRadius: 10.h,
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(10.h),
                      child: Transform.flip(
                        flipX: _appState.isArabic(),
                        child: Utils.buildSvgWithAssets(
                          icon: AppAssets.forward_arrow_icon_small,
                          iconColor: AppColors.whiteColor,
                          width: 40.h,
                          height: 40.h,
                          fit: BoxFit.contain,
                        ),
                      ),
                    ),
                  ).toShimmer2(isShow: isLoading).onPress(() {
                    Navigator.of(context).push(
                      CustomPageRoute(
                        page: PatientSickleavesListPage(),
                      ),
                    );
                  }),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  String getStatusText(BuildContext context) {
    String statusText = "";
    if (patientSickLeavesResponseModel.status == 1) {
      statusText = LocaleKeys.pendingActivation.tr(context: context);
    } else if (patientSickLeavesResponseModel.status == 2) {
      statusText = LocaleKeys.ready.tr(context: context);
    } else if (patientSickLeavesResponseModel.status == 3) {
      statusText = LocaleKeys.awaitingApproval.tr(context: context);
    } else {
      statusText = "";
    }
    return statusText;
  }

  Color getStatusColor() {
    Color statusColor = AppColors.whiteColor;
    if (patientSickLeavesResponseModel.status == 1) {
      statusColor = AppColors.statusPendingColor;
    } else if (patientSickLeavesResponseModel.status == 2) {
      statusColor = AppColors.successColor;
    } else if (patientSickLeavesResponseModel.status == 3) {
      statusColor = AppColors.primaryRedColor;
    } else {
      statusColor = AppColors.whiteColor;
    }
    return statusColor;
  }
}
