import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

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

import 'package:pweb/pages/address_book/page/search.dart';
import 'package:pweb/pages/dashboard/payouts/single/adress_book/long_list/long_list.dart';
import 'package:pweb/pages/dashboard/payouts/single/adress_book/short_list.dart';
import 'package:pweb/providers/recipient.dart';

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


class AdressBookPayout extends StatefulWidget {
  final ValueChanged<Recipient> onSelected;

  const AdressBookPayout({
    super.key,
    required this.onSelected,
  });

  @override
  State<AdressBookPayout> createState() => _AdressBookPayoutState();
}

class _AdressBookPayoutState extends State<AdressBookPayout> {
  static const double _expandedHeight = 400;
  static const double _collapsedHeight = 200;
  static const double _cardMargin = 1;
  static const double _paddingAll = 16;
  static const double _spacingBetween = 16;

  final FocusNode _searchFocusNode = FocusNode();
  late final TextEditingController _searchController;

  bool get _isExpanded => _searchFocusNode.hasFocus;

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

    _searchController.addListener(() {
      provider.setQuery(_searchController.text);
    });
  }

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

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

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

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

    return SizedBox(
      height: _isExpanded ? _expandedHeight : _collapsedHeight,
      child: Card(
        margin: const EdgeInsets.all(_cardMargin),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        elevation: 4,
        color: Theme.of(context).colorScheme.onSecondary,
        child: Padding(
          padding: const EdgeInsets.all(_paddingAll),
          child: Column(
            children: [
              RecipientSearchField(
                controller: _searchController,
                focusNode: _searchFocusNode,
                onChanged: (_) {},
              ),
              const SizedBox(height: _spacingBetween),
              Expanded(
                child: _isExpanded
                    ? LongListAdressBookPayout(
                        filteredRecipients: provider.filteredRecipients,
                        onSelected: widget.onSelected,
                      )
                    : ShortListAdressBookPayout(
                        recipients: provider.recipients,
                        onSelected: widget.onSelected,
                      ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
