import 'package:flutter/material.dart';

import 'package:pshared/models/payment/execution_operation.dart';
import 'package:pshared/models/payment/payment.dart';

import 'package:pweb/pages/report/details/section.dart';
import 'package:pweb/pages/report/details/sections/operations/tile.dart';

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


class PaymentOperationsSection extends StatelessWidget {
  final Payment payment;
  final bool Function(PaymentExecutionOperation operation)? canDownloadDocument;
  final ValueChanged<PaymentExecutionOperation>? onDownloadDocument;

  const PaymentOperationsSection({
    super.key,
    required this.payment,
    this.canDownloadDocument,
    this.onDownloadDocument,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final operations = payment.operations;
    if (operations.isEmpty) {
      return const SizedBox.shrink();
    }

    final children = <Widget>[];
    for (var i = 0; i < operations.length; i++) {
      final operation = operations[i];
      final canDownload = canDownloadDocument?.call(operation) ?? false;
      children.add(
        OperationHistoryTile(
          operation: operation,
          canDownloadDocument: canDownload,
          onDownloadDocument: canDownload && onDownloadDocument != null
              ? () => onDownloadDocument!(operation)
              : null,
        ),
      );
      if (i < operations.length - 1) {
        children.addAll([
          const SizedBox(height: 10),
          Divider(
            height: 1,
            color: Theme.of(context).dividerColor.withAlpha(20),
          ),
          const SizedBox(height: 10),
        ]);
      }
    }

    return DetailsSection(title: loc.operationfryTitle, children: children);
  }
}
