import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/provider/ledger.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 LedgerBalanceRefreshButton extends StatelessWidget {
  final String ledgerAccountRef;
  final VisibilityState iconOnly;
  final double iconSize = 18;

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

  @override
  Widget build(BuildContext context) {
    final ledgerProvider = context.watch<LedgerAccountsProvider>();
    final loc = AppLocalizations.of(context)!;
    final isBusy = ledgerProvider.isWalletRefreshing(ledgerAccountRef) || ledgerProvider.isLoading;
    final hasTarget = ledgerProvider.accounts.any((a) => a.ledgerAccountRef == ledgerAccountRef);

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

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