import 'package:flutter/material.dart';

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

import 'package:pweb/pages/address_book/page/search.dart';
import 'package:pweb/pages/payment_methods/widgets/card.dart';
import 'package:pweb/pages/payment_methods/widgets/search.dart';
import 'package:pweb/pages/payment_methods/widgets/section_title.dart';
import 'package:pweb/providers/recipient.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 RecipientProvider recipientProvider;
  final TextEditingController searchController;
  final FocusNode searchFocusNode;
  final ValueChanged<String> onSearchChanged;
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onRecipientCleared;

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

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

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SectionTitle(loc.recipient),
        SizedBox(height: dimensions.paddingSmall),
        RecipientSearchField(
          controller: searchController,
          onChanged: onSearchChanged,
          focusNode: searchFocusNode,
        ),
        if (recipientProvider.query.isNotEmpty) ...[
          SizedBox(height: dimensions.paddingMedium),
          RecipientSearchResults(
            dimensions: dimensions,
            recipientProvider: recipientProvider,
            onRecipientSelected: onRecipientSelected,
          ),
        ],
      ],
    );
  }
}