import 'package:fcs/helpers/theme.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 'delivery_list_row.dart';
import 'model/delivery_model.dart';

class DeliverList extends StatefulWidget {
  @override
  _DeliverListState createState() => _DeliverListState();
}

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

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

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

  @override
  Widget build(BuildContext context) {
    var deliveryModel = Provider.of<DeliveryModel>(context);
    final popupMenu = LocalPopupMenuButton(
      popmenus: [
        LocalPopupMenu(
            id: 1,
            textKey: "delivery.popupmenu.active",
            selected: deliveryModel.selectedIndex == 1),
        LocalPopupMenu(
            id: 2,
            textKey: "delivery.popupmenu.delivered",
            selected: deliveryModel.selectedIndex == 2)
      ],
      popupMenuCallback: (p) => this.setState(() {
        deliveryModel.selectedIndex = p.id;
      }),
    );
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(CupertinoIcons.back),
              onPressed: () => Navigator.of(context).pop(),
            ),
            backgroundColor: primaryColor,
            title: LocalText(context, "delivery",
                color: Colors.white, fontSize: 20),
            actions: <Widget>[popupMenu],
          ),
          body: Column(
            children: [
              Expanded(
                child: RefreshIndicator(
                  child: new ListView.separated(
                      physics: AlwaysScrollableScrollPhysics(),
                      controller: _controller,
                      separatorBuilder: (context, index) => Divider(
                            color: Colors.black,
                            height: 1,
                          ),
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      itemCount: deliveryModel.cartons.length,
                      itemBuilder: (BuildContext context, int index) {
                        return DeliveryListRow(
                            key: ValueKey(deliveryModel.cartons[index].id),
                            box: deliveryModel.cartons[index]);
                      }),
                  onRefresh: () => deliveryModel.refresh(),
                ),
              ),
              deliveryModel.isLoading
                  ? Container(
                      padding: EdgeInsets.all(8),
                      color: primaryColor,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text("Loading...",
                              style: TextStyle(color: Colors.white)),
                        ],
                      ),
                    )
                  : Container(),
            ],
          ),
        ),
      ),
    );
  }
}
