import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.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/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_payment_page.dart';
import 'package:hmg_patient_app_new/presentation/appointments/widgets/appointment_doctor_card.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';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:provider/provider.dart';

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

  PatientAppointmentHistoryResponseModel patientAppointmentHistoryResponseModel;

  @override
  State<AppointmentDetailsPage> createState() => _AppointmentDetailsPageState();
}

class _AppointmentDetailsPageState extends State<AppointmentDetailsPage> {
  late MyAppointmentsViewModel myAppointmentsViewModel;

  @override
  Widget build(BuildContext context) {
    myAppointmentsViewModel = Provider.of<MyAppointmentsViewModel>(context);
    return Scaffold(
      backgroundColor: AppColors.bgScaffoldColor,
      appBar: AppBar(
        title: "Appointment Details".needTranslation.toText18(),
        backgroundColor: AppColors.bgScaffoldColor,
      ),
      body: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      "Appointment Details".needTranslation.toText24(isBold: true),
                    ],
                  ),
                  SizedBox(height: 24.h),
                  AppointmentDoctorCard(
                    patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel,
                    onAskDoctorTap: () {},
                    onCancelTap: () {},
                    onRescheduleTap: () {},
                  ),
                  SizedBox(height: 16.h),
                  if (!AppointmentType.isArrived(widget.patientAppointmentHistoryResponseModel))
                    Column(
                      children: [
                        Container(
                          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                            color: AppColors.whiteColor,
                            borderRadius: 20.h,
                            hasShadow: false,
                          ),
                          child: Padding(
                            padding: EdgeInsets.all(16.h),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    "Appointment Status".needTranslation.toText16(isBold: true),
                                  ],
                                ),
                                SizedBox(height: 4.h),
                                (!AppointmentType.isConfirmed(widget.patientAppointmentHistoryResponseModel)
                                    ? "Not Confirmed".needTranslation.toText12(color: AppColors.primaryRedColor, fontWeight: FontWeight.w500)
                                    : "Confirmed".needTranslation.toText12(color: AppColors.successColor, fontWeight: FontWeight.w500)),
                                SizedBox(height: 16.h),
                                Stack(
                                  children: [
                                    ClipRRect(
                                      clipBehavior: Clip.hardEdge,
                                      borderRadius: BorderRadius.circular(24),
                                      child: Image.network(
                                        "https://maps.googleapis.com/maps/api/staticmap?center=${widget.patientAppointmentHistoryResponseModel.latitude},${widget.patientAppointmentHistoryResponseModel.longitude}&zoom=14&size=350x165&maptype=roadmap&markers=color:red%7C${widget.patientAppointmentHistoryResponseModel.latitude},${widget.patientAppointmentHistoryResponseModel.longitude}&key=AIzaSyB6TERnxIr0yJ3qG4ULBZbu0sAD4tGqtng",
                                        fit: BoxFit.contain,
                                      ),
                                    ),
                                    Positioned(
                                      bottom: 0,
                                      child: SizedBox(
                                        width: MediaQuery.of(context).size.width * 0.785,
                                        child: CustomButton(
                                          text: "Get Directions".needTranslation,
                                          onPressed: () {
                                            MapsLauncher.launchCoordinates(double.parse(widget.patientAppointmentHistoryResponseModel.latitude!),
                                                double.parse(widget.patientAppointmentHistoryResponseModel.longitude!), widget.patientAppointmentHistoryResponseModel.projectName);
                                          },
                                          backgroundColor: AppColors.textColor.withOpacity(0.8),
                                          borderColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.01),
                                          textColor: AppColors.whiteColor,
                                          fontSize: 14,
                                          fontWeight: FontWeight.w500,
                                          borderRadius: 12.h,
                                          padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                                          height: 40.h,
                                          icon: AppAssets.directions_icon,
                                          iconColor: AppColors.whiteColor,
                                          iconSize: 13.h,
                                        ).paddingAll(12.h),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ),
                        SizedBox(height: 16.h),
                        Container(
                          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                            color: AppColors.whiteColor,
                            borderRadius: 20.h,
                            hasShadow: false,
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.max,
                            children: [
                              Utils.buildSvgWithAssets(icon: AppAssets.prescription_reminder_icon, width: 35.h, height: 35.h),
                              SizedBox(width: 8.h),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  LocaleKeys.setReminder.tr(context: context).toText13(isBold: true),
                                  "Notify me before the appointment".needTranslation.toText11(color: AppColors.textColorLight, weight: FontWeight.w500),
                                ],
                              ),
                              const Spacer(),
                              Switch(
                                activeColor: AppColors.successColor,
                                activeTrackColor: AppColors.successColor.withValues(alpha: .15),
                                value: widget.patientAppointmentHistoryResponseModel.hasReminder!,
                                onChanged: (newValue) {
                                  setState(() {
                                    myAppointmentsViewModel.setAppointmentReminder(newValue, widget.patientAppointmentHistoryResponseModel);
                                  });
                                },
                              ),
                            ],
                          ).paddingSymmetrical(16.h, 16.h),
                        ),
                        SizedBox(height: 16.h),
                      ],
                    ),
                ],
              ).paddingSymmetrical(24.h, 24.h),
            ),
          ),
          Container(
            decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
              color: AppColors.whiteColor,
              borderRadius: 24.h,
              hasShadow: true,
            ),
            child: SizedBox(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  if (widget.patientAppointmentHistoryResponseModel.nextAction == 15 || widget.patientAppointmentHistoryResponseModel.nextAction == 20)
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            "Total amount to pay".needTranslation.toText18(isBold: true),
                            Utils.getPaymentAmountWithSymbol(widget.patientAppointmentHistoryResponseModel.patientShare!.toString().toText16(isBold: true), AppColors.blackColor, 13,
                                isSaudiCurrency: true),
                          ],
                        ),
                        SizedBox(height: 4.h),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            LocaleKeys.upcomingPaymentNow.tr(context: context).toText12(fontWeight: FontWeight.w500, color: AppColors.greyTextColor),
                            "VAT 15%(${widget.patientAppointmentHistoryResponseModel.patientTaxAmount})".needTranslation.toText14(isBold: true, color: AppColors.greyTextColor),
                          ],
                        ),
                        SizedBox(height: 18.h),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            SizedBox(
                              width: 150.h,
                              child: Utils.getPaymentMethods(),
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Utils.getPaymentAmountWithSymbol(widget.patientAppointmentHistoryResponseModel.patientShareWithTax!.toString().toText24(isBold: true), AppColors.blackColor, 17,
                                    isSaudiCurrency: true),
                              ],
                            ),
                          ],
                        )
                      ],
                    ).paddingOnly(left: 16.h, top: 24.h, right: 16.h, bottom: 0.h),
                  CustomButton(
                    text: AppointmentType.getNextActionText(widget.patientAppointmentHistoryResponseModel.nextAction),
                    onPressed: () {
                      myAppointmentsViewModel.setIsPatientAppointmentShareLoading(true);
                      Navigator.of(context).push(
                        FadePage(
                          page: AppointmentPaymentPage(patientAppointmentHistoryResponseModel: widget.patientAppointmentHistoryResponseModel),
                        ),
                      );
                    },
                    backgroundColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction),
                    borderColor: AppointmentType.getNextActionButtonColor(widget.patientAppointmentHistoryResponseModel.nextAction).withOpacity(0.01),
                    textColor: AppColors.whiteColor,
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    borderRadius: 12,
                    padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                    height: 50.h,
                    icon: AppointmentType.getNextActionIcon(widget.patientAppointmentHistoryResponseModel.nextAction),
                    iconColor: AppColors.whiteColor,
                    iconSize: 18.h,
                  ).paddingSymmetrical(16.h, 24.h),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
