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/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/my_appointments/models/resp_models/patient_appointment_history_response_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/utils/appointment_type.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/appointments/appointment_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/transitions/fade_page.dart';

class MedicalFileAppointmentCard extends StatelessWidget {
  MedicalFileAppointmentCard({super.key, required this.patientAppointmentHistoryResponseModel, required this.myAppointmentsViewModel, required this.onRescheduleTap, required this.onAskDoctorTap});

  PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;
  MyAppointmentsViewModel myAppointmentsViewModel;

  late Function onRescheduleTap;
  late Function onAskDoctorTap;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        CustomButton(
          text: DateUtil.formatDateToDate(DateUtil.convertStringToDate(patientAppointmentHistoryResponseModel.appointmentDate), false),
          onPressed: () {},
          backgroundColor: AppointmentType.isArrived(patientAppointmentHistoryResponseModel) ? AppColors.greyColor : AppColors.secondaryLightRedColor,
          borderColor: AppointmentType.isArrived(patientAppointmentHistoryResponseModel) ? AppColors.greyLightColor : AppColors.secondaryLightRedColor,
          textColor: AppointmentType.isArrived(patientAppointmentHistoryResponseModel) ? AppColors.textColor : AppColors.primaryRedColor,
          fontSize: 12,
          fontWeight: FontWeight.w500,
          borderRadius: 12.h,
          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
          height: 40.h,
          icon: AppointmentType.isArrived(patientAppointmentHistoryResponseModel) ? AppAssets.appointment_calendar_icon : AppAssets.alarm_clock_icon,
          iconColor: AppointmentType.isArrived(patientAppointmentHistoryResponseModel) ? AppColors.textColor : AppColors.primaryRedColor,
          iconSize: 16.h,
        ).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading),
        SizedBox(height: 16.h),
        Container(
          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.h, hasShadow: true),
          width: 200.h,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Image.network(
                    patientAppointmentHistoryResponseModel.doctorImageURL ?? "https://hmgwebservices.com/Images/MobileImages/DUBAI/unkown_female.png",
                    width: 25.h,
                    height: 27.h,
                    fit: BoxFit.fill,
                  ).circle(100).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading),
                  SizedBox(width: 8.h),
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        (patientAppointmentHistoryResponseModel.doctorNameObj ?? "").toText14(isBold: true, maxlines: 1).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading),
                        (patientAppointmentHistoryResponseModel.clinicName ?? "")
                            .toText12(maxLine: 1, fontWeight: FontWeight.w500, color: AppColors.greyTextColor)
                            .toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading),
                      ],
                    ),
                  ),
                ],
              ),
              SizedBox(height: 12.h),
              Row(
                children: [
                  myAppointmentsViewModel.isMyAppointmentsLoading
                      ? Container().toShimmer2(isShow: true, height: 40.h, width: 100.h, radius: 12.h)
                      : Expanded(
                          flex: 6,
                          child: AppointmentType.isArrived(patientAppointmentHistoryResponseModel)
                              ? getArrivedAppointmentButton(context).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading)
                              : CustomButton(
                                  text: AppointmentType.getNextActionText(patientAppointmentHistoryResponseModel.nextAction),
                                  onPressed: () {
                                    Navigator.of(context)
                                        .push(FadePage(
                                      page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: patientAppointmentHistoryResponseModel),
                                    ))
                                        .then((val) {
                                      // widget.myAppointmentsViewModel.initAppointmentsViewModel();
                                      // widget.myAppointmentsViewModel.getPatientAppointments(true, false);
                                    });
                                  },
                                  backgroundColor: AppointmentType.getNextActionButtonColor(patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.15),
                                  borderColor: AppointmentType.getNextActionButtonColor(patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.01),
                                  textColor: AppointmentType.getNextActionTextColor(patientAppointmentHistoryResponseModel.nextAction),
                                  fontSize: 14,
                                  fontWeight: FontWeight.w500,
                                  borderRadius: 12,
                                  padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                  height: 40.h,
                                  icon: AppointmentType.getNextActionIcon(patientAppointmentHistoryResponseModel.nextAction),
                                  iconColor: AppointmentType.getNextActionTextColor(patientAppointmentHistoryResponseModel.nextAction),
                                  iconSize: 14.h,
                                ).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading),
                        ),
                  SizedBox(width: 8.h),
                  Expanded(
                    flex: 2,
                    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: Utils.buildSvgWithAssets(
                          icon: AppAssets.forward_arrow_icon,
                          width: 10.h,
                          height: 10.h,
                          fit: BoxFit.contain,
                        ),
                      ),
                    ).toShimmer2(isShow: myAppointmentsViewModel.isMyAppointmentsLoading).onPress(() {
                      Navigator.of(context)
                          .push(
                        FadePage(
                          page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: patientAppointmentHistoryResponseModel),
                        ),
                      )
                          .then((val) {
                        // widget.myAppointmentsViewModel.initAppointmentsViewModel();
                        // widget.myAppointmentsViewModel.getPatientAppointments(true, false);
                      });
                    }),
                  ),
                ],
              ),
            ],
          ).paddingAll(16.h),
        ),
      ],
    );
  }

  Widget getArrivedAppointmentButton(BuildContext context) {
    return DateTime.now().difference(DateUtil.convertStringToDate(patientAppointmentHistoryResponseModel.appointmentDate)).inDays <= 15
        ? CustomButton(
            text: LocaleKeys.askDoctor.tr(context: context),
            onPressed: () {},
            backgroundColor: AppColors.secondaryLightRedColor,
            borderColor: AppColors.secondaryLightRedColor,
            textColor: AppColors.primaryRedColor,
            fontSize: 14,
            fontWeight: FontWeight.w500,
            borderRadius: 12,
            padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
            height: 40.h,
            icon: AppAssets.ask_doctor_icon,
            iconColor: AppColors.primaryRedColor,
            iconSize: 16.h,
          )
        : CustomButton(
            text: "Rebook".needTranslation,
            onPressed: () {
              onRescheduleTap();
            },
            backgroundColor: AppColors.greyColor,
            borderColor: AppColors.greyColor,
            textColor: AppColors.blackColor,
            fontSize: 14,
            fontWeight: FontWeight.w500,
            borderRadius: 12,
            padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
            height: 40.h,
            icon: AppAssets.rebook_appointment_icon,
            iconColor: AppColors.blackColor,
            iconSize: 16.h,
          );
  }
}
