import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/models/recipient/recipient.dart';
import 'package:pshared/provider/recipient/provider.dart';
import 'package:pweb/pages/address_book/page/empty.dart';

import 'package:pweb/pages/address_book/page/header.dart';
import 'package:pweb/pages/address_book/page/list.dart';
import 'package:pweb/pages/address_book/page/search.dart';
import 'package:pweb/utils/recipient/filtering.dart';

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


class RecipientAddressBookPage extends StatefulWidget {
  final ValueChanged<Recipient> onRecipientSelected;
  final VoidCallback onAddRecipient;
  final ValueChanged<Recipient>? onEditRecipient;
  final ValueChanged<Recipient>? onDeleteRecipient;

  const RecipientAddressBookPage({
    super.key,
    required this.onRecipientSelected,
    required this.onAddRecipient,
    this.onEditRecipient,
    this.onDeleteRecipient,
  });

  static const double _expandedHeight = 550;
  static const double _paddingAll = 16;
  static const double _bigBox = 30;
  static const double _smallBox = 20;

  @override
  State<RecipientAddressBookPage> createState() => _RecipientAddressBookPageState();
}

class _RecipientAddressBookPageState extends State<RecipientAddressBookPage> {
  late final TextEditingController _searchController;
  late final FocusNode _searchFocusNode;
  String _query = '';

  @override
  void initState() {
    super.initState();
    _searchController = TextEditingController();
    _searchFocusNode = FocusNode();
  }

  @override
  void dispose() {
    _searchController.dispose();
    _searchFocusNode.dispose();
    super.dispose();
  }

  void _setQuery(String query) {
    setState(() {
      _query = query;
    });
  }

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final provider = context.watch<RecipientsProvider>(); 
    final filteredRecipients = filterRecipients(
      recipients: provider.recipients,
      query: _query,
    );

    if (provider.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (provider.error != null) {
      return Center(child: Text(loc.notificationError(provider.error ?? loc.noErrorInformation)));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        RecipientAddressBookHeader(onAddRecipient: widget.onAddRecipient),
        const SizedBox(height: RecipientAddressBookPage._smallBox),
        RecipientSearchField(
          controller: _searchController,
          focusNode: _searchFocusNode,
          onChanged: _setQuery,
        ),
        const SizedBox(height: RecipientAddressBookPage._bigBox),
        SizedBox(
          height: RecipientAddressBookPage._expandedHeight,
          child: Padding(
            padding: const EdgeInsets.all(RecipientAddressBookPage._paddingAll),
            child: provider.recipients.isEmpty
              ? RecipientAddressBookEmptyState(
                  message: loc.noRecipientsYet,
                  actionLabel: loc.addRecipient,
                  onAction: widget.onAddRecipient,
                )
              : filteredRecipients.isEmpty
                ? Center(child: Text(loc.noRecipientsFound))
                : RecipientAddressBookList(
                    filteredRecipients: filteredRecipients,
                    onEdit: (recipient) => widget.onEditRecipient?.call(recipient),
                    onDelete: (recipient) => widget.onDeleteRecipient?.call(recipient),
                    onSelected: widget.onRecipientSelected,
                  ),
          ),
        ),
      ],
    );
  }
}
