import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/package/package_info.dart';
import 'package:fcs/pages/package/package_list_row.dart';
import 'package:fcs/pages/package_search/package_serach.dart';
import 'package:fcs/pages/widgets/local_popup_menu_button.dart';
import 'package:fcs/pages/widgets/local_popupmenu.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';

class PackageList extends StatefulWidget {
  final bool forCustomer;

  const PackageList({Key key, this.forCustomer = true}) : super(key: key);
  @override
  _PackageListState createState() => _PackageListState();
}

class _PackageListState extends State<PackageList> {
  bool _isLoading = false;

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

  @override
  Widget build(BuildContext context) {
    var packageModel = Provider.of<PackageModel>(context);
    var packages = widget.forCustomer
        ? packageModel.customerPackages
        : packageModel.packages;

    final popupMenu = LocalPopupMenuButton(
      popmenus: [
        LocalPopupMenu(
            id: 1,
            textKey: "package.popupmenu.active",
            selected: packageModel.menuSelectedIndex == 1),
        LocalPopupMenu(
            id: 2,
            textKey: "package.popupmenu.delivered",
            selected: packageModel.menuSelectedIndex == 2)
      ],
      popupMenuCallback: (p) => this.setState(() {
        packageModel.menuSelectedIndex = p.id;
      }),
    );

    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,
              "package.title",
              fontSize: 20,
              color: Colors.white,
            ),
            actions: <Widget>[
              widget.forCustomer
                  ? Container()
                  : IconButton(
                      icon: Icon(
                        Icons.search,
                        color: Colors.white,
                      ),
                      iconSize: 30,
                      onPressed: () => searchPackage(context,
                          callbackPackageSelect: _searchCallback),
                    ),
              popupMenu
            ],
          ),
          body: PaginatorListView<Package>(
            paginatorListener: packages,
            rowBuilder: (p) => PackageListRow(
              key: ValueKey(p.id),
              package: p,
              isCustomer: widget.forCustomer,
            ),
            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) => PackageInfo(package: _package)),
    );
  }
}
