import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/routes.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/advertisment_models/ad_details_model.dart';
import 'package:mc_common_app/models/advertisment_models/special_service_model.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/dialogs_and_bottomsheets.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/utils/navigator.dart';
import 'package:mc_common_app/utils/utils.dart';
import 'package:mc_common_app/view_models/ad_view_model.dart';
import 'package:mc_common_app/view_models/payment_view_model.dart';
import 'package:mc_common_app/views/advertisement/ad_duration_selection_sheet_content.dart';
import 'package:mc_common_app/views/advertisement/ads_images_slider.dart';
import 'package:mc_common_app/views/advertisement/picked_images_container.dart';
import 'package:mc_common_app/views/appointments/widgets/custom_calender_widget.dart';
import 'package:mc_common_app/widgets/bottom_sheet.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/common_widgets/info_bottom_sheet.dart';
import 'package:mc_common_app/widgets/common_widgets/time_slots.dart';
import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:mc_common_app/widgets/txt_field.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';

class AdsDetailView extends StatefulWidget {
  final AdDetailsModel adDetails;

  const AdsDetailView({Key? key, required this.adDetails}) : super(key: key);

  @override
  State<AdsDetailView> createState() => _AdsDetailViewState();
}

class _AdsDetailViewState extends State<AdsDetailView> {
  late AdVM adVM;

  @override
  void initState() {
    adVM = context.read<AdVM>();
    scheduleMicrotask(() {
      onAdDetailsLoaded();
    });
    super.initState();
  }

  Future<void> onAdDetailsLoaded() async {
    context.read<PaymentVM>().updateCurrentAdId(id: widget.adDetails.id!);
    if ((widget.adDetails.isMyAd ?? false) && (widget.adDetails.adPostStatus == AdPostStatus.reserved)) {
      await adVM.getAdBankingAccountInfo(adId: widget.adDetails.id!);
    }
  }

