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/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 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),
          ),
        );
      },
      child: Padding(
        padding: EdgeInsets.all(16.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
                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),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    widget.patientAppointmentHistoryResponseModel.doctorNameObj!.toText16(isBold: true),
                    SizedBox(height: 8.h),
                    Row(
                      children: [
                        CustomButton(
                          text: widget.patientAppointmentHistoryResponseModel.clinicName!,
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.w500,
                          borderRadius: 8,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                        SizedBox(width: 6.h),
                        CustomButton(
                          text: widget.patientAppointmentHistoryResponseModel.projectName!,
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.w500,
                          borderRadius: 8,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                      ],
                    ),
                    SizedBox(height: 6.h),
                    Row(
                      children: [
                        CustomButton(
                          icon: AppAssets.appointment_calendar_icon,
                          iconColor: AppColors.blackColor,
                          iconSize: 12.h,
                          text: DateUtil.formatDateToDate(DateUtil.convertStringToDate(widget.patientAppointmentHistoryResponseModel.appointmentDate), false),
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.w500,
                          borderRadius: 8,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                        SizedBox(width: 6.h),
                        CustomButton(
                          icon: AppAssets.appointment_time_icon,
                          iconColor: AppColors.blackColor,
                          iconSize: 12.h,
                          text: DateUtil.formatDateToTimeLang(DateUtil.convertStringToDate(widget.patientAppointmentHistoryResponseModel.appointmentDate), false),
                          onPressed: () {},
                          backgroundColor: AppColors.greyColor,
                          borderColor: AppColors.greyColor,
                          textColor: AppColors.blackColor,
                          fontSize: 10,
                          fontWeight: FontWeight.w500,
                          borderRadius: 8,
                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          height: 30.h,
                        ),
                      ],
                    )
                  ],
                ),
              ],
            ),
            SizedBox(height: 16.h),
            Row(
              children: [
                Expanded(
                  flex: 6,
                  child: CustomButton(
                    text: AppointmentType.getNextActionText(widget.patientAppointmentHistoryResponseModel.nextAction),
                    onPressed: () {
                      Navigator.of(context).push(
                        FadePage(
                          page: AppointmentDetailsPage(patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel),
                        ),
                      );
                    },
                    backgroundColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.1),
                    borderColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.01),
                    textColor: AppointmentType.getNextActionButtonColor(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.getNextActionButtonColor(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),
                      ),
                    );
                  }),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void performAppointmentNextAction(nextAction) {}
}
