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 StatelessWidget {
  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
  Widget build(BuildContext context) {
    
    final loc = AppLocalizations.of(context)!;
    final provider = context.watch<RecipientProvider>(); 

    if (provider.isLoading) {
      return const Center(child: CircularProgressIndicator()); //TODO This should be in the provider
    }

    if (provider.error != null) {
      return Center(child: Text('Error: ${provider.error}'));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        RecipientAddressBookHeader(onAddRecipient: onAddRecipient),
        const SizedBox(height: _smallBox),
        RecipientSearchField(
          controller: TextEditingController(text: provider.query),
          focusNode: FocusNode(),
          onChanged: provider.setQuery,
        ),
        const SizedBox(height: _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: _expandedHeight,
          child: Padding(
            padding: const EdgeInsets.all(_paddingAll),
            child: RecipientAddressBookList(
              filteredRecipients: provider.filteredRecipients,
              onEdit: (recipient) => onEditRecipient?.call(recipient),
              onSelected: onRecipientSelected,
            ),
          ),
        ),
      ],
    );
  }
}