import 'package:flutter/material.dart';

class ChatSearchAppBar extends StatelessWidget implements PreferredSizeWidget {
  final TextEditingController controller;
  final int matchCount;
  final int activeIndex;
  final ValueChanged<String> onChanged;
  final VoidCallback onClose;
  final VoidCallback? onPrevious;
  final VoidCallback? onNext;

  const ChatSearchAppBar({
    required this.controller,
    required this.matchCount,
    required this.activeIndex,
    required this.onChanged,
    required this.onClose,
    required this.onPrevious,
    required this.onNext,
    super.key,
  });

  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    final counterText = matchCount == 0
        ? '0/0'
        : '${activeIndex + 1}/$matchCount';
    return AppBar(
      leading: IconButton(
        tooltip: 'Zurück',
        icon: const Icon(Icons.arrow_back),
        onPressed: onClose,
      ),
      title: TextField(
        controller: controller,
        autofocus: true,
        textInputAction: TextInputAction.search,
        decoration: const InputDecoration(
          hintText: 'In Chat suchen…',
          border: InputBorder.none,
        ),
        onChanged: onChanged,
      ),
      actions: [
        Center(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 4),
            child: Text(
              counterText,
              style: const TextStyle(fontSize: 12),
            ),
          ),
        ),
        IconButton(
          tooltip: 'Vorheriges Ergebnis',
          icon: const Icon(Icons.keyboard_arrow_up),
          onPressed: onPrevious,
        ),
        IconButton(
          tooltip: 'Nächstes Ergebnis',
          icon: const Icon(Icons.keyboard_arrow_down),
          onPressed: onNext,
        ),
      ],
    );
  }
}
