import 'package:flutter/material.dart';

import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/recipient/provider.dart';

import 'package:pweb/pages/address_book/page/search.dart';
import 'package:pweb/pages/payout_page/send/widgets/card.dart';
import 'package:pweb/pages/payout_page/send/widgets/search.dart';
import 'package:pweb/pages/payout_page/send/widgets/section/title.dart';
import 'package:pweb/pages/payout_page/send/widgets/add_recipient_tile.dart';
import 'package:pweb/pages/dashboard/payouts/single/address_book/short_list.dart';
import 'package:pweb/utils/dimensions.dart';

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


class RecipientSection extends StatelessWidget {
  final Recipient? recipient;
  final AppDimensions dimensions;
  final RecipientsProvider recipientProvider;
  final String searchQuery;
  final List<Recipient> filteredRecipients;
  final TextEditingController searchController;
  final FocusNode searchFocusNode;
  final ValueChanged<String> onSearchChanged;
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onRecipientCleared;
  final VoidCallback onAddRecipient;

  const RecipientSection({
    super.key,
    required this.recipient,
    required this.dimensions,
    required this.recipientProvider,
    required this.searchQuery,
    required this.filteredRecipients,
    required this.searchController,
    required this.searchFocusNode,
    required this.onSearchChanged,
    required this.onRecipientSelected,
    required this.onRecipientCleared,
    required this.onAddRecipient,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    if (recipient != null) {
      return SelectedRecipientCard(
        dimensions: dimensions,
        recipient: recipient!,
        onClear: onRecipientCleared,
      );
    }

    return AnimatedBuilder(
      animation: recipientProvider,
      builder: (context, _) {
        final hasQuery = searchQuery.isNotEmpty;

        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SectionTitle(loc.recipient),
            SizedBox(height: dimensions.paddingSmall),
            RecipientSearchField(
              controller: searchController,
              onChanged: onSearchChanged,
              focusNode: searchFocusNode,
            ),
            if (hasQuery) ...[
              SizedBox(height: dimensions.paddingMedium),
              RecipientSearchResults(
                dimensions: dimensions,
                recipientProvider: recipientProvider,
                results: filteredRecipients,
                onRecipientSelected: onRecipientSelected,
              ),
            ] else ...[
              SizedBox(height: dimensions.paddingMedium),
              ShortListAddressBookPayout(
                recipients: recipientProvider.recipients,
                onSelected: onRecipientSelected,
                leading: AddRecipientTile(
                  label: loc.addRecipient,
                  onTap: onAddRecipient,
                ),
              ),
            ],
          ],
        );
      },
    );
  }
}
