import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/domain/entities/custom_duty.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/rates/custom_list.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.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:provider/provider.dart';
import 'input_text_border.dart';

class CargoTypeAddition extends StatefulWidget {
  @override
  _CargoTypeAdditionState createState() => _CargoTypeAdditionState();
}

class _CargoTypeAdditionState extends State<CargoTypeAddition> {
  bool _isLoading = false;

  List<CargoType> cargos = [];

  @override
  void initState() {
    super.initState();
    var shipmentRateModel =
        Provider.of<ShipmentRateModel>(context, listen: false);
    cargos = List.from(shipmentRateModel.rate.cargoTypes);
    cargos.forEach((p) {
      p.isChecked = false;
      p.isCutomDuty = false;
      p.weight = 0;
      p.qty = null;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    final cargoTableTitleBox = Container(
      padding: EdgeInsets.only(left: 0.0, right: 10.0, top: 20),
      child: Row(
        children: <Widget>[
          SizedBox(
            width: 50,
          ),
          Expanded(
            child: LocalText(
              context,
              "cargo.type",
              color: Colors.grey,
            ),
          ),
          SizedBox(
            width: 40,
          ),
          LocalText(
            context,
            "cargo.qty",
            color: Colors.grey,
          ),
          Spacer(),
          LocalText(
            context,
            "cargo.weight",
            color: Colors.grey,
          ),
        ],
      ),
    );

    List<Widget> getCargoRowList() {
      return cargos.map((c) {
        return Container(
          child: Container(
            padding:
                EdgeInsets.only(left: 0.0, right: 5.0, top: 3.0, bottom: 3.0),
            decoration: BoxDecoration(
              border: Border(
                bottom: BorderSide(color: Colors.grey[350], width: 1),
              ),
            ),
            child: Row(
              children: <Widget>[
                Checkbox(
                    value: c.isChecked,
                    activeColor: primaryColor,
                    onChanged: (bool check) {
                      setState(() {
                        c.isChecked = check;
                      });
                    }),
                Expanded(child: new Text(c.name, style: textStyle)),
                SizedBox(
                  width: c.isCutomDuty ? 50 : 40,
                  child: c.isCutomDuty
                      ? InkWell(
                          onTap: () {
                            setState(() {
                              cargos.removeWhere((t) => t.name == c.name);
                            });
                          },
                          child: Icon(
                            Icons.remove_circle,
                            color: Colors.black45,
                          ),
                        )
                      : Container(),
                ),
                Container(
                  height: 30,
                  width: 50,
                  child: c.isCutomDuty
                      ? InputTextBorder(
                          onAdd: (value) {
                            setState(() {
                              if (value == "" || value == null) {
                                c.isChecked = false;
                              } else {
                                c.qty = int.parse(value);
                                c.isChecked = true;
                              }
                            });
                          },
                        )
                      : Center(child: Text("-")),
                ),
                Spacer(),
                Container(
                  height: 30,
                  width: 60,
                  child: Center(
                      child: Text(
                          c.weight == null ? "" : c.weight.toStringAsFixed(2))),
                  // child: InputTextBorder(
                  //   onAdd: (value) {
                  //     setState(() {
                  //       if (value == "" || value == null) {
                  //         c.isChecked = false;
                  //       } else {
                  //         c.weight = double.parse(value);
                  //         c.isChecked = true;
                  //       }
                  //     });
                  //   },
                  // ),
                ),
              ],
            ),
          ),
        );
      }).toList();
    }

    final saveBtn = fcsButton(
      context,
      getLocalString(context, 'box.cargo.save.btn'),
      callack: () {
        List<CargoType> _cargos =
            this.cargos.where((c) => c.isChecked).toList();
        Navigator.pop(context, _cargos);
      },
    );

    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,
              "cargo.form.title",
              fontSize: 20,
              color: primaryColor,
            )),
        body: Container(
          padding: EdgeInsets.all(8),
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              LocalTitle(
                textKey: "box.select.cargo.title",
                trailing: IconButton(
                    icon: Icon(
                      Icons.add_circle,
                      color: primaryColor,
                    ),
                    onPressed: () async {
                      CustomDuty customDuty = await Navigator.of(context).push(
                          CupertinoPageRoute(
                              builder: (context) =>
                                  CustomList(selected: true)));
                      _addCustom(customDuty);
                    }),
              ),
              cargoTableTitleBox,
              Divider(
                color: Colors.grey[400],
              ),
              Column(
                children: getCargoRowList(),
              ),
              SizedBox(height: 30),
              saveBtn,
              SizedBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }

  _addCustom(CustomDuty customDuty) {
    if (customDuty == null) return;

    if (cargos.any((c) => c.name == customDuty.productType)) return;

    setState(() {
      cargos.add(CargoType(name: customDuty.productType, isCutomDuty: true));
    });
  }
}
