import 'package:farm_tpf/presentation/screens/location_unit/bloc/location_bloc.dart';
import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class WidgetSearchLocation extends StatefulWidget {
  final LocationType? type;
  final int? filterId;
  final int? selectedId;
  WidgetSearchLocation({this.type, this.selectedId, this.filterId});
  @override
  _WidgetSearchLocationState createState() => _WidgetSearchLocationState();
}

class _WidgetSearchLocationState extends State<WidgetSearchLocation> {
  BuildContext? _blocContext;
  final _searchController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _searchController.addListener(() {
      final keyword = _searchController.text;
      if (keyword.isNotEmpty) {
        //search when text change
      }
    });
  }

  Widget getSearchBarUI() {
    _searchController.text = "";
    return Padding(
      padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 0),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Padding(
              padding: const EdgeInsets.only(right: 8, top: 8, bottom: 0),
              child: Container(
                child: Padding(
                  padding: const EdgeInsets.only(left: 16, right: 16, top: 4, bottom: 4),
                  child: TextField(
                    textInputAction: TextInputAction.done,
                    controller: _searchController,
                    onChanged: (String txt) {},
                    cursorColor: AppColors.GRAY1,
                    decoration: InputDecoration(
                        suffixIcon: IconButton(
                            icon: Icon(
                              Icons.search,
                              size: 30,
                            ),
                            onPressed: () {
                              FocusScope.of(context).requestFocus(FocusNode());
                              BlocProvider.of<LocationBloc>(_blocContext!).add(OnSearch(
                                searchString: _searchController.text,
                                locationType: widget.type ?? LocationType.country,
                                selectedId: widget.selectedId ?? -1,
                                filterId: widget.filterId ?? -1,
                              ));
                            }),
                        hintText: 'Tìm kiếm',
                        hintStyle: TextStyle(color: Colors.grey[500])),
                    onSubmitted: (value) {
                      FocusScope.of(context).requestFocus(FocusNode());
                      BlocProvider.of<LocationBloc>(_blocContext!).add(
                        OnSearch(
                          searchString: value,
                          locationType: widget.type ?? LocationType.country,
                          selectedId: widget.selectedId ?? -1,
                          filterId: widget.filterId ?? -1,
                        ),
                      );
                    },
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    _blocContext = context;
    return Container(child: getSearchBarUI());
  }

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