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/bottom_up_page_route.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 'receiving_info.dart';
import 'receiving_list_row.dart';
import 'receiving_editor.dart';

class ReceivingList extends StatefulWidget {
  @override
  _ReceivingListState createState() => _ReceivingListState();
}

class _ReceivingListState extends State<ReceivingList> {
  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,
              "receiving.title",
              fontSize: 20,
              color: Colors.white,
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                iconSize: 30,
                onPressed: () => searchPackage(context,
                    callbackPackageSelect: _searchCallback),
              ),
            ],
          ),
          floatingActionButton: FloatingActionButton.extended(
            onPressed: () {
              _newReceiving();
            },
            icon: Icon(Icons.add),
            label: LocalText(context, "receiving.new", color: Colors.white),
            backgroundColor: primaryColor,
          ),
          body: PaginatorListView<Package>(
            paginatorListener: packages,
            rowBuilder: (p) => ReceivingListRow(
              key: ValueKey(p.id),
              package: p,
            ),
            color: primaryColor,
          )),
    );
  }

  _newReceiving() {
    Navigator.push(
      context,
      CupertinoPageRoute(builder: (context) => ReceivingEditor()),
    );
  }

  _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) => ReceivingInfo(package: _package)),
    );
  }
}
