import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/package_search/package_serach.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:fcs/pagination/paginator_listview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'processing_info.dart';
import 'processing_list_row.dart';

class ProcessingList extends StatefulWidget {
  @override
  _ProcessingListState createState() => _ProcessingListState();
}

class _ProcessingListState extends State<ProcessingList> {
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
  }

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

  @override
  Widget build(BuildContext context) {
    var packageModel = Provider.of<PackageModel>(context);
    var packages = packageModel.activePackages;

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(CupertinoIcons.back),
              onPressed: () => Navigator.of(context).pop(),
            ),
            backgroundColor: primaryColor,
            title: LocalText(
              context,
              "processing.title",
              fontSize: 20,
              color: Colors.white,
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                iconSize: 30,
                onPressed: () => searchPackage(context,
                    callbackPackageSelect: _searchCallback),
              ),
            ],
          ),
          body: PaginatorListView<Package>(
            paginatorListener: packages,
            rowBuilder: (p) => ProcessingListRow(
              key: ValueKey(p.id),
              package: p,
            ),
            color: primaryColor,
          )),
    );
  }

  _searchCallback(Package package) async {
    var packageModel = Provider.of<PackageModel>(context, listen: false);
    Package _package = await packageModel.getPackage(package.id);
    if (_package == null) return;
    Navigator.push(
      context,
      CupertinoPageRoute(
          builder: (context) => ProcessingInfo(package: _package)),
    );
  }
}
