import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/fcs_id_icon.dart';
import 'package:fcs/pages/widgets/local_button.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/multi_img_controller.dart';
import 'package:fcs/pages/widgets/multi_img_file.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:fcs/pages/widgets/status_tree.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 'receiving_editor.dart';

final DateFormat dateFormat = DateFormat("d MMM yyyy");

class ReceivingInfo extends StatefulWidget {
  final Package package;
  ReceivingInfo({this.package});

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

class _ReceivingInfoState extends State<ReceivingInfo> {
  Package _package;
  bool _isLoading = false;
  MultiImgController multiImgController = MultiImgController();

  @override
  void initState() {
    super.initState();
    initPackage(widget.package);
  }

  initPackage(Package package) {
    multiImgController.setImageUrls = package.photoUrls;
    setState(() {
      _package = package;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    bool isCustomer = Provider.of<MainModel>(context).isCustomer();

    final trackingIdBox = DisplayText(
      text: _package.trackingID,
      labelTextKey: "package.tracking.id",
      iconData: MaterialCommunityIcons.barcode_scan,
    );
    var fcsIDBox = DisplayText(
      text: _package.fcsID,
      labelTextKey: "processing.fcs.id",
      icon: FcsIDIcon(),
    );
    final customerNameBox = DisplayText(
      text: _package.userName,
      labelTextKey: "package.create.name",
      iconData: Icons.perm_identity,
    );
    final remarkBox = DisplayText(
      text: _package.remark ?? "-",
      labelTextKey: "package.edit.remark",
      iconData: Entypo.new_message,
    );
    final img = MultiImageFile(
      enabled: false,
      controller: multiImgController,
      title: "Receipt File",
    );

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back, color: primaryColor, size: 30),
            onPressed: () => Navigator.of(context).pop(),
          ),
          shadowColor: Colors.transparent,
          backgroundColor: Colors.white,
          title: LocalText(
            context,
            "receiving.info",
            fontSize: 20,
            color: primaryColor,
          ),
          actions: isCustomer
              ? null
              : <Widget>[
                  IconButton(
                    icon: Icon(Icons.delete, color: primaryColor),
                    onPressed: _delete,
                  ),
                  IconButton(
                    icon: Icon(Icons.edit, color: primaryColor),
                    onPressed: _edit,
                  )
                ],
        ),
        body: Card(
          child: Column(
            children: <Widget>[
              Expanded(
                  child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: ListView(children: <Widget>[
                  trackingIdBox,
                  fcsIDBox,
                  customerNameBox,
                  remarkBox,
                  _package.photoUrls.length == 0 ? Container() : img,
                  StatusTree(
                      shipmentHistory: _package.shipmentHistory,
                      currentStatus: _package.status),
                  SizedBox(
                    height: 20,
                  )
                ]),
              )),
            ],
          ),
        ),
      ),
    );
  }

  _edit() async {
    await Navigator.push(
      context,
      CupertinoPageRoute(
          builder: (context) => ReceivingEditor(
                package: widget.package,
              )),
    );
    PackageModel packageModel =
        Provider.of<PackageModel>(context, listen: false);
    var pkg = await packageModel.getPackage(widget.package.id);
    initPackage(pkg);
  }

  _delete() {
    showConfirmDialog(context, "receiving.delete.confirm", _deleteReceiving);
  }

  _deleteReceiving() async {
    setState(() {
      _isLoading = true;
    });
    try {
      PackageModel packageModel =
          Provider.of<PackageModel>(context, listen: false);
      await packageModel.deleteReceiving(_package);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
