import 'dart:developer';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/config/routes.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/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/view_models/chat_view_model.dart';
import 'package:mc_common_app/view_models/dashboard_view_model_customer.dart';
import 'package:mc_common_app/view_models/requests_view_model.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:provider/provider.dart';

void dealCompletedConsentBottomSheet({required BuildContext mainContext, required RequestStatusEnum requestStatusEnum, required int requestId, Function()? acceptRequestOffer}) {
  final requestVM = mainContext.read<RequestsVM>();
  return actionConfirmationBottomSheet(
    isOnlyOneButton: AppState().currentAppType == AppType.customer,
    context: mainContext,
    title: LocaleKeys.doYouWantToCompleteThisDeal.tr().toText(fontSize: 26, isBold: true, letterSpacing: -1.44),
    subtitle: AppState().currentAppType == AppType.provider ? LocaleKeys.providerCompletingDealMeansThat.tr() : LocaleKeys.customerCompletingDealMeansThat.tr(),
    checkBoxConfirmationWidget: AppState().currentAppType == AppType.customer
        ? Consumer(builder: (BuildContext context, ChatVM chatVM, Widget? child) {
            return Row(
              children: [
                Expanded(
                  child: CircleCheckBoxWithTitle(
                    isChecked: chatVM.acknowledgePaymentToMowaterStatus,
                    title: LocaleKeys.acknowledgePaymentToMowater.tr(),
                    onSelected: () {
                      chatVM.updateAcknowledgePaymentToMowaterStatus(!chatVM.acknowledgePaymentToMowaterStatus);
                    },
                    selectedColor: MyColors.darkPrimaryColor,
                  ),
                ),
              ],
            );
          })
        : null,
    actionButtonYes: Consumer(builder: (BuildContext context, ChatVM chatVM, Widget? child) {
      return Expanded(
        child: ShowFillButton(
          maxHeight: 55,
          isDisabled: !chatVM.acknowledgePaymentToMowaterStatus,
          title: AppState().currentAppType == AppType.customer ? LocaleKeys.submit.tr() : LocaleKeys.yes.tr(),
          fontSize: 15,
          onPressed: () async {
            pop(context);
            bool statusForReqAccept = false;

            if (acceptRequestOffer != null) {
              statusForReqAccept = await acceptRequestOffer();
              if (statusForReqAccept) {
                bool status = await requestVM.onActionRequestTapped(
                  context: mainContext,
                  requestStatusEnum: requestStatusEnum,
                  requestId: requestId,
                  needLoading: false,
                );
                log("status: $status");
                if (status) {
                  int index = requestVM.myFilteredRequests.indexWhere((element) => element.id == requestId);
                  if (index != -1) {
                    requestVM.myFilteredRequests[index].requestStatus = requestStatusEnum;
                  }
                  chatVM.updateAcknowledgePaymentToMowaterStatus(false);
                  mainContext.read<DashboardVmCustomer>().onNavbarTapped(4);
                  navigateReplaceWithNameUntilRoute(mainContext, AppRoutes.dashboard);
                }
              }
            } else {
              bool status = await requestVM.onActionRequestTapped(
                context: mainContext,
                requestStatusEnum: requestStatusEnum,
                requestId: requestId,
                needLoading: false,
              );
              log("status: $status");
              if (status) {
                int index = requestVM.myFilteredRequests.indexWhere((element) => element.id == requestId);
                if (index != -1) {
                  requestVM.myFilteredRequests[index].requestStatus = requestStatusEnum;
                }
                chatVM.updateAcknowledgePaymentToMowaterStatus(false);
                mainContext.read<DashboardVmCustomer>().onNavbarTapped(4);
                navigateReplaceWithNameUntilRoute(mainContext, AppRoutes.dashboard);
              }
            }
          },
        ),
      );
    }),
    actionButtonNo: Expanded(
      child: ShowFillButton(
        maxHeight: 55,
        title: LocaleKeys.no.tr(),
        fontSize: 15,
        onPressed: () => Navigator.pop(mainContext),
      ),
    ),
  );
}
