import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/recipient/methods_cache.dart';

import 'package:pweb/pages/address_book/page/recipient/info_row.dart';
import 'package:pweb/utils/payment/label.dart';


class RecipientPaymentRow extends StatelessWidget {
  final Recipient recipient;
  final double spacing;

  const RecipientPaymentRow({
    super.key, 
    required this.recipient, 
    this.spacing = 18
  });

  @override
  Widget build(BuildContext context) {
    final cacheProvider = context.watch<RecipientMethodsCacheProvider>();
    final recipientId = recipient.id;
    final isLoading = cacheProvider.isLoadingFor(recipientId);

    if (isLoading && !cacheProvider.hasMethodsFor(recipientId)) {
      return const Center(child: CircularProgressIndicator());
    }

    return Align(
      alignment: Alignment.centerLeft,
      child: Wrap(
        alignment: WrapAlignment.start,
        runAlignment: WrapAlignment.start,
        crossAxisAlignment: WrapCrossAlignment.start,
        spacing: spacing,
        runSpacing: spacing,
        children: cacheProvider
            .methodsForRecipient(recipientId)
            .map((m) => RecipientAddressBookInfoRow(
                  type: m.type,
                  value: getPaymentTypeDescription(context, m),
                ))
            .toList(),
      ),
    );
  }
}
