import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/local_dropdown.dart';
import 'package:fcs/pages/widgets/local_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

class CargoTypeEditor extends StatefulWidget {
  final CargoType cargo;
  CargoTypeEditor({this.cargo});

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

class _CargoTypeEditorState extends State<CargoTypeEditor> {
  TextEditingController _weightController = new TextEditingController();

  bool _isLoading = false;
  CargoType _cargo;

  @override
  void initState() {
    super.initState();
    if (widget.cargo != null) {
      _cargo = widget.cargo;
      _weightController.text = _cargo.weight.toString();
    } else {
      _loadDefalut();
    }
  }

  _loadDefalut() {
    ShipmentRateModel shipmentRateModel =
        Provider.of<ShipmentRateModel>(context, listen: false);
    _cargo = shipmentRateModel.rate.defaultCargoType.clone();
  }

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

  @override
  Widget build(BuildContext context) {
    ShipmentRateModel shipmentRateModel =
        Provider.of<ShipmentRateModel>(context);
    List<CargoType> cargos = shipmentRateModel.rate.cargoTypes;

    final rateBox = InputText(
        labelTextKey: 'cargo.weight',
        iconData: FontAwesomeIcons.weightHanging,
        textInputType: TextInputType.number,
        controller: _weightController);

    var cargoTypeBox = LocalDropdown<CargoType>(
      callback: (v) {
        setState(() {
          _cargo = v;
        });
      },
      labelKey: "cargo.type",
      iconData: Icons.text_format,
      selectedValue: _cargo,
      values: cargos,
    );

    final saveBtn = fcsButton(
      context,
      getLocalString(context, 'box.cargo.save.btn'),
      callack: () {
        _cargo.weight = double.tryParse(_weightController.text) ?? 0;
        Navigator.pop(context, _cargo);
      },
    );
    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(18),
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              cargoTypeBox,
              rateBox,
              SizedBox(height: 40),
              saveBtn,
              SizedBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }
}
