import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/domain/entities/pickup.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/package_search/package_serach.dart';
import 'package:fcs/pages/pickup/pickup_editor.dart';
import 'package:fcs/pages/pickup/pickup_info.dart';
import 'package:fcs/pages/pickup/pickup_list_row.dart';
import 'package:fcs/pages/pickup_search/pickup_serach.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:fcs/pagination/paginator_listview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'model/pickup_model.dart';

class PickupList extends StatefulWidget {
  @override
  _PickupListState createState() => _PickupListState();
}

class _PickupListState extends State<PickupList> {
  bool _isLoading = false;

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

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

  @override
  Widget build(BuildContext context) {
    var pickupModel = Provider.of<PickupModel>(context);
    var pickups = pickupModel.pickups;

    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,
              "pickup.title",
              fontSize: 20,
              color: Colors.white,
            ),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                iconSize: 30,
                onPressed: () => searchPickup(context,
                    callbackPickupSelect: _searchCallback),
              ),
            ],
          ),
          body: PaginatorListView<Pickup>(
            paginatorListener: pickups!,
            rowBuilder: (p) => PickupListRow(key: ValueKey(p.id), pickup: p),
            color: primaryColor,
          )),
    );
  }

  _searchCallback(Pickup pickup) async {
    Pickup? _pickup = await context.read<PickupModel>().getPickup(pickup.id!);
    if (_pickup == null) return;
    Navigator.push(
      context,
      CupertinoPageRoute(builder: (context) => PickupInfo(pickup: _pickup)),
    );
  }
}
