import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/shipment/model/shipment_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.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:provider/provider.dart';

import 'package:flutter/material.dart';

import 'shipment_editor.dart';
import 'shipment_list_row.dart';

class ShipmentList extends StatefulWidget {
  final bool forCustomer;

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

class _ShipmentListState extends State<ShipmentList> {
  bool _isLoading = false;
  var _controller = ScrollController();
  bool _forCustomer = true;

  @override
  void initState() {
    super.initState();
    _forCustomer = widget.forCustomer;
    _controller.addListener(() async {
      if (_controller.position.pixels == _controller.position.maxScrollExtent) {
        Provider.of<ShipmentModel>(context, listen: false)
            .loadMore(isCustomer: widget.forCustomer);
      }
    });

    Provider.of<ShipmentModel>(context, listen: false)
        .initData(widget.forCustomer);
  }

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

  @override
  Widget build(BuildContext context) {
    ShipmentModel shipmentModel = Provider.of<ShipmentModel>(context);
    final popupMenu = LocalPopupMenuButton(
      popmenus: _forCustomer
          ? [
              LocalPopupMenu(
                  id: 1, textKey: "shipment.popupmenu.active", selected: true),
              LocalPopupMenu(id: 2, textKey: "shipment.popupmenu.delivered"),
            ]
          : [
              LocalPopupMenu(
                  id: 1, textKey: "shipment.popupmenu.active", selected: true),
              LocalPopupMenu(id: 2, textKey: "shipment.popupmenu.delivered"),
              LocalPopupMenu(id: 3, textKey: "shipment.popupmenu.mypickup"),
            ],
      popupMenuCallback: (p) => this.setState(() {
        shipmentModel.menuSelectedIndex = p.id;
        if (p.id == 3) {
          Provider.of<ShipmentModel>(context, listen: false)
              .initData(widget.forCustomer, myPickup: true);
        } else {
          Provider.of<ShipmentModel>(context, listen: false)
              .initData(widget.forCustomer, myPickup: false);
        }
      }),
    );
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(CupertinoIcons.back),
              onPressed: () => Navigator.of(context).pop(),
            ),
            backgroundColor: primaryColor,
            title: LocalText(context, "shipment",
                fontSize: 18, color: Colors.white),
            actions: <Widget>[popupMenu],
          ),
          floatingActionButton: widget.forCustomer
              ? FloatingActionButton.extended(
                  onPressed: () {
                    _newPickup();
                  },
                  icon: Icon(Icons.add),
                  label:
                      LocalText(context, "shipment.new", color: Colors.white),
                  backgroundColor: primaryColor,
                )
              : Container(),
          body: Column(
            children: [
              Expanded(
                child: RefreshIndicator(
                  child: ListView.separated(
                      controller: _controller,
                      separatorBuilder: (context, index) => Divider(
                            color: Colors.grey,
                            height: 1,
                          ),
                      scrollDirection: Axis.vertical,
                      itemCount: shipmentModel.shipments.length,
                      itemBuilder: (BuildContext context, int index) {
                        return ShipmentListRow(
                          key: ValueKey(shipmentModel.shipments[index].id),
                          shipment: shipmentModel.shipments[index],
                          isCustomer: widget.forCustomer,
                        );
                      }),
                  onRefresh: () =>
                      shipmentModel.refresh(isCustomer: widget.forCustomer),
                ),
              ),
              shipmentModel.isLoading
                  ? Container(
                      padding: EdgeInsets.all(8),
                      color: primaryColor,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("Loading...",
                              style: TextStyle(color: Colors.white)),
                        ],
                      ),
                    )
                  : Container(),
            ],
          ),
        ),
      ),
    );
  }

  _newPickup() {
    Navigator.of(context)
        .push(CupertinoPageRoute(builder: (context) => ShipmentEditor()));
  }
}
