import 'package:flutter/material.dart';
import 'package:satu/core/entity/category_entity.dart';
import 'package:satu/core/entity/goods_entity.dart';
import 'package:satu/core/redux/actions/sell_actions.dart';
import 'package:satu/core/redux/store.dart';
import 'package:satu/core/services/dictionary_service.dart';
import 'package:satu/core/services/navigator_service.dart';
import 'package:satu/core/utils/locator.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/views/work/views/add_product/component/add_category_list_item.dart';
import 'package:satu/widgets/bar/products_app_bar.dart';
import 'package:satu/widgets/bar/products_title_bar.dart';
import 'package:satu/widgets/fields/input_field.dart';

import 'companent/contragent_list_item.dart';

class SelectContragentView extends StatefulWidget {
  @override
  _SelectContragentViewState createState() => _SelectContragentViewState();
}

class _SelectContragentViewState extends State<SelectContragentView> {
  final DictionaryService _dictionaryService = locator<DictionaryService>();
  final NavigatorService _navigatorService = locator<NavigatorService>();
  late TextEditingController _searchTextController;
  final FocusNode _searchFocusNode = new FocusNode();

  final List<Category> _contragents = [];

  @override
  void initState() {
    _searchTextController = TextEditingController();
    _searchTextController.addListener(() {
      if (_searchTextController.text.isNotEmpty) {
        searchByField(_searchTextController.text);
      } else {
        reset();
      }
    });
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const ProductsAppBar(
        title: 'Контрагент',
      ),
      body: Column(
        children: [
          InputField(
            placeholder: 'Поиск по наименованию или коду товара',
            search: true,
            controller: _searchTextController,
            fieldFocusNode: _searchFocusNode,
          ),
          verticalSpaceTiny,
          const ProductsTitleBarBar(
            title: 'Выберите контрагента',
          ),
          Expanded(
            child: ListView.separated(
              physics: const BouncingScrollPhysics(),
              itemCount: _contragents.length,
              itemBuilder: (BuildContext context, int index) {
                final Category category = _contragents[index];
                return ContragentListItem(
                  name: category.name,
                  key: Key('category_${category.id}'),
                  onPress: () => () {},
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return const Divider(
                  height: 1.0,
                  color: disableColor,
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  void reset() {
    _searchTextController.clear();
  }

  void searchByField(String query) async {

    List<Good> goods = await _dictionaryService.getGoodsByNameOrEan(query);
    setState(() {
      goods;
    });
  }
}
