import 'package:flutter/material.dart';

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

import 'package:pweb/models/state/load_more_state.dart';
import 'package:pweb/pages/report/cards/items.dart';

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


class OperationsCardsList extends StatelessWidget {
  final List<OperationItem> operations;
  final ValueChanged<OperationItem>? onTap;
  final LoadMoreState loadMoreState;
  final VoidCallback? onLoadMore;

  const OperationsCardsList({
    super.key,
    required this.operations,
    this.onTap,
    this.loadMoreState = LoadMoreState.hidden,
    this.onLoadMore,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final items = buildOperationCardItems(context, operations, onTap: onTap);

    if (operations.isEmpty) {
      return Padding(
        padding: const EdgeInsets.symmetric(vertical: 32),
        child: Center(
          child: Text(
            loc.reportPaymentsEmpty,
            style: Theme.of(context).textTheme.bodyMedium,
          ),
        ),
      );
    }

    final extraItems = loadMoreState == LoadMoreState.hidden ? 0 : 1;
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: items.length + extraItems,
      itemBuilder: (context, index) {
        if (index < items.length) {
          return items[index];
        }
        if (loadMoreState == LoadMoreState.loading) {
          return const Padding(
            padding: EdgeInsets.symmetric(vertical: 16),
            child: Center(child: CircularProgressIndicator()),
          );
        }
        return Padding(
          padding: const EdgeInsets.symmetric(vertical: 16),
          child: Center(
            child: TextButton(onPressed: onLoadMore, child: Text(loc.loadMore)),
          ),
        );
      },
    );
  }
}
