import 'package:flutter/material.dart';

import 'package:pshared/models/ledger/account.dart';
import 'package:pshared/models/payment/wallet.dart';

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


class BalanceCarousel extends StatelessWidget {
  final BalanceCarouselController controller;
  final ValueChanged<Wallet> onTopUp;
  final ValueChanged<LedgerAccount> onLedgerAddFunds;
  final ValueChanged<Wallet> onWalletTap;
  final ValueChanged<LedgerAccount> onLedgerTap;

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

  @override
  Widget build(BuildContext context) {
    if (controller.items.isEmpty) {
      return const SizedBox.shrink();
    }

    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.syncPageController();
    });

    final safeIndex = controller.index.clamp(0, controller.items.length - 1);

    return LayoutBuilder(
      builder: (context, constraints) {
        final cardHeight = WalletCardConfig.cardHeightForWidth(
          constraints.maxWidth,
        );

        return Column(
          children: [
            BalanceCarouselCardsView(
              controller: controller,
              onTopUp: onTopUp,
              onLedgerAddFunds: onLedgerAddFunds,
              onWalletTap: onWalletTap,
              onLedgerTap: onLedgerTap,
              height: cardHeight,
            ),
            const SizedBox(height: 16),
            BalanceCarouselNavigation(controller: controller, index: safeIndex),
          ],
        );
      },
    );
  }
}
