import 'dart:async';
import 'dart:io';

import 'package:fcs/domain/entities/invoice.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:path_provider/path_provider.dart';

import 'invoice_editor.dart';
import 'payment_page.dart';
import 'payment_pdf_screen.dart';

class InvoiceListRow extends StatefulWidget {
  final Invoice invoice;
  const InvoiceListRow({this.invoice});

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

class _InvoiceListRowState extends State<InvoiceListRow> {
  var dateFormatter = new DateFormat('dd MMM yyyy');
  final double dotSize = 15.0;
  Invoice _invoice = new Invoice();
  String pdfPath = '';

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

    if (widget.invoice != null) {
      _invoice = widget.invoice;
    }

    fromAsset('assets/Invoice-A092(A)-32.pdf', 'Invoice-A092(A)-32.pdf')
        .then((f) {
      setState(() {
        pdfPath = f.path;
      });
    });
  }

  Future<File> fromAsset(String asset, String filename) async {
    // To open from assets, you can copy them to the app storage folder, and the access them "locally"
    Completer<File> completer = Completer();
    print('asset => $asset');
    print('assest => ${await rootBundle.load(asset)}');
    try {
      var dir = await getApplicationDocumentsDirectory();
      File file = File("${dir.path}/$filename");
      var data = await rootBundle.load(asset);
      print('data => $data');
      var bytes = data.buffer.asUint8List();
      await file.writeAsBytes(bytes, flush: true);
      completer.complete(file);
    } catch (e) {
      throw Exception('Error parsing asset file! ===> ' + e.toString());
    }

    return completer.future;
  }

  @override
  Widget build(BuildContext context) {
    var owner = true;
    return Container(
      padding: EdgeInsets.only(left: 15, right: 15),
      child: Row(
        children: <Widget>[
          Expanded(
            child: new Padding(
              padding: const EdgeInsets.symmetric(vertical: 10.0),
              child: InkWell(
                onTap: () {
                  owner
                      ? Navigator.of(context).push(
                          BottomUpPageRoute(InvoiceEditor(invoice: _invoice)))
                      : Navigator.of(context)
                          .push(BottomUpPageRoute(PaymentPDFScreen(
                          path: pdfPath,
                        )));
                },
                child: new Row(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(left: 5, right: 10),
                      child: Icon(
                        FontAwesomeIcons.fileInvoice,
                        color: primaryColor,
                        size: 30,
                      ),
                    ),
                    new Expanded(
                      child: new Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Padding(
                            padding: const EdgeInsets.only(left: 8.0),
                            child: new Text(
                              _invoice.invoiceNumber == null
                                  ? ''
                                  : _invoice.invoiceNumber,
                              style: new TextStyle(
                                  fontSize: 15.0, color: Colors.black),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.only(left: 10.0, top: 10),
                            child: new Text(
                              dateFormatter.format(_invoice.invoiceDate),
                              style: new TextStyle(
                                  fontSize: 15.0, color: Colors.grey),
                            ),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
          // Padding(
          //   padding: const EdgeInsets.all(0),
          //   child: getStatus(_invoice.status),
          // ),
          _invoice.status == "Pending"
              ? Padding(
                  padding: const EdgeInsets.only(left: 10.0),
                  child: InkWell(
                      child: RaisedButton(
                    child: Row(
                      children: <Widget>[
                        Icon(
                          Icons.payment,
                          color: primaryColor,
                        ),
                        Padding(
                          padding: const EdgeInsets.only(left: 8.0),
                          child: Text("Payment"),
                        )
                      ],
                    ),
                    onPressed: () {
                      Navigator.of(context).push(
                          BottomUpPageRoute(PaymentPage(invoice: _invoice)));
                    },
                  )),
                )
              : Container(),
          Padding(
            padding: const EdgeInsets.only(left: 8.0),
            child: InkWell(
              child: Icon(
                Icons.more_vert,
                color: primaryColor,
              ),
              onTap: () {
                var act = actionSheet(context);
                showCupertinoModalPopup(
                    context: context, builder: (BuildContext context) => act);
              },
            ),
          ),
        ],
      ),
    );
  }

  actionSheet(BuildContext context) {
    return CupertinoActionSheet(
      actions: <Widget>[
        CupertinoActionSheetAction(
          child: Row(
            children: <Widget>[
              Icon(Icons.file_download),
              Padding(
                padding: const EdgeInsets.only(left: 8.0),
                child: Text(
                  'Download',
                  style: TextStyle(fontSize: 16, color: Colors.black),
                ),
              ),
            ],
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        )
      ],
    );
  }
}
