import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/address_book/page/filter_button.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/providers/recipient.dart';

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


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

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

  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;

  @override
  void initState() {
    super.initState();
    final provider = context.read<RecipientProvider>();
    _searchController = TextEditingController(text: provider.query);
    _searchFocusNode = FocusNode();
  }

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

  void _syncSearchField(RecipientProvider provider) {
    final query = provider.query;
    if (_searchController.text == query) return;

    _searchController.value = TextEditingValue(
      text: query,
      selection: TextSelection.collapsed(offset: query.length),
    );
  }

  @override
  Widget build(BuildContext context) {
    
    final loc = AppLocalizations.of(context)!;
    final provider = context.watch<RecipientProvider>(); 
    _syncSearchField(provider);

    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: provider.setQuery,
        ),
        const SizedBox(height: RecipientAddressBookPage._bigBox),
        Row(
          children: [
            RecipientFilterButton(
              text: loc.allStatus,
              filter: RecipientFilter.all,
              selected: provider.selectedFilter,
              onTap: provider.setFilter,
            ),
            RecipientFilterButton(
              text: loc.readyStatus,
              filter: RecipientFilter.ready,
              selected: provider.selectedFilter,
              onTap: provider.setFilter,
            ),
            RecipientFilterButton(
              text: loc.registeredStatus,
              filter: RecipientFilter.registered,
              selected: provider.selectedFilter,
              onTap: provider.setFilter,
            ),
            RecipientFilterButton(
              text: loc.notRegisteredStatus,
              filter: RecipientFilter.notRegistered,
              selected: provider.selectedFilter,
              onTap: provider.setFilter,
            ),
          ],
        ),
        SizedBox(
          height: RecipientAddressBookPage._expandedHeight,
          child: Padding(
            padding: const EdgeInsets.all(RecipientAddressBookPage._paddingAll),
            child: RecipientAddressBookList(
              filteredRecipients: provider.filteredRecipients,
              onEdit: (recipient) => widget.onEditRecipient?.call(recipient),
              onSelected: widget.onRecipientSelected,
            ),
          ),
        ),
      ],
    );
  }
}
