import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.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';

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

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

  List<CargoType> cargos = [];
  List<CargoType> specialCargos = [];

  @override
  void initState() {
    super.initState();
    var shipmentRateModel =
        Provider.of<ShipmentRateModel>(context, listen: false);

    cargos = shipmentRateModel.rate.cargoTypes.map((e) => e.clone()).toList();
    specialCargos = List.from(shipmentRateModel.rate.customDuties);
    specialCargos =
        shipmentRateModel.rate.customDuties.map((e) => e.clone()).toList();

    cargos.forEach((p) {
      p.isChecked = false;
      p.isCutomDuty = false;
      p.weight = 0;
      p.qty = 0;
    });
    specialCargos.forEach((p) {
      p.isChecked = false;
      p.isCutomDuty = true;
      p.weight = 0;
      p.qty = 1;
    });
  }

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

  @override
  Widget build(BuildContext context) {
    List<Widget> getCargoRowList(List<CargoType> _c) {
      return _c.map((c) {
        return Container(
          child: Container(
            padding:
                EdgeInsets.only(left: 10.0, right: 5.0, top: 3.0, bottom: 3.0),
            child: InkWell(
              onTap: () {
                setState(() {
                  c.isChecked = !c.isChecked;
                });
              },
              child: Row(
                children: <Widget>[
                  Checkbox(
                      value: c.isChecked,
                      activeColor: primaryColor,
                      onChanged: (bool? check) {
                        setState(() {
                          c.isChecked = check ?? false;
                        });
                      }),
                  new Text(c.name ?? '', style: textStyle),
                ],
              ),
            ),
          ),
        );
      }).toList();
    }

    final saveBtn = fcsButton(
      context,
      getLocalString(context, 'box.cargo.select.btn'),
      callack: () {
        List<CargoType> _cargos =
            this.cargos.where((c) => c.isChecked).toList();
        List<CargoType> _scargos =
            this.specialCargos.where((c) => c.isChecked).toList();
        _cargos.addAll(_scargos);
        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",
              ),
              Column(
                children: getCargoRowList(cargos),
              ),
              LocalTitle(
                textKey: "box.select.cargo.title",
              ),
              Column(
                children: getCargoRowList(specialCargos),
              ),
              SizedBox(height: 30),
              saveBtn,
              SizedBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }
}
