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_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:provider/provider.dart';

import 'package:flutter/material.dart';

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

class ShipmentList extends StatefulWidget {
  @override
  _ShipmentListState createState() => _ShipmentListState();
}

class _ShipmentListState extends State<ShipmentList> {
  bool _isLoading = false;

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

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

  @override
  Widget build(BuildContext context) {
    var pickupModel = Provider.of<ShipmentModel>(context);
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon: new Icon(Icons.close),
              onPressed: () => Navigator.of(context).pop(),
            ),
            backgroundColor: primaryColor,
            title: LocalText(context, "shipment",
                fontSize: 18, color: Colors.white),
            actions: <Widget>[
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                iconSize: 30,
                // onPressed: () => showPlacesSearch(context),
              ),
            ],
            // bottom: TabBar(
            //   unselectedLabelColor: Colors.grey,
            //   tabs: [
            //     Tab(
            //       text: "Upcoming",
            //     ),
            //     Tab(text: "Completed"),
            //     Tab(text: "Canceled"),
            //   ],
            // ),
          ),
          floatingActionButton: FloatingActionButton.extended(
            onPressed: () {
              _newPickup();
            },
            icon: Icon(Icons.add),
            label: Text(AppTranslations.of(context).text("shipment.new")),
            backgroundColor: primaryColor,
          ),
          body: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: pickupModel.pickups.length,
              itemBuilder: (BuildContext context, int index) {
                return ShipmentListRow(pickUp: pickupModel.pickups[index]);
              }),
          // body: TabBarView(
          //   children: [
          //     //Icon(Icons.directions_car),
          //     _upComing(),
          //     _completed(),
          //     _canceled()
          //   ],
          // )
        ),
      ),
    );
  }

  _newPickup() {
    Navigator.of(context).push(BottomUpPageRoute(ShipmentEditor()));
  }

  Widget _upComing() {
    var pickupModel = Provider.of<ShipmentModel>(context);
    return Column(
      children: <Widget>[
        Expanded(
          child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: pickupModel.upcoming.length,
              itemBuilder: (BuildContext context, int index) {
                return ShipmentListRow(pickUp: pickupModel.upcoming[index]);
              }),
        ),
      ],
    );
  }

  Widget _completed() {
    var pickupModel = Provider.of<ShipmentModel>(context);
    return Column(
      children: <Widget>[
        Expanded(
          child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: pickupModel.completed.length,
              itemBuilder: (BuildContext context, int index) {
                return ShipmentListRow(pickUp: pickupModel.completed[index]);
              }),
        ),
      ],
    );
  }

  Widget _canceled() {
    var pickupModel = Provider.of<ShipmentModel>(context);
    return Column(
      children: <Widget>[
        Expanded(
          child: new ListView.separated(
              separatorBuilder: (context, index) => Divider(
                    color: Colors.black,
                  ),
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.only(top: 15),
              shrinkWrap: true,
              itemCount: pickupModel.canceled.length,
              itemBuilder: (BuildContext context, int index) {
                return ShipmentListRow(pickUp: pickupModel.canceled[index]);
              }),
        ),
      ],
    );
  }
}
