import 'package:flutter/material.dart';
import 'package:flutter_expandable_fab/flutter_expandable_fab.dart';

import '../../Controller/account_controller.dart';
import '../../Controller/recurring_transaction_controller.dart';
import '../../Controller/transaction_controller.dart';

/// Ein Floating Action Button, der beim Klicken ein expandierendes Menü öffnet,
/// um neue Transaktionen oder Konten anzulegen.
class FloatingCreationButton extends StatefulWidget {
  /// Erstellt eine neue Instanz dieser Klasse
  const FloatingCreationButton({super.key});

  @override
  State<FloatingCreationButton> createState() => _FloatingCreationButtonState();
}

class _FloatingCreationButtonState extends State<FloatingCreationButton> {
  final AccountController _accountController = AccountController();
  final RecurringTransactionController _recurringTransactionController =
      RecurringTransactionController();
  final TransactionController _transactionController = TransactionController();

  final _key = GlobalKey<ExpandableFabState>();

  @override
  void initState() {
    super.initState();

    _accountController.selected.addListener(() {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(final BuildContext context) {
    final List<Widget> fabs = [];
    if (_accountController.selected.value != null) {
      fabs
        ..add(
          _expandableButton(
            label: 'Neue Transaktion',
            icon: Icons.swap_horiz,
            onPressed: _transactionController.newTransactionHandler,
          ),
        )
        ..add(
          _expandableButton(
            label: 'Neue wiederkehrende Transaktion',
            icon: Icons.repeat,
            onPressed:
                _recurringTransactionController.newRecurringTransactionHandler,
          ),
        );
    }

    fabs.add(
      _expandableButton(
        label: 'Neues Konto',
        icon: Icons.account_balance_wallet,
        onPressed: _accountController.newAccountHandler,
      ),
    );

    return ExpandableFab(
      key: _key,
      openButtonBuilder: RotateFloatingActionButtonBuilder(
        child: const Icon(Icons.add),
      ),
      type: ExpandableFabType.up,
      childrenAnimation: ExpandableFabAnimation.none,
      distance: 70,
      children: fabs,
    );
  }

  Widget _expandableButton({
    required final String label,
    required final IconData icon,
    required final VoidCallback onPressed,
  }) => GestureDetector(
    onTap: onPressed,
    child: Row(
      children: <Widget>[
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          decoration: BoxDecoration(borderRadius: BorderRadius.circular(8)),
          child: Text(label),
        ),
        const SizedBox(width: 12),
        FloatingActionButton.small(
          heroTag: null,
          onPressed: () {
            onPressed.call();

            final ExpandableFabState? state = _key.currentState;
            if (state != null && state.isOpen) {
              state.close();
            }
          },
          child: Icon(icon),
        ),
      ],
    ),
  );
}
