import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/model/main_model.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/package_new.dart';
import 'package:fcs/pages/package_search/package_serach.dart';
import 'package:fcs/pages/user_search/user_serach.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.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';

class PackageList extends StatefulWidget {
  @override
  _PackageListState createState() => _PackageListState();
}

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

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

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

  @override
  Widget build(BuildContext context) {
    var packageModel = Provider.of<PackageModel>(context);
    bool isCustomer = context.select((MainModel m) => m.isCustomer());

    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>[
              isCustomer
                  ? Container()
                  : IconButton(
                      icon: Icon(
                        Icons.search,
                        color: Colors.white,
                      ),
                      iconSize: 30,
                      onPressed: () => searchPackage(context,
                          callbackPackageSelect: _searchCallback),
                    ),
            ],
          ),
          floatingActionButton: isCustomer
              ? Container()
              : FloatingActionButton.extended(
                  onPressed: () {
                    _newPackage();
                  },
                  icon: Icon(Icons.add),
                  label: Text(
                      AppTranslations.of(context).text("package.create.title")),
                  backgroundColor: primaryColor,
                ),
          body: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: packageModel.packages.length,
              itemBuilder: (BuildContext context, int index) {
                return PackageListRow(
                  key: ValueKey(packageModel.packages[index].id),
                  package: packageModel.packages[index],
                );
              })),
    );
  }

  _newPackage() {
    Navigator.push(
      context,
      BottomUpPageRoute(PackageNew()),
    );
  }

  _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,
      BottomUpPageRoute(PackageInfo(package: _package)),
    );
  }
}
