import 'package:flutter/material.dart';

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

import 'package:pweb/utils/dimensions.dart';

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


class RecipientSearchResults extends StatelessWidget {
  final AppDimensions dimensions;
  final RecipientsProvider recipientProvider;
  final List<Recipient> results;
  final ValueChanged<Recipient> onRecipientSelected;

  const RecipientSearchResults({
    super.key,
    required this.dimensions,
    required this.recipientProvider,
    required this.results,
    required this.onRecipientSelected,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    if (recipientProvider.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }

    if (recipientProvider.error != null) {
      return Text(
        loc.notificationError(recipientProvider.error ?? loc.noErrorInformation),
        style: TextStyle(color: Theme.of(context).colorScheme.error),
      );
    }

    if (recipientProvider.recipients.isEmpty) {
      return Text(loc.noRecipientsYet);
    }

    if (results.isEmpty) {
      return Text(loc.noRecipientsFound);
    }

    return ConstrainedBox(
      constraints: const BoxConstraints(maxHeight: 240),
      child: ListView.separated(
        shrinkWrap: true,
        itemCount: results.length,
        separatorBuilder: (_, _) => SizedBox(height: dimensions.paddingSmall),
        itemBuilder: (context, index) {
          final recipient = results[index];
          return ListTile(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(dimensions.borderRadiusSmall),
            ),
            leading: CircleAvatar(
              child: Text(recipient.name.substring(0, 1).toUpperCase()),
            ),
            title: Text(recipient.name),
            subtitle: Text(recipient.email),
            trailing: const Icon(Icons.arrow_forward_ios_rounded, size: 16),
            onTap: () => onRecipientSelected(recipient),
          );
        },
      ),
    );
  }
}
