import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/device_transfer_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/models/generic_attachment_model.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/modules/asset_delivery_module/pages/attachment_view.dart';
import 'package:test_sa/modules/asset_delivery_module/provider/asset_delivery_provider.dart';
import 'package:test_sa/modules/asset_delivery_module/provider/attachment_type_provider.dart';
import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/new_views/swipe_module/dialoge/acknowledge_work_dialog.dart';
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';

class HelperFunction {
  static void attachmentTap({required BuildContext context, bool viewOnly = false, required AssetDeliveryProvider assetDeliveryProvider, required dynamic deliveryTableItemId}) async {
    Lookup? result;
    result = await showAttachmentTypeBottomSheet(context);
    List<GenericAttachmentModel> list = [];

    if (result != null) {
      showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) => const AppLazyLoading(),
      );
      list = await assetDeliveryProvider.getAttachments(tableItemId: deliveryTableItemId, attachmentType: result.value);
      Navigator.pop(context);
      // if (list.isNotEmpty) {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => AssetDeliveryAttachmentView(
                    tableItemId: deliveryTableItemId,
                    attachmentType: result!.value,
                    viewOnly: viewOnly,
                    attachmentList: list,
                  )));
      // }
    }
  }

  static void cancelRequest({required BuildContext context,  int ?itemId,  int? requestId}) async {
    AssetDeliveryProvider assetDeliveryProvider = Provider.of<AssetDeliveryProvider>(context, listen: false);
    showDialog(
      context: context,
      builder: (BuildContext cxt) => AcknowledgeWorkDialog(
        message: "Are you sure you want to change this request status to cancel?",
        confirmButtonText: 'Ok',
        cancelButtonText: 'Cancel',
        onSave: () async {
          showDialog(context: context, barrierDismissible: false, builder: (context) => const AppLazyLoading());
          await assetDeliveryProvider.changeDeliveryStatusToCancel(itemId: itemId).then((status) async {
            Navigator.pop(context);
            if (status) {
              Navigator.pop(context);
              await assetDeliveryProvider.getAssetDeliveryTableListById(requestId: requestId);
            }
          });
        },
        onDiscard: () {},
      ),
    );
  }

  static Future<Lookup?> showAttachmentTypeBottomSheet(BuildContext context) {
    return showModalBottomSheet<Lookup?>(
      context: context,
      isScrollControlled: true,
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(20)),
      ),
      builder: (_) {
        return SafeArea(
          top: false,
          child: Padding(
            padding: const EdgeInsets.only(
              top: 16,
              left: 16,
              right: 16,
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                SingleItemDropDownMenu<Lookup, AttachmentTypeLookupProvider>(
                  context: context,
                  title: 'Attachment Type'.addTranslation,
                  onSelect: (value) {
                    Navigator.pop(context, value);
                  },
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
