import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/discount/discount_list_row.dart';
import 'package:fcs/pages/discount/model/discount_model.dart';
import 'package:fcs/pages/main/util.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 'discount_editor.dart';

class DiscountList extends StatefulWidget {
  final bool selectionMode;

  const DiscountList({Key key, this.selectionMode = false}) : super(key: key);
  @override
  _DiscountListState createState() => _DiscountListState();
}

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

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

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

    final popupMenu = LocalPopupMenuButton(
      popmenus: [
        LocalPopupMenu(
            id: 1,
            textKey: "discount.popupmenu.available",
            selected: discountModel.selectedIndex == 1),
        LocalPopupMenu(
            id: 2,
            textKey: "discount.popupmenu.used",
            selected: discountModel.selectedIndex == 2)
      ],
      popupMenuCallback: (p) => this.setState(() {
        discountModel.selectedIndex = p.id;
      }),
    );

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            AppTranslations.of(context).text("discount.title"),
          ),
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          actions: <Widget>[popupMenu],
        ),
        body: Column(
          children: [
            Expanded(
              child: ListView.separated(
                separatorBuilder: (context, index) => Divider(
                  color: Colors.black,
                  height: 1,
                ),
                controller: _controller,
                itemCount: discountModel.discounts.length,
                itemBuilder: (BuildContext context, int index) {
                  var discount = discountModel.discounts[index];
                  return DiscountListRow(
                    key: ValueKey(discount.id),
                    discount: discount,
                    onSelect: (d) {
                      if (widget.selectionMode) {
                        Navigator.pop(context, discount);
                        return;
                      }
                      Navigator.push(
                        context,
                        CupertinoPageRoute(
                            builder: (context) =>
                                DiscountEditor(discount: discount)),
                      );
                    },
                  );
                },
              ),
            ),
            discountModel.isLoading
                ? Container(
                    padding: EdgeInsets.all(8),
                    color: primaryColor,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text("Loading...",
                            style: TextStyle(color: Colors.white)),
                      ],
                    ),
                  )
                : Container(),
          ],
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: () {
            Navigator.push(
              context,
              CupertinoPageRoute(builder: (context) => DiscountEditor()),
            );
          },
          icon: Icon(Icons.add),
          label: LocalText(
            context,
            'discount.new',
            color: Colors.white,
          ),
          backgroundColor: primaryColor,
        ),
      ),
    );
  }
}
