import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/organizations.dart';
import 'package:pshared/service/payment/documents.dart';

import 'package:pweb/utils/download.dart';
import 'package:pweb/utils/error/snackbar.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';


Future<void> downloadPaymentAct(
  BuildContext context, {
  required String gatewayService,
  required String operationRef,
  required String paymentRef,
}) async {
  final organizations = context.read<OrganizationsProvider>();
  if (!organizations.isOrganizationSet) {
    return;
  }
  final gateway = gatewayService.trim();
  final operation = operationRef.trim();
  final payment = paymentRef.trim();
  if (gateway.isEmpty || operation.isEmpty || payment.isEmpty) {
    return;
  }

  final loc = AppLocalizations.of(context)!;
  await executeActionWithNotification(
    context: context,
    action: () async {
      final file = await PaymentDocumentsService.getOperationDocument(
        organizations.current.id,
        gateway,
        operation,
        payment,
      );
      await downloadFile(file);
    },
    errorMessage: loc.downloadActError,
  );
}
