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/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/utils/appointment_type.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.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';

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

  PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;
  late Function onRescheduleTap;
  late Function onCancelTap;
  late Function onAskDoctorTap;

  @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: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Image.network(
                  patientAppointmentHistoryResponseModel.doctorImageURL!,
                  width: 63.h,
                  height: 63.h,
                  fit: BoxFit.cover,
                ).circle(100),
                SizedBox(width: 16.h),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      patientAppointmentHistoryResponseModel.doctorNameObj!.toText16(isBold: true),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 3.h,
                        runSpacing: 4.h,
                        children: [
                          AppCustomChipWidget(labelText: patientAppointmentHistoryResponseModel.clinicName!),
                          AppCustomChipWidget(labelText: patientAppointmentHistoryResponseModel.projectName!),
                          AppCustomChipWidget(
                              icon: AppAssets.doctor_calendar_icon,
                              labelText:
                                  "${DateUtil.formatDateToDate(DateUtil.convertStringToDate(patientAppointmentHistoryResponseModel.appointmentDate), false)}, ${DateUtil.formatDateToTimeLang(DateUtil.convertStringToDate(patientAppointmentHistoryResponseModel.appointmentDate), false)}"),
                          AppCustomChipWidget(
                            icon: !patientAppointmentHistoryResponseModel.isLiveCareAppointment! ? AppAssets.walkin_appointment_icon : AppAssets.small_livecare_icon,
                            iconColor: !patientAppointmentHistoryResponseModel.isLiveCareAppointment! ? AppColors.textColor : AppColors.whiteColor,
                            labelText: patientAppointmentHistoryResponseModel.isLiveCareAppointment! ? LocaleKeys.livecare.tr(context: context) : "Walk In".needTranslation,
                            backgroundColor: !patientAppointmentHistoryResponseModel.isLiveCareAppointment! ? AppColors.greyColor : AppColors.successColor,
                            textColor: !patientAppointmentHistoryResponseModel.isLiveCareAppointment! ? AppColors.textColor : AppColors.whiteColor,
                          ),
                          AppCustomChipWidget(icon: AppAssets.rating_icon, iconColor: AppColors.ratingColorYellow, labelText: "Rating: ${patientAppointmentHistoryResponseModel.decimalDoctorRate}"),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            getAppointmentActionButtons(
              AppointmentType.isArrived(patientAppointmentHistoryResponseModel),
            ),
          ],
        ),
      ),
    );
  }

  Widget getAppointmentActionButtons(bool isAppointmentArrived) {
    if (isAppointmentArrived) {
      return DateTime.now().difference(DateUtil.convertStringToDate(patientAppointmentHistoryResponseModel.appointmentDate)).inDays <= 15
          ? CustomButton(
              text: LocaleKeys.askDoctor.tr(),
              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,
              iconSize: 16.h,
              icon: AppAssets.ask_doctor_icon,
              iconColor: AppColors.primaryRedColor,
            )
          : CustomButton(
              text: "Rebook with same doctor".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,
            );
    } else {
      return patientAppointmentHistoryResponseModel.isLiveCareAppointment ?? false
          ? CustomButton(
              text: LocaleKeys.cancel.tr(),
              onPressed: () {
                onCancelTap();
              },
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              textColor: AppColors.whiteColor,
              fontSize: 14,
              fontWeight: FontWeight.w500,
              borderRadius: 12.h,
              height: 40.h,
              icon: AppAssets.cancel,
              iconColor: AppColors.whiteColor,
              iconSize: 16.h,
            )
          : Row(
              children: [
                Expanded(
                  child: CustomButton(
                    text: LocaleKeys.reschedule.tr(),
                    onPressed: () {
                      onRescheduleTap();
                    },
                    backgroundColor: AppColors.secondaryLightRedColor,
                    borderColor: AppColors.secondaryLightRedColor,
                    textColor: AppColors.primaryRedColor,
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    borderRadius: 12.h,
                    height: 40.h,
                    icon: AppAssets.appointment_calendar_icon,
                    iconColor: AppColors.primaryRedColor,
                    iconSize: 16.h,
                  ),
                ),
                SizedBox(width: 16.h),
                Expanded(
                  child: CustomButton(
                    text: LocaleKeys.cancel.tr(),
                    onPressed: () {
                      onCancelTap();
                    },
                    backgroundColor: AppColors.primaryRedColor,
                    borderColor: AppColors.primaryRedColor,
                    textColor: AppColors.whiteColor,
                    fontSize: 14,
                    fontWeight: FontWeight.w500,
                    borderRadius: 12.h,
                    height: 40.h,
                    icon: AppAssets.cancel,
                    iconColor: AppColors.whiteColor,
                    iconSize: 16.h,
                  ),
                ),
              ],
            );
    }
  }
}
