import 'package:fcs/domain/entities/box.dart';
import 'package:fcs/domain/entities/cargo.dart';
import 'package:fcs/domain/entities/invoice.dart';
import 'package:fcs/domain/entities/payment_method.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/discount/model/discount_model.dart';
import 'package:fcs/pages/main/model/main_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/multi_img_controller.dart';
import 'package:fcs/pages/widgets/multi_img_file.dart';
import 'package:fcs/pages/widgets/my_data_table.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

import 'box_addition.dart';

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

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

class _InvoiceEditorState extends State<InvoiceEditor> {
  List<PaymentMethod> get paymentMethods {
    List<PaymentMethod> methods = [
      PaymentMethod(
        name: 'AYA Bank',
        accountName: 'FCS',
        account: '100 23404320548398',
        phone: '+959123456789',
      ),
      PaymentMethod(
        name: 'KBZ Bank',
        accountName: 'FCS',
        account: '100 23404320548398',
        phone: '+959123456789',
      ),
      PaymentMethod(
        name: 'PayPal',
        accountName: 'FCS',
        link: 'https://www.paypal.com/donate/buttons',
      ),
    ];
    return methods;
  }

  var dateFormatter = new DateFormat('dd MMM yyyy');
  TextEditingController _invoiceNumberController = new TextEditingController();
  TextEditingController _dateController = new TextEditingController();
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _phoneController = new TextEditingController();
  TextEditingController _discountController = new TextEditingController();
  TextEditingController _amountController = new TextEditingController();
  TextEditingController _statusController = new TextEditingController();
  TextEditingController _handlingFeeController = new TextEditingController();
  TextEditingController _customFeeController = new TextEditingController();
  MultiImgController multiImgController = MultiImgController();
  TextEditingController _descriptionController = new TextEditingController();
  TextEditingController _balanceController = new TextEditingController();

  Invoice _invoice;
  bool _isLoading = false;
  List<Box> _boxes = [];
  bool isSwitched = false;
  String deliveryfee = '\$0';
  List<Cargo> _cargoTypes = [
    Cargo(type: 'General Cargo', weight: 33, price: 6),
    Cargo(type: 'Medicine', weight: 33, price: 7),
    Cargo(type: 'Dangerous Cargo', weight: 33, price: 8)
  ];

  List<String> _receipts = [
    "assets/buying_online_with_first_last_name.png",
  ];

