import 'package:flutter/cupertino.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_medical_response_model.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:open_filex/open_filex.dart';
import 'package:share_plus/share_plus.dart';

class PatientMedicalReportCard extends StatelessWidget {
  PatientMedicalReportCard({super.key, required this.patientMedicalReportResponseModel, required this.medicalFileViewModel, this.isLoading = false});

  PatientMedicalReportResponseModel patientMedicalReportResponseModel;
  MedicalFileViewModel medicalFileViewModel;

  bool isLoading = true;

  @override
  Widget build(BuildContext context) {
    AppState _appState = getIt.get<AppState>();
    return Container(
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 20.h,
        hasShadow: true,
      ),
      child: Padding(
        padding: EdgeInsets.all(16.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Utils.buildImgWithNetwork(
                  url: isLoading ? "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png" : patientMedicalReportResponseModel.doctorImageURL!,
                  iconColor: AppColors.transparent,
                  width: 63.h,
                  height: 63.h,
                  fit: BoxFit.fill,
                ).circle(100).toShimmer2(isShow: isLoading),
                SizedBox(width: 16.h),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      (isLoading ? "" : patientMedicalReportResponseModel.doctorName!).toText16(isBold: true).toShimmer2(isShow: isLoading),
                      SizedBox(height: 4.h),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 3.h,
                        runSpacing: 4.h,
                        children: [
                          AppCustomChipWidget(labelText: isLoading ? "" : patientMedicalReportResponseModel.clinicDescription!).toShimmer2(isShow: isLoading),
                          AppCustomChipWidget(labelText: isLoading ? "" : patientMedicalReportResponseModel.projectName!).toShimmer2(isShow: isLoading),
                          AppCustomChipWidget(
                                  icon: AppAssets.doctor_calendar_icon,
                                  labelText: isLoading
                                      ? ""
                                      : "${DateUtil.formatDateToDate(DateUtil.convertStringToDate(patientMedicalReportResponseModel.requestDate), false)}, ${DateUtil.formatDateToTimeLang(DateUtil.convertStringToDate(patientMedicalReportResponseModel.requestDate), false)}")
                              .toShimmer2(isShow: isLoading),
                          AppCustomChipWidget(
                                  icon: AppAssets.rating_icon, iconColor: AppColors.ratingColorYellow, labelText: isLoading ? "" : "Rating: ${patientMedicalReportResponseModel.decimalDoctorRate}")
                              .toShimmer2(isShow: isLoading),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            patientMedicalReportResponseModel.status == 2
                ? Padding(
                    padding: EdgeInsets.only(top: 16.h),
                    child: Row(
                      children: [
                        Expanded(
                          child: CustomButton(
                            text: "Share",
                            onPressed: () {
                              getMedicalReportPDF(true, context, _appState);
                            },
                            backgroundColor: AppColors.secondaryLightRedColor,
                            borderColor: AppColors.secondaryLightRedColor,
                            textColor: AppColors.primaryRedColor,
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                            borderRadius: 12.h,
                            height: 40.h,
                            icon: AppAssets.download_1,
                            iconColor: AppColors.primaryRedColor,
                            iconSize: 16.h,
                          ).toShimmer2(isShow: isLoading),
                        ),
                        SizedBox(width: 16.h),
                        Expanded(
                          child: CustomButton(
                            text: "Download",
                            onPressed: () async {
                              getMedicalReportPDF(false, context, _appState);
                            },
                            backgroundColor: AppColors.primaryRedColor,
                            borderColor: AppColors.primaryRedColor,
                            textColor: AppColors.whiteColor,
                            fontSize: 14,
                            fontWeight: FontWeight.w600,
                            borderRadius: 12.h,
                            height: 40.h,
                            icon: AppAssets.download_1,
                            iconColor: AppColors.whiteColor,
                            iconSize: 16.h,
                          ).toShimmer2(isShow: isLoading),
                        ),
                      ],
                    ),
                  )
                : SizedBox.shrink()
          ],
        ),
      ),
    );
  }

  void getMedicalReportPDF(bool isShare, BuildContext context, AppState _appState) async {
    LoaderBottomSheet.showLoader();
    await medicalFileViewModel.getPatientMedicalReportPDF(patientMedicalReportResponseModel, _appState.getAuthenticatedUser()!).then((val) async {
      LoaderBottomSheet.hideLoader();
      if (medicalFileViewModel.patientMedicalReportPDFBase64.isNotEmpty) {
        String path = await Utils.createFileFromString(medicalFileViewModel.patientMedicalReportPDFBase64, "pdf");
        if (isShare) {
          Share.shareXFiles([XFile(path)], text: "Medical Report");
        } else {
          try {
            OpenFilex.open(path);
          } catch (ex) {
            showCommonBottomSheetWithoutHeight(
              context,
              child: Utils.getErrorWidget(loadingText: "Cannot open file"),
              callBackFunc: () {},
              isFullScreen: false,
              isCloseButtonVisible: true,
            );
          }
        }
      }
    });
  }
}
