import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.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:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'fcs_shipment_editor.dart';
import 'fcs_shipment_list_row.dart';

class FcsShipmentList extends StatefulWidget {
  @override
  _FcsShipmentListState createState() => _FcsShipmentListState();
}

class _FcsShipmentListState extends State<FcsShipmentList> {
  bool _isLoading = false;
  var _controller = ScrollController();

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

    _controller.addListener(() async {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        Provider.of<FcsShipmentModel>(context, listen: false).loadMore();
      }
    });
    Provider.of<FcsShipmentModel>(context, listen: false).initData();
  }

  @override
  Widget build(BuildContext context) {
    var shipmentModel = Provider.of<FcsShipmentModel>(context);

    final popupMenu = LocalPopupMenuButton(
      popmenus: [
        LocalPopupMenu(
            id: 1,
            textKey: "FCSshipment.popupmenu.active",
            selected: shipmentModel.selectedIndex == 1),
        LocalPopupMenu(
            id: 2,
            textKey: "FCSshipment.popupmenu.shipped",
            selected: shipmentModel.selectedIndex == 2)
      ],
      popupMenuCallback: (p) => this.setState(() {
        shipmentModel.selectedIndex = 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, 'FCSshipment.list.title',
                  color: Colors.white, fontSize: 20),
              actions: [popupMenu],
            ),
            floatingActionButton: FloatingActionButton.extended(
              onPressed: () {
                _newShipment();
              },
              icon: Icon(Icons.add),
              label: LocalText(context, "FCSshipment.add", color: Colors.white),
              backgroundColor: primaryColor,
            ),
            body: Column(
              children: <Widget>[
                Expanded(
                  child: RefreshIndicator(
                    child: ListView.separated(
                        physics: AlwaysScrollableScrollPhysics(),
                        controller: _controller,
                        separatorBuilder: (context, index) => Divider(
                              color: Colors.black,
                              height: 1,
                            ),
                        scrollDirection: Axis.vertical,
                        shrinkWrap: true,
                        itemCount: shipmentModel.fcsShipments.length,
                        itemBuilder: (BuildContext context, int index) {
                          return FcsShipmentListRow(
                              key: ValueKey(
                                  shipmentModel.fcsShipments[index].id),
                              shipment: shipmentModel.fcsShipments[index]);
                        }),
                    onRefresh: () => shipmentModel.refresh(),
                  ),
                ),
                shipmentModel.isLoading
                    ? Container(
                        padding: EdgeInsets.all(8),
                        color: primaryColor,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text("Loading...",
                                style: TextStyle(color: Colors.white)),
                          ],
                        ),
                      )
                    : Container(),
              ],
            )));
  }

  _newShipment() {
    Navigator.of(context)
        .push(CupertinoPageRoute(builder: (context) => FcsShipmentEditor()));
  }
}
