import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:satu/core/models/inventarization/inventarization_response.dart';
import 'package:satu/core/services/inventarization_service.dart';
import 'package:satu/core/utils/locator.dart';
import 'package:satu/shared/app_colors.dart';
import 'package:satu/shared/shared_styles.dart';
import 'package:satu/shared/ui_helpers.dart';
import 'package:satu/views/inventarization/widget/inventarization_list_tile.dart';

import '../../../widgets/bar/products_app_bar.dart';

class InventarizationView extends StatefulWidget {
  const InventarizationView({Key? key}) : super(key: key);

  @override
  State<InventarizationView> createState() => _InventarizationViewState();
}

class _InventarizationViewState extends State<InventarizationView> {

  final InventarizationService _service = locator<InventarizationService>();

  static const _pageSize = 20;

  final PagingController<int, InventarizationResponse> _pagingController =
  PagingController(firstPageKey: 1);

  @override
  void initState() {
    _pagingController.addPageRequestListener((pageKey) {
      _fetchData(pageKey, _pageSize);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const ProductsAppBar(
        title: 'Инвентаризация',
        drawerShow: true,
      ),
      body: Column(
        children: [
          verticalSpaceSmall,
          Padding(
            padding: const EdgeInsets.only( left: 15.0),
            child: SizedBox(
              width: double.infinity,
              child: Row(
                children: [
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Номер документа',
                      style: textGray11Style,
                    ),
                  ),
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Дата создания',
                      style: textGray11Style,
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Text(
                      'Акты',
                      style: textGray11Style,
                    ),
                  ),
                ],
              ),
            ),
          ),
          verticalSpaceSmall,
          Expanded(
            child: PagedListView<int, InventarizationResponse>.separated(
              physics: const BouncingScrollPhysics(),
              separatorBuilder: (BuildContext context, int index) {
                return const Divider(
                  height: 1.0,
                  color: disableColor,
                );
              },
              pagingController: _pagingController,
              builderDelegate: PagedChildBuilderDelegate<InventarizationResponse>(
                itemBuilder: (BuildContext context, InventarizationResponse item,
                    int index) {
                  return InventarizationListTile(
                    key: Key('category_${item.id}'),
                    // onPress: () async {
                    //   final dynamic result = await _navigatorService
                    //       .push(categoryEditRoute, arguments: item);
                    //   if (result != null && true == (result as bool)) {
                    //     _pagingController.refresh();
                    //   }
                    // },
                    item: item,
                  );
                },
              ),
            ),
          )
        ],
      ),
    );
  }

  Future<void> _fetchData(int pageKey, int perPage) async {
    final List<InventarizationResponse> newItems = await _service
        .getList(page: pageKey, perpage: perPage);
    final isLastPage = newItems.length < _pageSize;
    if (isLastPage) {
      _pagingController.appendLastPage(newItems);
    } else {
      final nextPageKey = pageKey + 1;
      _pagingController.appendPage(newItems, nextPageKey);
    }
  }
}
