import 'package:fcs/domain/entities/package.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:fcs/pages/widgets/display_text.dart';
import 'package:fcs/pages/widgets/fcs_id_icon.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 'processing_editor.dart';

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

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

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

class _ProcessingInfoState extends State<ProcessingInfo> {
  var dateFormatter = new DateFormat('dd MMM yyyy');
  Package _package;
  bool _isLoading = false;
  MultiImgController multiImgController = MultiImgController();

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

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

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

  @override
  Widget build(BuildContext context) {
    final trackingIdBox = DisplayText(
      text: _package.trackingID,
      labelTextKey: "processing.tracking.id",
      iconData: MaterialCommunityIcons.barcode_scan,
    );
    var fcsIDBox = DisplayText(
      text: _package.fcsID,
      labelTextKey: "processing.fcs.id",
      icon: FcsIDIcon(),
    );
    final phoneNumberBox = DisplayText(
      text: _package.phoneNumber,
      labelTextKey: "processing.phone",
      iconData: Icons.phone,
    );
    final customerNameBox = DisplayText(
      text: _package.userName,
      labelTextKey: "processing.name",
      iconData: Icons.perm_identity,
    );
    final marketBox = DisplayText(
      text: _package.market ?? "-",
      labelTextKey: "processing.market",
      iconData: Icons.store,
    );
    final descBox = DisplayText(
      text: _package.desc ?? "-",
      labelTextKey: "processing.desc",
      iconData: MaterialCommunityIcons.message_text_outline,
    );
    final remarkBox = DisplayText(
      text: _package.remark ?? "-",
      labelTextKey: "processing.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,
            "processing.info.title",
            fontSize: 20,
            color: primaryColor,
          ),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.delete, color: primaryColor),
              onPressed: _delete,
            ),
            IconButton(
              icon: Icon(Icons.edit, color: primaryColor),
              onPressed: _gotoEditor,
            ),
          ],
        ),
        body: Card(
          child: Column(
            children: <Widget>[
              Expanded(
                  child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: ListView(children: <Widget>[
                  trackingIdBox,
                  fcsIDBox,
                  customerNameBox,
                  phoneNumberBox,
                  marketBox,
                  descBox,
                  remarkBox,
                  _package.photoUrls.length == 0 ? Container() : img,
                  StatusTree(
                      shipmentHistory: _package.shipmentHistory,
                      currentStatus: _package.status),
                  SizedBox(
                    height: 20,
                  )
                ]),
              )),
            ],
          ),
        ),
      ),
    );
  }

  _delete() {
    showConfirmDialog(context, "processing.delete.confirm", _deletePackage);
  }

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

  _gotoEditor() async {
    bool deleted = await Navigator.push<bool>(
        context,
        CupertinoPageRoute(
            builder: (context) => ProcessingEditor(
                  package: widget.package,
                )));
    if (deleted ?? false) {
      Navigator.pop(context);
    } else {
      PackageModel packageModel =
          Provider.of<PackageModel>(context, listen: false);
      Package p = await packageModel.getPackage(_package.id);
      initPackage(p);
    }
  }
}
