import 'package:fcs/domain/entities/cargo_type.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/progress.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'model/shipment_rate_model.dart';

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

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

class _CargoEditorState extends State<CargoEditor> {
  TextEditingController _descController = new TextEditingController();
  TextEditingController _rateController = new TextEditingController();

  bool _isLoading = false;
  CargoType _cargo;
  bool _isNew = false;

  @override
  void initState() {
    super.initState();
    if (widget.cargo != null) {
      _cargo = widget.cargo;
      _descController.text = _cargo.name;
      _rateController.text = _cargo.rate.toString();
    } else {
      _isNew = true;
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final typeBox = InputText(
        labelTextKey: 'cargo.type',
        iconData: Icons.text_format,
        controller: _descController);
    final rateBox = InputText(
        labelTextKey: 'cargo.rate',
        iconData: Icons.attach_money,
        controller: _rateController);
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(CupertinoIcons.back),
            onPressed: () => Navigator.of(context).pop(),
          ),
          backgroundColor: primaryColor,
          title: Text(AppTranslations.of(context).text("cargo.form.title")),
          actions: [
            IconButton(
              icon: Icon(Icons.delete),
              onPressed: _delete,
            )
          ],
        ),
        body: Container(
          padding: EdgeInsets.all(18),
          child: Column(
            children: <Widget>[
              Expanded(
                child: ListView(
                  children: <Widget>[
                    typeBox,
                    rateBox,
                    SizedBox(height: 30),
                  ],
                ),
              ),
              fcsButton(context, getLocalString(context, "btn.save"),
                  callack: _save),
              SizedBox(height: 10)
            ],
          ),
        ),
      ),
    );
  }

  _save() async {
    setState(() {
      _isLoading = true;
    });
    try {
      var shipmentRateModel =
          Provider.of<ShipmentRateModel>(context, listen: false);
      CargoType _cargo = CargoType(
          name: _descController.text, rate: double.parse(_rateController.text));
      if (_isNew) {
        await shipmentRateModel.addCargoType(_cargo);
      } else {
        _cargo.id = widget.cargo.id;
        await shipmentRateModel.updateCargoType(_cargo);
      }
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  _delete() {
    showConfirmDialog(context, "cargo.edit.delete.confirm", _deleteCargoType);
  }

  _deleteCargoType() async {
    setState(() {
      _isLoading = true;
    });
    try {
      var shipmentRateModel =
          Provider.of<ShipmentRateModel>(context, listen: false);
      await shipmentRateModel.deleteCargoType(widget.cargo.id);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }
}
