import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/payout_page/send/widgets/payment_info/section.dart';
import 'package:pweb/pages/payout_page/send/widgets/recipient/section.dart';
import 'package:pweb/pages/payout_page/send/widgets/section/card.dart';
import 'package:pweb/utils/dimensions.dart';
import 'package:pweb/models/state/visibility.dart';


class PaymentRecipientDetailsCard extends StatelessWidget {
  final AppDimensions dimensions;
  final Recipient? recipient;
  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;
  final VoidCallback onAddPaymentMethod;
  final VisibilityState paymentDetailsVisibility;
  final VoidCallback onTogglePaymentDetails;

  const PaymentRecipientDetailsCard({
    super.key,
    required this.dimensions,
    required this.recipient,
    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,
    required this.onAddPaymentMethod,
    required this.paymentDetailsVisibility,
    required this.onTogglePaymentDetails,
  });

  @override
  Widget build(BuildContext context) {
    final flowProvider = context.watch<PaymentFlowProvider>();
    final isRecipientSelectionLocked =
        !flowProvider.hasRecipient && flowProvider.manualPaymentData != null;

    return PaymentSectionCard(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if (!isRecipientSelectionLocked) ...[
            RecipientSection(
              recipient: recipient,
              dimensions: dimensions,
              recipientProvider: recipientProvider,
              searchQuery: searchQuery,
              filteredRecipients: filteredRecipients,
              searchController: searchController,
              searchFocusNode: searchFocusNode,
              onSearchChanged: onSearchChanged,
              onRecipientSelected: onRecipientSelected,
              onRecipientCleared: onRecipientCleared,
              onAddRecipient: onAddRecipient,
            ),
            SizedBox(height: dimensions.paddingMedium),
          ],
          PaymentInfoSection(
            dimensions: dimensions,
            titleVisibility: VisibilityState.hidden,
            onAddMethod: onAddPaymentMethod,
            paymentDetailsVisibility: paymentDetailsVisibility,
            onTogglePaymentDetails: onTogglePaymentDetails,
          ),
        ],
      ),
    );
  }
}
