import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/payment/wallets.dart';

import 'package:pweb/models/state/visibility.dart';
import 'package:pweb/widgets/refresh_balance/button.dart';

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


class WalletBalanceRefreshButton extends StatelessWidget {
  final String walletRef;
  final VisibilityState iconOnly;
  final double iconSize = 18;

  const WalletBalanceRefreshButton({
    super.key,
    required this.walletRef,
    this.iconOnly = VisibilityState.visible,
  });

  @override
  Widget build(BuildContext context) {
    final walletsProvider = context.watch<WalletsProvider>();
    final loc = AppLocalizations.of(context)!;
    final isBusy = walletsProvider.isWalletRefreshing(walletRef) || walletsProvider.isLoading;
    final hasTarget = walletsProvider.wallets.any((w) => w.id == walletRef);

    void refresh() {
      final provider = context.read<WalletsProvider>();
      provider.refreshBalance(walletRef);
    }

    return BalanceRefreshButton(
      isBusy: isBusy,
      enabled: hasTarget,
      onPressed: refresh,
      iconOnly: iconOnly,
      label: loc.refreshBalance,
      tooltip: loc.refreshBalance,
      iconSize: iconSize,
    );
  }
}
