import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/enums/work_order_next_step.dart';
import 'package:test_sa/models/helper_data_models/workorder/work_order_helper_models.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/new_models/work_order_detail_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/providers/service_request_providers/reject_reason_provider.dart';
import 'package:test_sa/service_request_latest/service_request_detail_provider.dart';
import 'package:test_sa/service_request_latest/views/components/activities_list_view.dart';
import 'package:test_sa/service_request_latest/views/components/bottom_sheets/service_request_bottomsheet.dart';
import 'package:test_sa/service_request_latest/views/components/verify_arrival_view.dart';
import 'package:test_sa/service_request_latest/views/forms/asset_retired/verify_asset_detail.dart';

class FooterActionButton {
  static Widget footerContainer({required Widget child}) {
    return Align(
      alignment: Alignment.bottomCenter,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.toScreenWidth, vertical: 16.toScreenHeight),
        color: AppColor.white10,
        child: child,
      ),
    );
  }

  static Widget requestDetailsFooterWidget(
      {required WorkOrderNextStepEnum workOrderNextStepStatus,
      List<Activities> activities = const [],
      required BuildContext context,
      Lookup? status,
      required UserProvider userProvider,
      bool isEmpIsAssigned = false}) {
    ServiceRequestDetailProvider requestDetailProvider = Provider.of<ServiceRequestDetailProvider>(context, listen: false);
    bool showMarkAsFixedButton = activities.isEmpty ? false : activities.any((object) => object.activityStatus!.value == 14 || object.activityStatus!.value == 19);
    if (userProvider.user?.type == UsersTypes.engineer) {
      if (workOrderNextStepStatus == WorkOrderNextStepEnum.assignToMe && isEmpIsAssigned) return const SizedBox();
      switch (workOrderNextStepStatus) {
        case WorkOrderNextStepEnum.assignToMe:
          return footerContainer(
              child: AppFilledButton(
            label: 'Assign To Me',
            // maxWidth: true,
            buttonColor: AppColor.primary10,
            onPressed: () async {
              requestDetailProvider.engineerAssignWorkOrderToHimself(id: requestDetailProvider.currentWorkOrder!.data!.requestId.toString());
            },
            fontSize: 12.toScreenWidth,
          ));
        case WorkOrderNextStepEnum.markedAsFixed:
          break;
        // TODO: Handle this case.
        case WorkOrderNextStepEnum.endWorkFlow:
          if (requestDetailProvider.isReadOnlyRequest) {
            return footerContainer(
              child: AppFilledButton(
                label: context.translation.activities,
                buttonColor: AppColor.neutral50,
                onPressed: () async {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const ActivitiesListView()));
                },
              ),
            );
          }
          return footerContainer(
              child: AppFilledButton(
                label: context.translation.close,
                // maxWidth: true,
                buttonColor: AppColor.primary10,
                onPressed: () async {
                  Navigator.pop(context);
                },
              ));
        case WorkOrderNextStepEnum.nTakeAction:
          if (requestDetailProvider.isReadOnlyRequest) {
            return footerContainer(
              child: AppFilledButton(
                label: context.translation.activities,
                buttonColor: AppColor.neutral50,
                onPressed: () async {
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const ActivitiesListView()));
                },
              ),
            );
          }
          return footerContainer(
              child: AppFilledButton(
            label: context.translation.close,
            // maxWidth: true,
            buttonColor: AppColor.primary10,
            onPressed: () async {
              Navigator.pop(context);
            },
          ));
        case WorkOrderNextStepEnum.eRejectAccept:
          return footerContainer(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.min,
            children: [
              AppFilledButton(
                label: context.translation.reject,
                maxWidth: true,
                buttonColor: Colors.white54,
                textColor: AppColor.red30,
                showBorder: true,
                onPressed: () async {
                  //TODO need to discuss getData is not called by default...
                  if (context.mounted) {
                    RejectReasonProvider rejectionReasonProvider = Provider.of<RejectReasonProvider>(context, listen: false);
                    rejectionReasonProvider.reset();
                    //rejectionReasonProvider.getDate();
                  }
                  ServiceRequestBottomSheet.rejectRequestBottomSheet(context: context);
                },
              ).expanded,
              const SizedBox(
                width: 20,
              ),
              AppFilledButton(
                label: context.translation.accept,
                maxWidth: true,
                buttonColor: AppColor.green70,
                onPressed: () async {
                  requestDetailProvider.engineerAcceptWorkOrder(id: requestDetailProvider.currentWorkOrder!.data!.requestId.toString()).whenComplete(() {});
                },
              ).expanded,
            ],
          ));
        case WorkOrderNextStepEnum.eFixRemotelyNeedVisit:
          return footerContainer(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            mainAxisSize: MainAxisSize.min,
            children: [
              AppFilledButton(
                label: context.translation.needAVisit,
                maxWidth: true,
                buttonColor: AppColor.neutral50,
                onPressed: () async {
                  requestDetailProvider.needVisitHelperModel = NeedVisitHelperModel();
                  ServiceRequestBottomSheet.initialVisitBottomSheet(context: context);
                },
              ).expanded,
              const SizedBox(
                width: 20,
              ),
              AppFilledButton(
                label: context.translation.fixedRemotely,
                maxWidth: true,
                buttonColor: AppColor.green70,
                onPressed: () async {
                  requestDetailProvider.fixRemotelyHelperModel = FixRemotelyHelperModel();
                  ServiceRequestBottomSheet.fixRemotelyBottomSheet(context: context);
                },
              ).expanded,
            ],
          ));
        case WorkOrderNextStepEnum.eArrived:
          return footerContainer(
              child: AppFilledButton(
            label: context.translation.iHaveArrived,
            showIcon: true,
            icon: 'arrived_icon'.toSvgAsset(),
            buttonColor: AppColor.green70,
            onPressed: () async {
              Navigator.push(context, MaterialPageRoute(builder: (context) => const VerifyArrivalView()));
            },
          ));
        case WorkOrderNextStepEnum.verifyAssetDetail:
          return footerContainer(
              child: AppFilledButton(
            label: context.translation.updateAssetDetails,
            // maxWidth: true,
            buttonColor: AppColor.primary10,
            onPressed: () async {
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => VerifyAssetDetails(
                            isEdit: false,
                          )));
            },
          ));
        case WorkOrderNextStepEnum.activity:
          return footerContainer(
              child: Column(
            children: [
              AppFilledButton(
                label: context.translation.activities,
                // maxWidth: true,
                buttonColor: AppColor.neutral50,
                onPressed: () async {
                  // ServiceRequestBottomSheet.activityTypeBottomSheet(context: context);
                  Navigator.push(context, MaterialPageRoute(builder: (context) => const ActivitiesListView()));
                },
              ),
              if (showMarkAsFixedButton) ...[
                8.height,
                AppFilledButton(
                  label: context.translation.markAsCompleted,
                  // maxWidth: true,
                  buttonColor: AppColor.green70,
                  onPressed: () {
                    bool isValid = ServiceRequestBottomSheet.validateAssetSituation(Provider.of<ServiceRequestDetailProvider>(context, listen: false));
                    if (isValid) ServiceRequestBottomSheet.feedBackBottomSheet(context: context);
                  },
                ),
              ]
            ],
          ));
        case WorkOrderNextStepEnum.endWorkFlow:
          return const SizedBox();

        case WorkOrderNextStepEnum.assetRetirementManagementApproval:
          return footerContainer(
              child: AppFilledButton(
            label: context.translation.assetRetiredPendingOpManagementApproval,
            buttonColor: AppColor.neutral140,
            textColor: AppColor.neutral150,
            fontSize: 12.toScreenWidth,
          ));
        case WorkOrderNextStepEnum.waitingForRequesterToConfirm:
          return footerContainer(
              child: AppFilledButton(
            label: 'Waiting for requester to verify',
            buttonColor: AppColor.neutral140,
            textColor: AppColor.neutral150,
            fontSize: 12.toScreenWidth,
          ));
        case WorkOrderNextStepEnum.eFixRemotely:
          return footerContainer(
            child: AppFilledButton(
              label: context.translation.fixedRemotely,
              maxWidth: true,
              buttonColor: AppColor.green70,
              onPressed: () async {
                requestDetailProvider.fixRemotelyHelperModel = FixRemotelyHelperModel();
                ServiceRequestBottomSheet.fixRemotelyBottomSheet(context: context);
              },
            ),
          );
        case WorkOrderNextStepEnum.eNeedVisit:
          return footerContainer(
              child: AppFilledButton(
            label: context.translation.needAVisit,
            maxWidth: true,
            buttonColor: AppColor.neutral50,
            onPressed: () async {
              requestDetailProvider.needVisitHelperModel = NeedVisitHelperModel();
              ServiceRequestBottomSheet.initialVisitBottomSheet(context: context);
            },
          ));
      }
    } else {
      if (workOrderNextStepStatus == WorkOrderNextStepEnum.nTakeAction) {
        return footerContainer(
            child: AppFilledButton(
          label: context.translation.takeAction,
          // maxWidth: true,
          buttonColor: AppColor.primary10,
          onPressed: () async {
            ServiceRequestBottomSheet.nurseTakeActionBottomSheet(context: context);
          },
        ));
      }
      if (workOrderNextStepStatus == WorkOrderNextStepEnum.waitingForRequesterToConfirm) {
        return footerContainer(
            child: AppFilledButton(
          label: context.translation.takeAction,
          // maxWidth: true,
          buttonColor: AppColor.primary10,
          onPressed: () async {
            ServiceRequestBottomSheet.nurseVerifyArrivalBottomSheet(context: context);
          },
        ));
      }
    }
    return const SizedBox();
  }
}
