import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

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


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

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

  @override
  State<RecipientPaymentRow> createState() => _RecipientPaymentRowState();
}

class _RecipientPaymentRowState extends State<RecipientPaymentRow> {
  late final PaymentMethodsProvider _methodsProvider;

  @override
  void initState() {
    super.initState();
    _methodsProvider = PaymentMethodsProvider()
      ..addListener(_onProviderChanged)
      ..loadMethods(
        context.read<OrganizationsProvider>(), 
        widget.recipient.id,
      );
  }

  @override
  void dispose() {
    _methodsProvider.removeListener(_onProviderChanged);
    _methodsProvider.dispose();
    super.dispose();
  }

  void _onProviderChanged() => setState(() {});

  @override
  Widget build(BuildContext context) {
    if (!_methodsProvider.isReady) return const Center(child: CircularProgressIndicator());

    return Row(
      spacing: widget.spacing,
      children: _methodsProvider.methods.map((m) => RecipientAddressBookInfoRow(
        type: m.type,
        value: getPaymentTypeDescription(context, m),
      )).toList(),
    );
  }
}
