import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/controllers/balance_mask/wallets.dart';
import 'package:pshared/models/ledger/account.dart';
import 'package:pshared/provider/ledger.dart';
import 'package:pshared/models/payment/wallet.dart';

import 'package:pweb/pages/dashboard/buttons/balance/carousel/carousel.dart';
import 'package:pweb/controllers/dashboard/balance/carousel.dart';

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


class BalanceWidget extends StatelessWidget {
  final ValueChanged<Wallet> onTopUp;
  final ValueChanged<LedgerAccount> onLedgerAddFunds;
  final ValueChanged<Wallet> onWalletTap;
  final ValueChanged<LedgerAccount> onLedgerTap;

  const BalanceWidget({
    super.key,
    required this.onTopUp,
    required this.onLedgerAddFunds,
    required this.onWalletTap,
    required this.onLedgerTap,
  });

  @override
  Widget build(BuildContext context) {
    final walletsController = context.watch<WalletsController>();
    final ledgerProvider = context.watch<LedgerAccountsProvider>();
    final carousel = context.watch<BalanceCarouselController>();
    final loc = AppLocalizations.of(context)!;

    final wallets = walletsController.wallets;
    final accounts = ledgerProvider.accounts;
    final isLoading =
        walletsController.isLoading &&
        ledgerProvider.isLoading &&
        wallets.isEmpty &&
        accounts.isEmpty;

    if (isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    final carouselWidget = BalanceCarousel(
      controller: carousel,
      onTopUp: onTopUp,
      onLedgerAddFunds: onLedgerAddFunds,
      onWalletTap: onWalletTap,
      onLedgerTap: onLedgerTap,
    );

    if (wallets.isEmpty && accounts.isEmpty) {
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Center(child: Text(loc.noWalletsAvailable)),
          const SizedBox(height: 12),
          carouselWidget,
        ],
      );
    }

    return carouselWidget;
  }
}