  @override
  void initState() {
    super.initState();
    if (widget.invoice != null) {
      _invoice = widget.invoice;
      _invoiceNumberController.text = _invoice.invoiceNumber;
      _dateController.text = dateFormatter.format(_invoice.invoiceDate);
      _nameController.text = _invoice.customerName;
      _phoneController.text = _invoice.customerPhoneNumber;
      // _amountController.text = _invoice.getAmount.toString();
      _amountController.text = _invoice.amount.toString();
      _statusController.text = _invoice.status.toString();
      _handlingFeeController.text = '0';
      _customFeeController.text = '0';
      // multiImgController.setImageUrls = _receipts;
      _descriptionController.text = 'For Electronics goods';
      _balanceController.text =
          (_invoice.amount - _invoice.receipts[0].amount).toString();
      // _boxes = _invoice.packages;
    } else {
      _dateController.text = dateFormatter.format(DateTime.now());
      _amountController.text = '0';
      _handlingFeeController.text = '0';
      _customFeeController.text = '0';
      _descriptionController.text = '';
      _balanceController.text = '0';
    }

    _boxes = [
      Box(
          shipmentNumber: "A202",
          receiverNumber: "3",
          receiverName: "Ko Myo Min",
          boxNumber: "1",
          rate: 7,
          packageType: "General",
          weight: 75,
          status: "Packed",
          receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
          cargoDesc: "Clothes",
          arrivedDate: DateTime(2020, 6, 1),
          width: 10,
          height: 10,
          length: 10,
          // packages: packages,
          // statusHistory: statusHistory,
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ]),
      Box(
          shipmentNumber: "A202",
          receiverNumber: "3",
          receiverName: "Ko Myo Min",
          boxNumber: "2",
          rate: 7,
          packageType: "General",
          weight: 75,
          status: "Packed",
          cargoDesc: "Clothes",
          arrivedDate: DateTime(2020, 6, 1),
          width: 10,
          height: 10,
          length: 10,
          // statusHistory: statusHistory,
          // packages: packages,
          receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
          cargoTypes: [
            Cargo(type: 'General Cargo', weight: 25),
            Cargo(type: 'Medicine', weight: 20),
            Cargo(type: 'Dangerous Cargo', weight: 30)
          ])
    ];
  }

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

  @override
  Widget build(BuildContext context) {
    var mainModel = Provider.of<MainModel>(context);
    var discountModel = Provider.of<DiscountModel>(context);

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          title: LocalText(context, 'invoice.form.title',
              color: Colors.white, fontSize: 20),
        ),
        body: Card(
          child: Column(
            children: <Widget>[
              Expanded(
                  child: Padding(
                padding: const EdgeInsets.all(10.0),
                child: ListView(children: <Widget>[
                  TextFormField(
                      controller: _dateController,
                      readOnly: true,
                      decoration: InputDecoration(
                        fillColor: Colors.white,
                        labelText: 'Invoice Date',
                        labelStyle: TextStyle(
                          fontSize: 16,
                        ),
                        filled: true,
                        enabledBorder: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        icon: Icon(
                          Icons.date_range,
                          color: primaryColor,
                        ),
                      )),
                  widget.invoice == null
                      ? Container()
                      : Container(
                          padding: EdgeInsets.only(top: 5),
                          child: TextFormField(
                              controller: _invoiceNumberController,
                              readOnly: true,
                              decoration: InputDecoration(
                                fillColor: Colors.white,
                                labelText: 'Invoice Number',
                                labelStyle: TextStyle(
                                  fontSize: 16,
                                ),
                                filled: true,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                icon: Icon(
                                  FontAwesomeIcons.fileInvoice,
                                  color: primaryColor,
                                ),
                              )),
                        ),
                  widget.invoice == null
                      ? Container(
                          padding: EdgeInsets.only(top: 5),
                          child: TextFormField(
                            initialValue: "Ko Myo Min",
                            cursorColor: primaryColor,
                            decoration: InputDecoration(
                                fillColor: Colors.white,
                                labelText: 'Customer Name',
                                labelStyle: TextStyle(
                                  fontSize: 16,
                                ),
                                filled: true,
                                focusedBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                        color: Colors.grey, width: 1.0)),
                                icon: Icon(
                                  FontAwesomeIcons.fileInvoice,
                                  color: primaryColor,
                                ),
                                suffixIcon: IconButton(
                                    icon: Icon(
                                      Icons.search,
                                      color: Colors.grey,
                                    ),
                                    onPressed: () {})),
                          ),
                        )
                      : Container(),
                  widget.invoice == null
                      ? Container()
                      : Container(
                          padding: EdgeInsets.only(top: 0),
                          child: TextFormField(
                              controller: _nameController,
                              readOnly: true,
                              decoration: InputDecoration(
                                fillColor: Colors.white,
                                labelText: 'Customer Name',
                                labelStyle: TextStyle(
                                  fontSize: 16,
                                ),
                                filled: true,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                icon: Icon(
                                  Feather.user,
                                  color: primaryColor,
                                ),
                              )),
                        ),
                  // widget.invoice == null
                  //     ? Container()
                  //     : TextFormField(
                  //         controller: _phoneController,
                  //         readOnly: true,
                  //         decoration: InputDecoration(
                  //           fillColor: Colors.white,
                  //           labelText: 'Customer Phone Number',
                  //           labelStyle:
                  //               TextStyle(fontSize: 16, color: Colors.grey),
                  //           filled: true,
                  //           enabledBorder: InputBorder.none,
                  //           focusedBorder: InputBorder.none,
                  //           icon: Icon(
                  //             Icons.phone,
                  //             color: Colors.grey,
                  //           ),
                  //         )),
                  // Container(
                  //   padding: EdgeInsets.only(top: 0),
                  //   child: fcsInput('Amount', FontAwesomeIcons.moneyBill,
                  //       controller: _amountController),
                  // ),

                  widget.invoice == null
                      ? Container()
                      : Container(
                          padding: EdgeInsets.only(top: 5),
                          child: TextFormField(
                              controller: _statusController,
                              readOnly: true,
                              decoration: InputDecoration(
                                fillColor: Colors.white,
                                labelText: 'Status',
                                labelStyle: TextStyle(
                                  fontSize: 16,
                                ),
                                filled: true,
                                enabledBorder: InputBorder.none,
                                focusedBorder: InputBorder.none,
                                icon: Icon(
                                  Icons.av_timer,
                                  color: primaryColor,
                                ),
                              )),
                        ),

                  Container(
                    padding: EdgeInsets.only(top: 0),
                    child: TextFormField(
                        controller: _amountController,
                        readOnly: true,
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: 'Amount',
                          labelStyle: TextStyle(
                            fontSize: 16,
                          ),
                          filled: true,
                          enabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          icon: Icon(
                            FontAwesomeIcons.moneyBill,
                            color: primaryColor,
                          ),
                        )),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 5),
                    child: TextFormField(
                        controller: _balanceController,
                        readOnly: true,
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: 'Balance',
                          labelStyle: TextStyle(
                            fontSize: 16,
                          ),
                          filled: true,
                          enabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          icon: Icon(
                            FontAwesomeIcons.moneyBill,
                            color: primaryColor,
                          ),
                        )),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 5),
                    child: TextFormField(
                        controller: _handlingFeeController,
                        readOnly: true,
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: 'Handling Fee',
                          labelStyle: TextStyle(
                            fontSize: 16,
                          ),
                          filled: true,
                          enabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                          icon: Icon(
                            FontAwesomeIcons.moneyBill,
                            color: primaryColor,
                          ),
                        )),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 5),
                    child: TextFormField(
                        controller: _customFeeController,
                        readOnly: false,
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: 'Customs Fee',
                          labelStyle: TextStyle(
                            fontSize: 16,
                          ),
                          filled: true,
                          icon: Icon(
                            FontAwesomeIcons.moneyBill,
                            color: primaryColor,
                          ),
                          focusedBorder: UnderlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.grey, width: 1.0)),
                        )),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 5),
                    child: TextFormField(
                        controller: _descriptionController,
                        readOnly: false,
                        decoration: InputDecoration(
                          fillColor: Colors.white,
                          labelText: 'Customs Fee Description',
                          labelStyle: TextStyle(
                            fontSize: 16,
                          ),
                          filled: true,
                          icon: Icon(
                            Icons.comment,
                            color: primaryColor,
                          ),
                          focusedBorder: UnderlineInputBorder(
                              borderSide:
                                  BorderSide(color: Colors.grey, width: 1.0)),
                        )),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 20, left: 18),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            child: Text(
                          'Discounts',
                          style: TextStyle(
                              fontSize: 16,
                              color: primaryColor,
                              fontWeight: FontWeight.bold),
                        )),
                        Container(
                          width: 150.0,
                          child: DropdownButtonFormField(
                            items: discountModel.discounts
                                .map((e) => DropdownMenuItem(
                                    child: Text(e.code), value: e.code))
                                .toList(),
                            onChanged: (selected) => {},
                          ),
                        ),
                      ],
                    ),
                  ),

                  Container(
                    padding: EdgeInsets.only(top: 5, left: 18),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            child: Text(
                          'Payment Method',
                          style: TextStyle(
                              fontSize: 16,
                              color: primaryColor,
                              fontWeight: FontWeight.bold),
                        )),
                        Container(
                          width: 150.0,
                          child: DropdownButtonFormField(
                            items: paymentMethods
                                .map((e) => DropdownMenuItem(
                                    child: Text(e.name), value: e.name))
                                .toList(),
                            onChanged: (selected) => {},
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 5),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                            child: Padding(
                          padding: const EdgeInsets.only(left: 18.0),
                          child: Text(
                            'Delivery fee:',
                            style: TextStyle(
                                fontSize: 16,
                                color: primaryColor,
                                fontWeight: FontWeight.bold),
                          ),
                        )),
                        Switch(
                          value: isSwitched,
                          onChanged: (value) {
                            setState(() {
                              isSwitched = value;
                              if (value) {
                                deliveryfee = '\$5';
                              } else {
                                deliveryfee = '\$0';
                              }
                              print(isSwitched);
                            });
                          },
                          activeTrackColor: primaryColor.withOpacity(0.8),
                          activeColor: primaryColor,
                        ),
                        Text(
                          '(Delivery fee : $deliveryfee)',
                          style: TextStyle(
                              color: primaryColor, fontWeight: FontWeight.bold),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),

                  ExpansionTile(
                    title: Text(
                      'Payment Attachment',
                      style: TextStyle(
                          color: primaryColor, fontWeight: FontWeight.bold),
                    ),
                    children: <Widget>[
                      widget.invoice != null
                          ? Padding(
                              padding: EdgeInsets.only(left: 20),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.only(top: 10),
                                    child: Text(
                                      '${dateFormatter.format(_invoice.receipts[0].date)} ',
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 16),
                                    ),
                                  ),
                                  Container(
                                      padding: EdgeInsets.only(left: 10),
                                      child: MultiImageFile(
                                        enabled: false,
                                        controller: multiImgController,
                                        title: "Receipt",
                                      )),
                                  Container(
                                    padding: EdgeInsets.only(top: 10, left: 10),
                                    child: Text(
                                      '\$${_invoice.receipts[0].amount} ',
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 16),
                                    ),
                                  ),
                                ],
                              ),
                            )
                          : Container(),
                      SizedBox(
                        height: 25,
                      ),
                    ],
                  ),
                  ExpansionTile(
                    title: Text(
                      'Box Information',
                      style: TextStyle(
                          color: primaryColor, fontWeight: FontWeight.bold),
                    ),
                    children: <Widget>[
                      Container(
                        child: SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: MyDataTable(
                            headingRowHeight: 40,
                            columnSpacing: 20,
                            columns: [
                              MyDataColumn(
                                label: LocalText(
                                  context,
                                  "box.number",
                                  color: Colors.grey,
                                ),
                              ),
                              MyDataColumn(
                                label: LocalText(
                                  context,
                                  "box.length",
                                  color: Colors.grey,
                                ),
                              ),
                              MyDataColumn(
                                label: LocalText(
                                  context,
                                  "box.width",
                                  color: Colors.grey,
                                ),
                              ),
                              MyDataColumn(
                                label: LocalText(
                                  context,
                                  "box.height",
                                  color: Colors.grey,
                                ),
                              ),
                            ],
                            rows: getBoxRow(context),
                          ),
                        ),
                      ),
                      true
                          ? Container(
                              padding: EdgeInsets.only(top: 20),
                              child: Align(
                                alignment: Alignment.bottomRight,
                                child: FloatingActionButton.extended(
                                  icon: Icon(Icons.add),
                                  label: Text(AppTranslations.of(context)
                                      .text("invoice.add_box")),
                                  backgroundColor: primaryColor,
                                  onPressed: () {
                                    Navigator.of(context)
                                        .push(BottomUpPageRoute(BoxAddition()));
                                  },
                                ),
                              ),
                            )
                          : Container(),
                      SizedBox(height: 25),
                    ],
                  ),

                  //Cargo Table
                  ExpansionTile(
                      title: Text(
                        'Cargo Table',
                        style: TextStyle(
                            color: primaryColor, fontWeight: FontWeight.bold),
                      ),
                      children: <Widget>[
                        Container(
                          child: SingleChildScrollView(
                            scrollDirection: Axis.horizontal,
                            child: MyDataTable(
                                headingRowHeight: 40,
                                columnSpacing: 20,
                                columns: [
                                  MyDataColumn(
                                    label: LocalText(
                                      context,
                                      "cargo.type",
                                      color: Colors.grey,
                                    ),
                                  ),
                                  MyDataColumn(
                                    label: LocalText(
                                      context,
                                      "cargo.weight",
                                      color: Colors.grey,
                                    ),
                                  ),
                                  MyDataColumn(
                                    label: LocalText(
                                      context,
                                      "cargo.rate",
                                      color: Colors.grey,
                                    ),
                                  ),
                                  MyDataColumn(
                                    label: LocalText(
                                      context,
                                      "cargo.amount",
                                      color: Colors.grey,
                                    ),
                                  ),
                                ],
                                rows: getCargoDataRow(context)),
                          ),
                        ),
                      ]),
                ]),
              )),
              widget.invoice == null
                  ? Align(
                      alignment: Alignment.bottomCenter,
                      child: Center(
                          child: Container(
                        width: 250,
                        child: FlatButton(
                          child: Text('Create invoice'),
                          color: primaryColor,
                          textColor: Colors.white,
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                      )))
                  : mainModel.isCustomer()
                      ? Container()
                      : Container(
                          child: Column(
                          children: <Widget>[
                            Align(
                                alignment: Alignment.bottomCenter,
                                child: Center(
                                    child: Container(
                                  width: 250,
                                  child: FlatButton(
                                    child: Text('Save invoice'),
                                    color: primaryColor,
                                    textColor: Colors.white,
                                    onPressed: () {
                                      Navigator.pop(context);
                                    },
                                  ),
                                ))),
                          ],
                        )),
              widget.invoice == null
                  ? Container()
                  : Align(
                      alignment: Alignment.bottomCenter,
                      child: Center(
                          child: Container(
                        width: 250,
                        child: FlatButton(
                          child: Text('Attach Payment Receipt'),
                          color: primaryColor,
                          textColor: Colors.white,
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                      )))
            ],
          ),
        ),
      ),
    );
  }

  getCargoTableByBox(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(5.0),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(
            color: Colors.grey[100],
            width: 1,
          ),
        ),
        child: Row(
          children: <Widget>[
            Expanded(
              child: Container(
                child: SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: MyDataTable(
                      headingRowHeight: 40,
                      columnSpacing: 20,
                      columns: [
                        MyDataColumn(
                          label: LocalText(
                            context,
                            "cargo.type",
                            color: Colors.grey,
                          ),
                        ),
                        MyDataColumn(
                          label: LocalText(
                            context,
                            "cargo.weight",
                            color: Colors.grey,
                          ),
                        ),
                        MyDataColumn(
                          label: LocalText(
                            context,
                            "cargo.rate",
                            color: Colors.grey,
                          ),
                        ),
                        MyDataColumn(
                          label: LocalText(
                            context,
                            "cargo.amount",
                            color: Colors.grey,
                          ),
                        ),
                      ],
                      rows: getCargoDataRow(context)),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  List<MyDataRow> getBoxRow(BuildContext context) {
    return _boxes.map((p) {
      p.cargoTypes.map((cargo) {
        print('cargo => $cargo');
        _cargoTypes.asMap().map((index, _cargo) {
          if (_cargo.type == cargo.type) {
            setState(() {
              _cargoTypes[index].weight += cargo.weight;
            });
            print('${_cargoTypes[index].type} =>${_cargoTypes[index]}');
          }
        });
      });
      return MyDataRow(
        onSelectChanged: (bool selected) {},
        cells: [
          MyDataCell(new Text(
            p.boxNumber == null
                ? ""
                : '${p.shipmentNumber}-${p.receiverNumber} #${p.boxNumber}',
            style: textStyle,
          )),
          MyDataCell(new Text(
            p.length == null ? "" : p.length.toString(),
            style: textStyle,
          )),
          MyDataCell(new Text(
            p.width == null ? "" : p.width.toString(),
            style: textStyle,
          )),
          MyDataCell(new Text(
            p.height == null ? "" : p.height.toString(),
            style: textStyle,
          )),
        ],
      );
    }).toList();
  }

  List<MyDataRow> getCargoDataRow(BuildContext context) {
    return _cargoTypes.map((cargo) {
      var amt = cargo.weight * cargo.price;
      return MyDataRow(
        onSelectChanged: (bool selected) {},
        cells: [
          MyDataCell(new Text(
            cargo.type,
            style: textStyle,
          )),
          MyDataCell(new Text(
            cargo.weight.toString(),
            style: textStyle,
          )),
          MyDataCell(new Text(
            '\$${cargo.price}',
            style: textStyle,
          )),
          MyDataCell(new Text(
            "\$$amt",
            style: textStyle,
          )),
        ],
      );
    }).toList();
  }
}