  void deleteAdBottomSheet(BuildContext context) {
    return actionConfirmationBottomSheet(
      context: context,
      title: LocaleKeys.deleteAdConfirmation.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
      subtitle: LocaleKeys.deleteAdConfirmationMessage.tr(),
      actionButtonYes: Expanded(
        child: ShowFillButton(
          maxHeight: 55,
          title: LocaleKeys.yes.tr(),
          fontSize: 15,
          onPressed: () {
            Navigator.pop(context);
            adVM.deleteMyAd(context, adId: widget.adDetails.id!);
          },
        ),
      ),
      actionButtonNo: Expanded(
        child: ShowFillButton(
          maxHeight: 55,
          isFilled: false,
          borderColor: MyColors.darkPrimaryColor,
          title: LocaleKeys.no.tr(),
          txtColor: MyColors.darkPrimaryColor,
          fontSize: 15,
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: LocaleKeys.ads.tr(),
        profileImageUrl: MyAssets.bnCar,
        isRemoveBackButton: false,
        isDrawerEnabled: false,
        actions: [
          ((widget.adDetails.isMyAd ?? false) && (widget.adDetails.adPostStatus != AdPostStatus.reserved) && (widget.adDetails.adPostStatus != AdPostStatus.active)
                  ? IconButton(
                      icon: const Icon(Icons.delete_outline, color: MyColors.redColor),
                      onPressed: () {
                        return deleteAdBottomSheet(context);
                      },
                    )
                  : IconButton(icon: const Icon(Icons.chat_outlined, color: Colors.black), onPressed: () => adVM.onMessagesButtonPressed(context: context, adDetailsModel: widget.adDetails)))
              .toContainer(
            margin: const EdgeInsets.fromLTRB(0, 8, 21, 8),
            paddingAll: 0,
            padding: const EdgeInsets.only(right: 21),
            borderRadius: 100,
            borderColor: MyColors.lightGreyEFColor,
            isEnabledBorder: true,
            height: 40,
            width: 42,
          )
        ],
        onTap: () {},
      ),
      body: Container(
        padding: const EdgeInsets.only(bottom: 10, left: 21, right: 21),
        child: Column(
          children: [
            Expanded(
              child: ListView(
                shrinkWrap: true,
                children: [
                  Column(
                    mainAxisSize: MainAxisSize.min,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      CarouselWithIndicatorDemo(vehicleImages: widget.adDetails.vehicle!.image!),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Flexible(
                            child: "${widget.adDetails.vehicle!.vehicleTitle} | ${widget.adDetails.vehicle!.color!.label} | ${widget.adDetails.id}".toText(
                              fontSize: 18,
                              isBold: true,
                              maxLines: 2,
                            ),
                          ),
                          (widget.adDetails.vehicle!.cityName ?? "").toText(fontSize: 10, isBold: true, color: MyColors.lightTextColor),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              ("${LocaleKeys.model.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                              "${widget.adDetails.vehicle!.modelyear!.label}".toText(
                                fontSize: 14,
                                isBold: true,
                              ),
                            ],
                          ),
                          "${widget.adDetails.vehicle!.countryID}".toText(fontSize: 10, isBold: true, color: MyColors.lightTextColor),
                        ],
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              ("${LocaleKeys.mileage.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                              "${widget.adDetails.vehicle!.mileage!.mileageEnd}Km".toText(
                                fontSize: 14,
                                isBold: true,
                              ),
                            ],
                          ),
                          widget.adDetails.createdOn != null
                              ? DateTime.parse(widget.adDetails.createdOn!).getTimeAgo().toText(fontSize: 10, isBold: true, color: MyColors.lightTextColor)
                              : const SizedBox(),
                        ],
                      ),
                      Row(
                        children: [
                          ("${LocaleKeys.transmission.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                          "${widget.adDetails.vehicle!.transmission!.label}".toText(
                            fontSize: 14,
                            isBold: true,
                          ),
                        ],
                      ),
                      8.height,
                      ("${LocaleKeys.description.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      "${widget.adDetails.vehicle!.vehicleDescription}".toText(
                        fontSize: 14,
                        isBold: true,
                      ),
                      if (widget.adDetails.isMyAd ?? false) ...[
                        8.height,
                        ("${LocaleKeys.demand.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: [
                            widget.adDetails.vehicle!.demandAmount!.toInt().toString().toText(fontSize: 30, height: 1.2, isBold: true),
                            LocaleKeys.sar.tr().toText(fontSize: 15, isBold: true, color: MyColors.lightTextColor).paddingOnly(bottom: 5),
                          ],
                        ),
                        if (widget.adDetails.adPostStatus == AdPostStatus.expired) ...[
                          8.height,
                          const Divider(thickness: 1, height: 1),
                          8.height,
                          LocaleKeys.adDurationExpired.tr().toText(
                                color: MyColors.redColor,
                                fontSize: 12,
                                isItalic: true,
                              ),
                        ],
                      ]
                    ],
                  ).toWhiteContainer(width: double.infinity, allPading: 12),
                  12.height,
                  Consumer(builder: (BuildContext context, AdVM adVM, Widget? child) {
                    if (adVM.adsBankDetailsModel != null) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          LocaleKeys.bankDetails.tr().toText(fontSize: 18, isBold: true),
                          // Row(
                          //   children: [
                          //     "Full Name: ".toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                          //     "${widget.adDetails.vehicle!.vehicleDescription}".toText(
                          //       fontSize: 14,
                          //       isBold: true,
                          //     ),
                          //   ],
                          // ),
                          Row(
                            children: [
                              ("${LocaleKeys.bankName.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                              (adVM.adsBankDetailsModel!.bankName ?? "").toText(
                                fontSize: 14,
                                isBold: true,
                              ),
                            ],
                          ),
                          Row(
                            children: [
                              ("${LocaleKeys.iban.tr()}: ").toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                              (adVM.adsBankDetailsModel!.iban ?? "").toText(
                                fontSize: 14,
                                isBold: true,
                              ),
                            ],
                          ),
                        ],
                      ).toWhiteContainer(width: double.infinity, allPading: 12);
                    }
                    return const SizedBox.shrink();
                  }),
                  12.height,
                  if (widget.adDetails.adPostStatus == AdPostStatus.rejected)
                    Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            LocaleKeys.rejectionComments.tr().toText(fontSize: 13, isBold: true, color: MyColors.lightTextColor),
                            Row(
                              children: [
                                LocaleKeys.editAd.tr().toText(fontSize: 10, isBold: true),
                                2.width,
                                const Icon(Icons.edit, size: 15),
                              ],
                            ).onPress(() async {
                              Utils.showLoading(context);
                              await adVM.getVehicleTypes();
                              await adVM.getVehicleBrandsByVehicleTypeId(vehicleIdForEditAd: widget.adDetails.vehicle!.vehicleType ?? -1);
                              Utils.hideLoading(context);
                              adVM.onEditUpdateAdPressed(context, widget.adDetails);
                            }),
                          ],
                        ).paddingOnly(bottom: 5),
                        Row(
                          children: [
                            Flexible(
                              child: (widget.adDetails.comment ?? "").toText(
                                color: MyColors.redColor,
                                fontSize: 12,
                                isItalic: true,
                              ),
                            )
                          ],
                        ),
                      ],
                    ).toWhiteContainer(width: double.infinity, allPading: 12),
                ],
              ),
            ),
            SizedBox(
              child: Column(
                children: [
                  if (!(widget.adDetails.isMyAd ?? false)) ...[
                    const Divider(thickness: 1, height: 1),
                    18.height,
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        widget.adDetails.vehicle!.demandAmount!.toInt().toString().toText(fontSize: 30, isBold: true),
                        LocaleKeys.sar.tr().toText(fontSize: 15, isBold: true, color: MyColors.lightTextColor).paddingOnly(bottom: 5),
                      ],
                    ),
                    14.height,
                  ],
                  widget.adDetails.isMyAd ?? false ? BuildAdDetailsActionButtonForMyAds(adDetailsModel: widget.adDetails) : BuildAdDetailsActionButtonForExploreAds(adDetailsModel: widget.adDetails),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class BuildAdDetailsActionButtonForExploreAds extends StatelessWidget {
  final AdDetailsModel adDetailsModel;

  const BuildAdDetailsActionButtonForExploreAds({Key? key, required this.adDetailsModel}) : super(key: key);

  void reserveAdPriceBreakDownClicked(BuildContext context, AdDetailsModel adDetailsModel) {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        enableDrag: true,
        builder: (BuildContext context) {
          return InfoBottomSheet(
              title: LocaleKeys.reserveAd.tr().toText(fontSize: 24, isBold: true),
              description: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.reservationAmount.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "${adDetailsModel.reservePrice}".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  const Divider(),
                  LocaleKeys.belowAmountPayLater.tr().toText(fontSize: 12),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      LocaleKeys.carPrice.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "${adDetailsModel.vehicle!.demandAmount ?? 0.0}".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      LocaleKeys.vatExcluded.tr().toText(fontSize: 10, isBold: true),
                    ],
                  ),
                  const Divider(),
                  LocaleKeys.specialService.toText(fontSize: 16, isBold: true),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.carInsuranceService.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor, fontWeight: FontWeight.w500),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.registrationCarPlates.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.homeDeliveryService.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  12.height,
                  LocaleKeys.specialServicechargesInsuranceDeliveryLocation.tr().toText(fontSize: 12, maxLines: 2),
                  30.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.totalAmount.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "${(adDetailsModel.vehicle!.demandAmount ?? 0.0)}".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      LocaleKeys.estimated.tr().toText(fontSize: 10, isBold: true),
                    ],
                  ),
                  30.height,
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      const Icon(
                        Icons.warning,
                        color: MyColors.adPendingStatusColor,
                        size: 19,
                      ).paddingOnly(bottom: 2),
                      3.width,
                      LocaleKeys.servicesReservingAd.tr().toText(
                            color: MyColors.adPendingStatusColor,
                            fontSize: 12,
                            isItalic: true,
                          ),
                    ],
                  ),
                  15.height,
                  Row(
                    children: [
                      Expanded(
                        child: ShowFillButton(
                          maxHeight: 55,
                          title: LocaleKeys.completeReservation.tr(),
                          onPressed: () async {
                            // Navigator.pop(context);
                            bool status = await context.read<AdVM>().createReserveAd(adId: adDetailsModel.id!, context: context);
                            if (status) {
                              navigateReplaceWithName(context, AppRoutes.paymentMethodsView, arguments: PaymentTypes.adReserve);
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                  19.height,
                ],
              ));
        });
  }

  Widget reserveAdAction(BuildContext context, AdDetailsModel adDetailsModel) {
    return Row(
      children: [
        Expanded(
          child: ShowFillButton(
            maxHeight: 55,
            title: LocaleKeys.reserveAd.tr(),
            onPressed: () {
              reserveAdPriceBreakDownClicked(context, adDetailsModel);
              // navigateWithName(context, AppRoutes.paymentMethodsView);
            },
          ),
        ),
        if (adDetailsModel.whatsAppNo != null && adDetailsModel.whatsAppNo!.isNotEmpty) ...[
          8.width,
          Container(
              height: 55,
              width: 55,
              alignment: Alignment.center,
              decoration: BoxDecoration(border: Border.all(color: MyColors.black, width: 2)),
              child: MyAssets.whatsAppIcon.buildSvg(
                height: 35,
                width: 35,
              )).onPress(() {
            Utils.openNumberViaWhatsApp(phoneNumber: adDetailsModel.whatsAppNo ?? "");
          }),
        ],
        if (adDetailsModel.phoneNo != null && adDetailsModel.phoneNo!.isNotEmpty) ...[
          8.width,
          Container(
            height: 55,
            width: 55,
            alignment: Alignment.center,
            decoration: BoxDecoration(border: Border.all(color: MyColors.black, width: 2)),
            child: const Icon(Icons.phone, color: MyColors.black),
          ).onPress(() {
            Utils.openNumberViaCaller(phoneNumber: adDetailsModel.phoneNo ?? "");
          }),
        ]
      ],
    );
  }

  Widget defaultActionForProviderAndCustomer(BuildContext context, AdDetailsModel adDetailsModel) {
    return (adDetailsModel.phoneNo == null)
        ? Row(
            children: [
              Expanded(
                child: ShowFillButton(
                  maxHeight: 55,
                  title: LocaleKeys.contact.tr(),
                  fontSize: 18,
                  isBold: false,
                  iconWidget: const Padding(
                    padding: EdgeInsets.only(right: 10),
                    child: Icon(Icons.phone, color: MyColors.white, size: 24),
                  ),
                  onPressed: () {
                    Utils.openNumberViaCaller(phoneNumber: adDetailsModel.phoneNo ?? "");
                  },
                ),
              ),
              if (adDetailsModel.whatsAppNo == null) ...[
                8.width,
                Container(
                        height: 55,
                        width: 55,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(border: Border.all(color: MyColors.black, width: 2)),
                        child: MyAssets.whatsAppIcon.buildSvg(height: 33, width: 35))
                    .onPress(() {
                  Utils.openNumberViaWhatsApp(phoneNumber: adDetailsModel.whatsAppNo ?? "");
                }),
              ],
            ],
          )
        : const SizedBox.shrink();
  }

  @override
  Widget build(BuildContext context) {
    switch (adDetailsModel.createdByRoleEnum!) {
      case CreatedByRoleEnum.customer:
      case CreatedByRoleEnum.provider:
        return defaultActionForProviderAndCustomer(context, adDetailsModel);
      case CreatedByRoleEnum.admin:
        return reserveAdAction(context, adDetailsModel);
      case CreatedByRoleEnum.allAds:
        return const SizedBox.shrink();
    }
  }
}

