import 'package:fcs/domain/entities/fcs_shipment.dart';
import 'package:fcs/domain/entities/shipment.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/shipment/model/shipment_model.dart';
import 'package:fcs/pages/widgets/local_button.dart';
import 'package:fcs/pages/widgets/local_dropdown.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/local_title.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'widgets.dart';

class ShipmentPack extends StatefulWidget {
  final Shipment shipment;
  ShipmentPack({this.shipment});

  @override
  _ShipmentPackState createState() => _ShipmentPackState();
}

class _ShipmentPackState extends State<ShipmentPack> {
  var dateFormatter = new DateFormat('dd MMM yyyy');
  var timeFormatter = new DateFormat('jm');

  Shipment _shipment;
  bool _isLoading = false;
  var now = new DateTime.now();

  FcsShipment _fcsShipment;
  List<FcsShipment> _fcsShipments;

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

    _shipment = widget.shipment;
    _loadFcsShipments();
  }

  _loadFcsShipments() async {
    FcsShipmentModel fcsShipmentModel =
        Provider.of<FcsShipmentModel>(context, listen: false);
    var fcsShipments = await fcsShipmentModel.getActiveFcsShipments();
    var fcsShipment = fcsShipments
        .firstWhere((e) => e.id == _shipment.fcsShipmentID, orElse: () => null);
    setState(() {
      _fcsShipments = fcsShipments;
      _fcsShipment = fcsShipment;
    });
  }

  @override
  Widget build(BuildContext context) {
    final shipmentNumberBox = getShipmentNumberStatus(context, _shipment);

    var fcsShipmentsBox = LocalDropdown<FcsShipment>(
      callback: (v) {
        setState(() {
          _fcsShipment = v;
        });
      },
      labelKey: "shipment.pack.fcs.shipment",
      iconData: MaterialCommunityIcons.worker,
      display: (u) => u.shipmentNumber,
      selectedValue: _fcsShipment,
      values: _fcsShipments,
    );

    final packbtn = LocalButton(
      textKey: "shipment.pack.btn",
      callBack: _save,
    );
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(
              CupertinoIcons.back,
              color: primaryColor,
            ),
            onPressed: () => Navigator.of(context).pop(),
          ),
          shadowColor: Colors.transparent,
          backgroundColor: Colors.white,
          title: LocalText(
            context,
            "shipment.pack.menuitem",
            fontSize: 20,
            color: primaryColor,
          ),
        ),
        body: Padding(
          padding: const EdgeInsets.all(10.0),
          child: ListView(
            children: <Widget>[
              Center(child: shipmentNumberBox),
              SizedBox(
                height: 10,
              ),
              LocalTitle(textKey: "shipment.pack.fcs.shipment"),
              fcsShipmentsBox,
              SizedBox(
                height: 10,
              ),
              packbtn,
            ],
          ),
        ),
      ),
    );
  }

  _save() async {
    _shipment.fcsShipmentID = this._fcsShipment.id;
    setState(() {
      _isLoading = true;
    });
    try {
      ShipmentModel shipmentModel =
          Provider.of<ShipmentModel>(context, listen: false);
      await shipmentModel.packShipment(_shipment);
      Navigator.pop(context, true);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
