import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/app_state.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/chat_models/chat_message_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/views/advertisement/bottom_sheets/ad_duration_selection_sheet.dart';
import 'package:mc_common_app/views/advertisement/components/picked_images_container_widget.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/checkbox_with_title_desc.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 BuildAdDetailsActionButtonForExploreAds extends StatelessWidget {
  final AdDetailsModel adDetailsModel;

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

  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 && adDetailsModel.phoneNo!.isNotEmpty)
        ? 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 && 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: 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({super.key, required this.adDetailsModel});

  void onBookPhotographyServiceClicked(BuildContext context, {required AdDetailsModel adDetailsModel}) async {
    AdVM adVM = context.read<AdVM>();
    if (adVM.photoOfficeSelectedId.selectedId == -1) {
      adVM.getPhotographyServiceScheduleListByOffices(latitude: AppState().currentLocation.latitude, longitude: AppState().currentLocation.longitude, isNeedToRebuild: true);
    }

    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, height: 1.2),
              description: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  20.height,
                  adVM.state == ViewState.busy
                      ? const Center(child: CircularProgressIndicator())
                      : adVM.photoSSSchedulesByOffices.isEmpty
                          ? LocaleKeys.noAvailableOfficesInCity.tr().toText(fontSize: 14, height: 1.2)
                          : 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)),
                                  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
                            },
                          ),
                        ),
                      ],
                    ),
                  ],
                  30.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}) {
    bool isForShippingOrDelivery = true;
    return Center(
      child: pendingText.toText(
        color: isForShippingOrDelivery ? MyColors.lightTextColor : MyColors.adPendingStatusColor,
        fontSize: isForShippingOrDelivery ? 15 : 16,
        // isItalic: true,
      ),
    ).toContainer(
      marginAll: 5,
      paddingAll: 12,
      borderRadius: 8,
      width: double.infinity,
      backgroundColor: isForShippingOrDelivery ? MyColors.grey98Color.withOpacity(0.1) : MyColors.adPendingStatusColor.withOpacity(0.16),
    );
  }

  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: () {
                  return actionConfirmationBottomSheet(
                    context: context,
                    title: LocaleKeys.markAsSold.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
                    subtitle: LocaleKeys.markAdAsSoldDesc.tr(),
                    actionButtonYes: Expanded(
                      child: ShowFillButton(
                        maxHeight: 55,
                        title: LocaleKeys.yes.tr(),
                        fontSize: 15,
                        onPressed: () {
                          adVM.markAdAsSold(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);
                        },
                      ),
                    ),
                  );
                },
              ),
            ),
          ],
        ),
        8.height,
        Row(
          children: [
            Expanded(
              child: ShowFillButton(
                isFilled: false,
                borderColor: MyColors.redColor,
                maxHeight: 55,
                title: LocaleKeys.deactivateAd.tr(),
                txtColor: MyColors.redColor,
                onPressed: () {
                  buildAdDeactivateReasonsBottomSheet(context, adDetails: adDetailsModel);
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  Future buildAdDeactivateReasonsBottomSheet(BuildContext context, {required AdDetailsModel adDetails}) {
    return showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      enableDrag: true,
      builder: (BuildContext context) {
        return Consumer(builder: (BuildContext context, AdVM adVM, Widget? child) {
          return InfoBottomSheet(
              title: LocaleKeys.pleaseSpecify.tr().toText(fontSize: 28, isBold: true, letterSpacing: -1.44),
              description: Padding(
                padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        12.height,
                        ListView.separated(
                          shrinkWrap: true,
                          itemCount: adVM.deActivateAdModelList.length,
                          separatorBuilder: (BuildContext context, int index) {
                            return const Divider(thickness: 0.5);
                          },
                          itemBuilder: (BuildContext context, int index) {
                            OfferRequestCommentModel offerRequestCommentModel = adVM.deActivateAdModelList[index];
                            return CircleCheckBoxWithTitle(
                              isChecked: offerRequestCommentModel.isSelected ?? false,
                              title: '${offerRequestCommentModel.title}',
                              onSelected: () {
                                adVM.updateSelectionInDeActivateAdModelList(index);
                              },
                              selectedColor: MyColors.darkPrimaryColor,
                            );
                          },
                        ),
                        if (adVM.selectedDeActivateAdCommentModel.index == adVM.deActivateAdModelList.length - 1) ...[
                          // comparing if the "other" is selected
                          12.height,
                          TxtField(
                            maxLines: 5,
                            value: adVM.deactivateAdReasonDescription,
                            errorValue: adVM.deactivateAdReasonDescriptionError,
                            keyboardType: TextInputType.text,
                            hint: LocaleKeys.reason.tr(),
                            onChanged: (v) => adVM.updateDeactivateAdReasonDescription(v),
                          ),
                        ],
                      ],
                    ),
                    25.height,
                    ShowFillButton(
                      title: LocaleKeys.submit.tr(),
                      onPressed: () {
                        String comments = "";
                        if (adVM.selectedDeActivateAdCommentModel.index == adVM.deActivateAdModelList.length - 1) //Other
                        {
                          comments = adVM.deactivateAdReasonDescription;
                        } else {
                          comments = adVM.selectedDeActivateAdCommentModel.title ?? "";
                        }

                        if (!adVM.isDeActivateReasonSubmitValidated()) {
                          return;
                        }

                        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: () {
                                adVM.deactivateTheAd(context, adId: adDetailsModel.id!, comment: comments);
                              },
                            ),
                          ),
                          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,
                  ],
                ),
              ));
        });
      },
    );
  }

  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.reactivateAd.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: () async {
                      Utils.showLoading(context);
                      await adVM.getVehicleTypes();
                      await adVM.getVehicleBrandsByVehicleTypeId(vehicleIdForEditAd: adDetailsModel.vehicle!.vehicleType ?? -1);
                      Utils.hideLoading(context);
                      adVM.onEditUpdateAdPressed(context: context, previousDetails: adDetailsModel, isFromExtendAd: 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: AdDurationSelectionSheet(isFromExtendAd: true, isUpdateAdSelected: false, adsID: adDetailsModel.id ?? -1));
                    },
                  ),
                ),
              );
            },
          ),
        ),
      ],
    );
  }

  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:
        return pendingForReviewAction(pendingText: LocaleKeys.cancelledByOwner.tr());
      case AdPostStatus.pendingForPost:
      case AdPostStatus.pendingForActive:
        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.deActive:
        return pendingForReviewAction(pendingText: LocaleKeys.deactivateAd.tr());

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