class BuildAdDetailsActionButtonForMyAds extends StatelessWidget {
  final AdDetailsModel adDetailsModel;

  const BuildAdDetailsActionButtonForMyAds({Key? key, required this.adDetailsModel}) : super(key: key);

  void onBookPhotographyServiceClicked(BuildContext context, {required AdDetailsModel adDetailsModel}) async {
    AdVM adVM = context.read<AdVM>();
    if (adVM.photoOfficeSelectedId.selectedId == -1) {
      adVM.getPhotographyServiceScheduleListByOffices(latitude: 46.703430, longitude: 24.625720, isNeedToRebuild: true); // TODO: These Lat Long need to be dynamic
    }

    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      enableDrag: true,
      builder: (BuildContext context) {
        return Consumer(builder: (BuildContext context, AdVM adVM, Widget? child) {
          return InfoBottomSheet(
              title: LocaleKeys.setDateandTime.tr().toText(fontSize: 16, isBold: true, letterSpacing: -1.44, height: 1.2),
              description: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  25.height,
                  adVM.state == ViewState.busy
                      ? const Center(child: CircularProgressIndicator())
                      : Builder(
                          builder: (context) {
                            List<DropValue> vehicleCitiesDrop = [];
                            for (int i = 0; i < adVM.photoSSSchedulesByOffices.length; i++) {
                              var element = adVM.photoSSSchedulesByOffices[i];
                              vehicleCitiesDrop.add(DropValue(element.photoOfficeID?.toInt() ?? 0, element.photoOfficeName ?? "", i.toString()));
                            }

                            return DropdownField(
                              (DropValue value) => adVM.updatePhotoOfficeSelectedId(SelectionModel(selectedId: value.id, selectedOption: value.value, itemPrice: value.subValue)),
                              // here the item price is the index of the selected option
                              list: vehicleCitiesDrop,
                              dropdownValue: adVM.photoOfficeSelectedId.selectedId != -1 ? DropValue(adVM.photoOfficeSelectedId.selectedId, adVM.photoOfficeSelectedId.selectedOption, "") : null,
                              hint: LocaleKeys.selectOffice.tr(),
                              errorValue: adVM.photoOfficeSelectedId.errorValue,
                            );
                          },
                        ),
                  if (adVM.photoOfficeSelectedId.selectedId != -1) ...[
                    9.height,
                    CustomCalenderAppointmentWidget(
                      customTimeDateSlotList: adVM.selectedPhotoSSSchedulesByOffice.customTimeDateSlotList ?? [],
                      onDateSelected: (dateIndex) => adVM.updateSelectedPhotoOfficeAppointmentDate(dateIndex: dateIndex),
                      selectedCustomTimeDateSlotModel: adVM.selectedPhotoSSSchedulesByOffice.selectedCustomTimeDateSlotModel,
                    ),
                    if (adVM.selectedPhotoSSSchedulesByOffice.selectedCustomTimeDateSlotModel != null && adVM.selectedPhotoSSSchedulesByOffice.selectedCustomTimeDateSlotModel!.date != null) ...[
                      5.height,
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          LocaleKeys.availableSlots.tr().toText(fontSize: 14, isBold: true),
                        ],
                      ),
                      5.height,
                      SizedBox(
                        width: double.infinity,
                        child: BuildTimeSlots(
                          timeSlots: adVM.selectedPhotoSSSchedulesByOffice.customTimeDateSlotList![adVM.selectedPhotoSSSchedulesByOffice.selectedDateIndex!].availableSlots ?? [],
                          onPressed: (slotIndex) => adVM.updateSelectedAppointmentSlotByDate(slotIndex: slotIndex),
                        ),
                      ),
                      20.height,
                    ],
                    5.height,
                    Row(
                      children: [
                        Expanded(
                          child: ShowFillButton(
                            maxHeight: 55,
                            title: LocaleKeys.bookAndPay.tr(),
                            fontSize: 15,
                            onPressed: () {
                              adVM.onAdSSBookAppointmentPressed(context, adDetailsModel: adDetailsModel, adsSpecialServiceID: 1); //1 for photography Service
                            },
                          ),
                        ),
                      ],
                    ),
                  ],
                  19.height,
                ],
              ));
        });
      },
    );
  }

  void reserveAdPriceBreakDownClicked(BuildContext context) {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        enableDrag: true,
        builder: (BuildContext context) {
          return InfoBottomSheet(
              title: LocaleKeys.reserveAd.tr().toText(fontSize: 24, isBold: true),
              description: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.reservationAmounts.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "500".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  const Divider(),
                  LocaleKeys.belowAmountPayLater.tr().toText(fontSize: 12),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      LocaleKeys.carPrice.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "30,000".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.tax.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "4,500".toText(fontSize: 16, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 0),
                        ],
                      )
                    ],
                  ),
                  const Divider(),
                  LocaleKeys.specialService.tr().toText(fontSize: 16, isBold: true),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.carInsuranceService.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor, fontWeight: FontWeight.w500),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.registrationCarPlates.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  5.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.homeDeliveryService.tr().toText(fontSize: 14, isBold: true, color: MyColors.lightTextColor),
                      LocaleKeys.toBeDecided.tr().toText(fontSize: 12, isBold: true),
                    ],
                  ),
                  12.height,
                  LocaleKeys.specialServicechargesInsuranceDeliveryLocation.tr().toText(fontSize: 12),
                  30.height,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      LocaleKeys.totalAmount.tr().toText(fontSize: 16, isBold: true),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          "34,500".toText(fontSize: 19, isBold: true),
                          2.width,
                          LocaleKeys.sar.tr().toText(color: MyColors.lightTextColor, fontSize: 10, isBold: true).paddingOnly(bottom: 3),
                        ],
                      )
                    ],
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      LocaleKeys.estimated.tr().toText(fontSize: 10, isBold: true),
                    ],
                  ),
                  44.height,
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: [
                      const Icon(
                        Icons.warning,
                        color: MyColors.adPendingStatusColor,
                        size: 19,
                      ).paddingOnly(bottom: 2),
                      3.width,
                      LocaleKeys.servicesReservingAd.tr().toText(
                            color: MyColors.adPendingStatusColor,
                            fontSize: 12,
                            isItalic: true,
                          ),
                    ],
                  ),
                  15.height,
                  Row(
                    children: [
                      Expanded(
                        child: ShowFillButton(
                          maxHeight: 55,
                          title: LocaleKeys.completeReservation.tr(),
                          onPressed: () {
                            Navigator.pop(context);
                            navigateWithName(context, AppRoutes.paymentMethodsView);
                          },
                        ),
                      ),
                    ],
                  ),
                  19.height,
                ],
              ));
        });
  }

  Widget pendingForReviewAction({required String pendingText}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            Expanded(
              child: ShowFillButton(
                backgroundColor: MyColors.grey98Color.withOpacity(0.3),
                txtColor: MyColors.lightTextColor,
                maxHeight: 55,
                title: pendingText,
                isBold: false,
                onPressed: () {},
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget pendingForPaymentAction(BuildContext context, {required AdDetailsModel ad}) {
    SpecialServiceModelForAds? photoSpecialServiceModel;
    for (var element in ad.specialservice!) {
      if (element.specialServiceID == 1) {
        photoSpecialServiceModel = element;
      }
    }
    bool payButtonStatus = photoSpecialServiceModel != null && photoSpecialServiceModel.appointmentStatusId == 0;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        if (photoSpecialServiceModel != null && photoSpecialServiceModel.appointmentStatusId == 0) ...[
          Row(
            children: [
              Expanded(
                child: ShowFillButton(
                  isFilled: false,
                  borderColor: MyColors.darkPrimaryColor,
                  maxHeight: 55,
                  title: ("${LocaleKeys.book.tr()} ${photoSpecialServiceModel.name}"),
                  txtColor: MyColors.darkPrimaryColor,
                  onPressed: () {
                    onBookPhotographyServiceClicked(context, adDetailsModel: adDetailsModel);
                  },
                ),
              ),
            ],
          ),
          8.height,
        ],
        Row(
          children: [
            Expanded(
              child: ShowFillButton(
                maxHeight: 55,
                backgroundColor: payButtonStatus ? MyColors.grey98Color.withOpacity(0.3) : MyColors.darkPrimaryColor,
                txtColor: payButtonStatus ? MyColors.lightTextColor : MyColors.white,
                isBold: false,
                title: LocaleKeys.payNow.tr(),
                onPressed: () {
                  if (photoSpecialServiceModel == null) {
                    navigateWithName(context, AppRoutes.paymentMethodsView, arguments: PaymentTypes.ads);
                  }
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget markAsSoldAction(BuildContext context) {
    AdVM adVM = context.read<AdVM>();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Row(
          children: [
            Expanded(
              child: ShowFillButton(
                maxHeight: 55,
                title: LocaleKeys.markAsSold.tr(),
                isBold: false,
                onPressed: () {
                  adVM.markAdAsSold(context, adId: adDetailsModel.id!);
                },
              ),
            ),
          ],
        ),
        8.height,
        Row(
          children: [
            Expanded(
              child: ShowFillButton(
                isFilled: false,
                borderColor: MyColors.redColor,
                maxHeight: 55,
                title: LocaleKeys.deactivateAd.tr(),
                txtColor: MyColors.redColor,
                onPressed: () {
                  return actionConfirmationBottomSheet(
                    context: context,
                    title: LocaleKeys.doWantDeactivateAd.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
                    subtitle: LocaleKeys.stoptheBuyers.tr(),
                    actionButtonYes: Expanded(
                      child: ShowFillButton(
                        maxHeight: 55,
                        title: LocaleKeys.yes.tr(),
                        fontSize: 15,
                        onPressed: () {
                          Navigator.pop(context);
                          adVM.deactivateTheAd(context, adId: adDetailsModel.id!);
                        },
                      ),
                    ),
                    actionButtonNo: Expanded(
                      child: ShowFillButton(
                        maxHeight: 55,
                        isFilled: false,
                        borderColor: MyColors.darkPrimaryColor,
                        title: LocaleKeys.no.tr(),
                        txtColor: MyColors.darkPrimaryColor,
                        fontSize: 15,
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  Future buildCancelReservationBottomSheet(BuildContext context, {required AdDetailsModel adDetails}) {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      enableDrag: true,
      builder: (BuildContext context) {
        return InfoBottomSheet(
            title: LocaleKeys.cancelReservation.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
            description: Padding(
              padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
              child: Consumer<AdVM>(
                builder: (BuildContext context, AdVM adVM, Widget? child) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          12.height,
                          TxtField(
                            maxLines: 5,
                            value: adVM.reservationCancelReason,
                            errorValue: adVM.reservationCancelError,
                            keyboardType: TextInputType.text,
                            hint: LocaleKeys.reasonForCancellation.tr(),
                            onChanged: (v) => adVM.updateReservationCancelReason(v),
                          ),
                        ],
                      ),
                      25.height,
                      ShowFillButton(
                        title: LocaleKeys.submit.tr(),
                        onPressed: () {
                          bool status = adVM.validateReservationCancelReason();
                          if (status) {
                            Navigator.pop(context);
                            return actionConfirmationBottomSheet(
                              context: context,
                              title: LocaleKeys.doWantCancelReservation.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
                              subtitle: LocaleKeys.yourAdVisibletoBuy.tr(),
                              actionButtonYes: Expanded(
                                child: ShowFillButton(
                                  maxHeight: 55,
                                  title: LocaleKeys.yes.tr(),
                                  fontSize: 15,
                                  onPressed: () {
                                    Navigator.pop(context);
                                    adVM.cancelMyAdReservation(context, adId: adDetails.id!, reason: "");
                                  },
                                ),
                              ),
                              actionButtonNo: Expanded(
                                child: ShowFillButton(
                                  maxHeight: 55,
                                  isFilled: false,
                                  borderColor: MyColors.darkPrimaryColor,
                                  title: LocaleKeys.no.tr(),
                                  txtColor: MyColors.darkPrimaryColor,
                                  fontSize: 15,
                                  onPressed: () {
                                    Navigator.pop(context);
                                  },
                                ),
                              ),
                            );
                          }
                        },
                        maxWidth: double.infinity,
                      ),
                      19.height,
                    ],
                  );
                },
              ),
            ));
      },
    );
  }

  Widget cancelReservationAction(BuildContext context, {required AdDetailsModel adDetails}) {
    return Row(
      children: [
        Expanded(
          child: ShowFillButton(
              borderColor: MyColors.redColor,
              txtColor: MyColors.redColor,
              isFilled: false,
              fontSize: 16,
              maxHeight: 55,
              title: LocaleKeys.cancelReservation.tr(),
              onPressed: () {
                buildCancelReservationBottomSheet(context, adDetails: adDetails);
              }),
        ),
      ],
    );
  }

  Widget completeDealAction(BuildContext context, {required AdDetailsModel adDetails}) {
    return Row(
      children: [
        Expanded(
          child: ShowFillButton(
              maxHeight: 55,
              backgroundColor: MyColors.darkPrimaryColor,
              txtColor: MyColors.white,
              isBold: false,
              title: LocaleKeys.completeDeal.tr(),
              onPressed: () {
                buildCompleteDealBottomSheet(context, adDetails: adDetails);
              }),
        ),
      ],
    );
  }

  Future buildCompleteDealBottomSheet(BuildContext context, {required AdDetailsModel adDetails}) {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      enableDrag: true,
      builder: (BuildContext context) {
        return InfoBottomSheet(
            title: LocaleKeys.uploadBankReceipt.tr().toText(fontSize: 26, isBold: true, letterSpacing: -1.44),
            description: Padding(
              padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
              child: Consumer<AdVM>(
                builder: (BuildContext context, AdVM adVM, Widget? child) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          12.height,
                          TxtField(
                            maxLines: 4,
                            value: adVM.completeDealNotesForAdmin,
                            errorValue: "",
                            keyboardType: TextInputType.text,
                            hint: LocaleKeys.notesForAdmin.tr(),
                            onChanged: (v) => adVM.updateCompleteDealNotesForAdmin(v),
                          ),
                        ],
                      ),
                      15.height,
                      LocaleKeys.attachFile.tr().toText(fontSize: 20, isBold: true, letterSpacing: -0.5),
                      if (adVM.pickedReceiptPdfFiles.isNotEmpty) ...[
                        16.height,
                        PickedFilesContainer(
                          pickedFiles: adVM.pickedReceiptPdfFiles,
                          onCrossPressedPrimary: adVM.removePdfFileFromList,
                          onAddFilePressed: () {
                            context.read<AdVM>().pickPdfReceiptFile(context);
                          },
                          isPdf: true,
                        ),
                      ] else ...[
                        Row(
                          children: [
                            Container(
                              height: 90,
                              width: 90,
                              decoration: BoxDecoration(color: MyColors.greyButtonColor, border: Border.all(width: 2, color: MyColors.greyAddBorderColor)),
                              margin: const EdgeInsets.all(8),
                              alignment: Alignment.center,
                              child: Container(
                                height: 24,
                                width: 24,
                                decoration: const BoxDecoration(shape: BoxShape.circle, color: MyColors.darkTextColor),
                                child: const Icon(Icons.add, color: MyColors.white),
                              ),
                            ).onPress(() {
                              context.read<AdVM>().pickPdfReceiptFile(context);
                            }),
                          ],
                        ),
                      ],
                      15.height,
                      ShowFillButton(
                        title: LocaleKeys.submit.tr(),
                        onPressed: () {
                          //Upload Attachment
                        },
                        maxWidth: double.infinity,
                      ),
                      19.height,
                    ],
                  );
                },
              ),
            ));
      },
    );
  }

  Widget expiredAdAction(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: ShowFillButton(
            fontSize: 16,
            maxHeight: 55,
            title: LocaleKeys.extendAd.tr(),
            onPressed: () {
              final AdVM adVM = context.read<AdVM>();
              return actionConfirmationBottomSheet(
                context: context,
                title: LocaleKeys.updateAdDetails.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
                subtitle: LocaleKeys.durationExtendingAd.tr(),
                actionButtonYes: Expanded(
                  child: ShowFillButton(
                    maxHeight: 55,
                    title: LocaleKeys.yes.tr(),
                    fontSize: 15,
                    onPressed: () {
                      Navigator.pop(context);
                      adVM.updateSelectionVehicleTypeId(
                        SelectionModel(selectedId: adDetailsModel.vehicle!.vehicleType!, selectedOption: (adDetailsModel.vehicle!.vehicleType!).toVehicleTypeString(), errorValue: ""),
                      );
                      showMyBottomSheet(context, child: const AdDurationSelectionSheetContent(isFromExtendAd: true, isUpdateAdSelected: true));
                    },
                  ),
                ),
                actionButtonNo: Expanded(
                  child: ShowFillButton(
                    maxHeight: 55,
                    isFilled: false,
                    borderColor: MyColors.darkPrimaryColor,
                    title: LocaleKeys.no.tr(),
                    txtColor: MyColors.darkPrimaryColor,
                    fontSize: 15,
                    onPressed: () {
                      adVM.updateSelectionVehicleTypeId(
                        SelectionModel(selectedId: adDetailsModel.vehicle!.vehicleType!, selectedOption: (adDetailsModel.vehicle!.vehicleType!).toVehicleTypeString(), errorValue: ""),
                      );

                      showMyBottomSheet(context, child: const AdDurationSelectionSheetContent(isFromExtendAd: true, isUpdateAdSelected: false));
                    },
                  ),
                ),
              );
            },
          ),
        ),
      ],
    );
  }

  Widget reservedAdActions(context, {required AdDetailsModel adDetailsModel, required AdVM adVM}) {
    switch (adDetailsModel.adReserveStatus) {
      case AdReserveStatus.defaultStatus:
        return pendingForReviewAction(pendingText: LocaleKeys.waitingAdminsResponse.tr());

      case AdReserveStatus.reserved:
        return cancelReservationAction(context, adDetails: adDetailsModel);

      case AdReserveStatus.cancelledByOwner:
        return pendingForReviewAction(pendingText: LocaleKeys.cancelledByOwner.tr());

      case AdReserveStatus.cancelledByAdmin:
        return pendingForReviewAction(pendingText: LocaleKeys.cancelledByAdmin.tr());

      case AdReserveStatus.timeOver:
        return pendingForReviewAction(pendingText: LocaleKeys.reservationTimeOver.tr());

      case AdReserveStatus.dealDone:
        if (adVM.adsBankDetailsModel != null) {
          return completeDealAction(context, adDetails: adDetailsModel);
        }
        return pendingForReviewAction(pendingText: LocaleKeys.waitingAdminsResponse.tr());

      case AdReserveStatus.fullPaymentVerified:
        return pendingForReviewAction(pendingText: LocaleKeys.paymentVerified.tr());

      default:
        return pendingForReviewAction(pendingText: LocaleKeys.waitingAdminsResponse.tr());
    }
  }

  @override
  Widget build(BuildContext context) {
    switch (adDetailsModel.adPostStatus!) {
      case AdPostStatus.pendingForPayment:
        return pendingForPaymentAction(context, ad: adDetailsModel);
      case AdPostStatus.active:
        return markAsSoldAction(context);
      case AdPostStatus.reserved:
        AdVM adVM = context.watch<AdVM>();
        if (adVM.state == ViewState.busy) {
          return const CircularProgressIndicator();
        } else {
          return reservedAdActions(context, adDetailsModel: adDetailsModel, adVM: adVM);
        }
      case AdPostStatus.buyingService:
      case AdPostStatus.reserveCancel:
      case AdPostStatus.rejected:
        return pendingForReviewAction(pendingText: LocaleKeys.rejectedFormAdmin.tr());
      case AdPostStatus.cancelled:
      case AdPostStatus.pendingForPost:
        return pendingForReviewAction(pendingText: LocaleKeys.waitingAdminPost.tr());

      case AdPostStatus.pendingForReview:
        return pendingForReviewAction(pendingText: LocaleKeys.waitingAdminsApproval.tr());

      case AdPostStatus.sold:
        return pendingForReviewAction(pendingText: LocaleKeys.sold.tr());

      case AdPostStatus.expired:
        return expiredAdAction(context);
      case AdPostStatus.allAds:
        break;
    }
    return const SizedBox.shrink();
  }
}
