import 'package:flutter/material.dart';
import 'package:fotodocumentation/l10n/app_localizations.dart';
import 'package:fotodocumentation/pages/ui_utils/general_style.dart';
import 'package:fotodocumentation/utils/di_container.dart';

class SearchBarWidget extends StatefulWidget {
  final TextEditingController searchController;
  final Function(String) onSearch;
  const SearchBarWidget({super.key, required this.searchController, required this.onSearch});

  @override
  State<SearchBarWidget> createState() => _SearchBarWidgetState();
}

class _SearchBarWidgetState extends State<SearchBarWidget> {
  GeneralStyle get _generalStyle => DiContainer.get();
  
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 0.0, vertical: 16.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
            child: TextField(
              key: Key("Search_text_field"),
              controller: widget.searchController,
              textAlignVertical: TextAlignVertical.center,
              decoration: InputDecoration(
                hint: Text(
                  AppLocalizations.of(context)!.searchTFHint,
                  style: TextStyle(
                    fontSize: 16,
                    fontFamily: _generalStyle.fontFamily,
                    color: _generalStyle.secondaryTextLabelColor.withValues(alpha: 0.5),
                  ),
                ),
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(30),
                  borderSide: BorderSide(color: _generalStyle.secondaryTextLabelColor),
                ),
                prefixIcon: Icon(Icons.search, size: 30, color: _generalStyle.secondaryWidgetBackgroundColor,),
                contentPadding: EdgeInsets.zero,
                isDense: true,
              ),
              onSubmitted: (_) => _actionSubmit(),
            ),
          ),
          const SizedBox(width: 8),
        ],
      ),
    );
  }

  void _actionSubmit() {
    widget.onSearch(widget.searchController.text);
  }
}
