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/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/chip/app_custom_chip_widget.dart';
import 'package:hmg_patient_app_new/widgets/transitions/fade_page.dart';
import 'package:smooth_corner/smooth_corner.dart';

class AppointmentCard extends StatefulWidget {
  AppointmentCard({super.key, required this.patientAppointmentHistoryResponseModel, required this.myAppointmentsViewModel});

  PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;
  MyAppointmentsViewModel myAppointmentsViewModel;

  @override
  State<AppointmentCard> createState() => _AppointmentCardState();
}

class _AppointmentCardState extends State<AppointmentCard> {
  @override
  Widget build(BuildContext context) {
    AppState appState = getIt.get<AppState>();
    return InkWell(
      onTap: () {
        Navigator.of(context)
            .push(
          FadePage(
            page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel),
          ),
        )
            .then((val) {
          widget.myAppointmentsViewModel.initAppointmentsViewModel();
          widget.myAppointmentsViewModel.getPatientAppointments(true, false);
        });
      },
      child: Padding(
        padding: EdgeInsets.all(14.h),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: Wrap(
                    alignment: WrapAlignment.start,
                    direction: Axis.horizontal,
                    spacing: 6.h,
                    runSpacing: 6.h,
                    children: [
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          CustomButton(
                            text: appState.isArabic()
                                ? widget.patientAppointmentHistoryResponseModel.isInOutPatientDescriptionN!
                                : widget.patientAppointmentHistoryResponseModel.isInOutPatientDescription!,
                            onPressed: () {},
                            backgroundColor: AppColors.primaryRedColor.withOpacity(0.1),
                            borderColor: AppColors.primaryRedColor.withOpacity(0.0),
                            textColor: AppColors.primaryRedColor,
                            fontSize: 10,
                            fontWeight: FontWeight.w500,
                            borderRadius: 8,
                            padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            height: 30.h,
                          ),
                        ],
                      ),
                      Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          CustomButton(
                            text: AppointmentType.getAppointmentStatusType(widget.patientAppointmentHistoryResponseModel.patientStatusType!),
                            onPressed: () {},
                            backgroundColor: AppColors.successColor.withOpacity(0.1),
                            borderColor: AppColors.successColor.withOpacity(0.0),
                            textColor: AppColors.successColor,
                            fontSize: 10,
                            fontWeight: FontWeight.w500,
                            borderRadius: 8,
                            padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            height: 30.h,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                // TODO: Implement the logic to enable/disable the switch based on reminder status
                AppointmentType.isArrived(widget.patientAppointmentHistoryResponseModel)
                    ? SizedBox()
                    : Switch(
                        activeColor: AppColors.successColor,
                        activeTrackColor: AppColors.successColor.withValues(alpha: .15),
                        thumbIcon: WidgetStateProperty.resolveWith<Icon?>(
                          (Set<WidgetState> states) {
                            if (states.contains(WidgetState.selected)) {
                              return const Icon(Icons.check); // Icon when switch is ON
                            }
                            return const Icon(Icons.close); // Icon when switch is OFF
                          },
                        ),
                        value: widget.patientAppointmentHistoryResponseModel.hasReminder!,
                        onChanged: (newValue) {
                          setState(() {
                            widget.myAppointmentsViewModel.setAppointmentReminder(newValue, widget.patientAppointmentHistoryResponseModel);
                          });
                        },
                      ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Image.network(
                  widget.patientAppointmentHistoryResponseModel.doctorImageURL!,
                  width: 63.h,
                  height: 63.h,
                  fit: BoxFit.fill,
                ).circle(100),
                SizedBox(width: 16.h),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      widget.patientAppointmentHistoryResponseModel.doctorNameObj!.toText16(isBold: true),
                      Wrap(
                        direction: Axis.horizontal,
                        spacing: 3.h,
                        runSpacing: 4.h,
                        children: [
                          AppCustomChipWidget(labelText: widget.patientAppointmentHistoryResponseModel.clinicName!),
                          AppCustomChipWidget(labelText: widget.patientAppointmentHistoryResponseModel.projectName!),
                          AppCustomChipWidget(
                              icon: AppAssets.appointment_calendar_icon,
                              labelText: DateUtil.formatDateToDate(DateUtil.convertStringToDate(widget.patientAppointmentHistoryResponseModel.appointmentDate), false)),
                          AppCustomChipWidget(
                              icon: AppAssets.appointment_time_icon,
                              labelText: DateUtil.formatDateToTimeLang(DateUtil.convertStringToDate(widget.patientAppointmentHistoryResponseModel.appointmentDate), false)),
                        ],
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                Expanded(
                  flex: 6,
                  child: AppointmentType.isArrived(widget.patientAppointmentHistoryResponseModel)
                      ? getArrivedAppointmentButton()
                      : CustomButton(
                          text: AppointmentType.getNextActionText(widget.patientAppointmentHistoryResponseModel.nextAction),
                          onPressed: () {
                            Navigator.of(context)
                                .push(FadePage(
                              page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel),
                            ))
                                .then((val) {
                              widget.myAppointmentsViewModel.initAppointmentsViewModel();
                              widget.myAppointmentsViewModel.getPatientAppointments(true, false);
                            });
                          },
                          backgroundColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.15),
                          borderColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.01),
                          textColor: AppointmentType.getNextActionTextColor(widget.patientAppointmentHistoryResponseModel.nextAction),
                          fontSize: 14,
                          fontWeight: FontWeight.w500,
                          borderRadius: 12,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 40.h,
                          icon: AppointmentType.getNextActionIcon(widget.patientAppointmentHistoryResponseModel.nextAction),
                          iconColor: AppointmentType.getNextActionTextColor(widget.patientAppointmentHistoryResponseModel.nextAction),
                          iconSize: 14.h,
                        ),
                ),
                SizedBox(width: 8.h),
                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: Utils.buildSvgWithAssets(
                        icon: AppAssets.forward_arrow_icon,
                        width: 10.h,
                        height: 10.h,
                        fit: BoxFit.contain,
                      ),
                    ),
                  ).onPress(() {
                    Navigator.of(context)
                        .push(
                      FadePage(
                        page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel),
                      ),
                    )
                        .then((val) {
                      widget.myAppointmentsViewModel.initAppointmentsViewModel();
                      widget.myAppointmentsViewModel.getPatientAppointments(true, false);
                    });
                  }),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget getArrivedAppointmentButton() {
    return DateTime.now().difference(DateUtil.convertStringToDate(widget.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 with same doctor".needTranslation,
            onPressed: () {},
            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,
          );
  }

  void performAppointmentNextAction(nextAction) {}
}
