import 'package:fcs/domain/entities/rate.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/localization/app_translations.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.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:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

import '../main/util.dart';

class ShipmentRatesEdit extends StatefulWidget {
  ShipmentRatesEdit();

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

class _ShipmentRatesEditState extends State<ShipmentRatesEdit> {
  bool _isLoading = false;
  TextEditingController _minWeight = new TextEditingController();
  TextEditingController _deliveryFee = new TextEditingController();
  TextEditingController _volumetricRatio = new TextEditingController();
  TextEditingController _diffDiscountWeight = new TextEditingController();
  TextEditingController _diffWeightRate = new TextEditingController();

  late Rate rate;

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

    _minWeight.text = rate.freeDeliveryWeight.toStringAsFixed(2);
    _deliveryFee.text = rate.deliveryFee.toStringAsFixed(2);
    _volumetricRatio.text = rate.volumetricRatio.toStringAsFixed(2);
    _diffDiscountWeight.text = rate.diffDiscountWeight.toStringAsFixed(2);
    _diffWeightRate.text = rate.diffWeightRate.toStringAsFixed(2);
  }

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

  @override
  Widget build(BuildContext context) {
    var shipmentRateModel = Provider.of<ShipmentRateModel>(context);

    final minWigBox = InputText(
        labelTextKey: 'rate.min_weight',
        iconData: FontAwesomeIcons.weightHanging,
        controller: _minWeight);
    final feeBox = InputText(
        labelTextKey: 'rate.delivery_fee',
        iconData: Icons.attach_money,
        controller: _deliveryFee);
    final ratioBox = InputText(
        labelTextKey: 'rate.volumetric_ratio',
        iconData: FontAwesomeIcons.weightHanging,
        controller: _volumetricRatio);
    final diffDiscountWeightBox = InputText(
        labelTextKey: 'rate.diff_discount_weight',
        iconData: FontAwesomeIcons.weightHanging,
        controller: _diffDiscountWeight);
    final diffWeightRateBox = InputText(
        labelTextKey: 'rate.diff_weight_rate',
        iconData: Icons.attach_money,
        controller: _diffWeightRate);

    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          leading: new IconButton(
            icon: new Icon(
              CupertinoIcons.back,
            ),
            onPressed: () {
              if (isDataChanged()) {
                showConfirmDialog(context, "back.button_confirm", () {
                  Navigator.of(context).pop();
                });
              } else {
                Navigator.of(context).pop();
              }
            },
          ),
          backgroundColor: primaryColor,
          title: Text(AppTranslations.of(context)!.text("rate.edit.title")),
        ),
        body: Container(
          padding: EdgeInsets.all(18),
          child: Column(
            children: <Widget>[
              Expanded(
                child: ListView(
                  children: <Widget>[
                    minWigBox,
                    feeBox,
                    ratioBox,
                    diffDiscountWeightBox,
                    diffWeightRateBox,
                    SizedBox(height: 10),
                  ],
                ),
              ),
              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);
      Rate _rate = new Rate(
          deliveryFee: double.parse(_deliveryFee.text),
          freeDeliveryWeight: double.parse(_minWeight.text),
          volumetricRatio: double.parse(_volumetricRatio.text),
          diffDiscountWeight: double.parse(_diffDiscountWeight.text),
          diffWeightRate: double.parse(_diffWeightRate.text));
      Rate r = new Rate();
      print('_rate =>$r');
      await shipmentRateModel.updateRate(_rate);
      Navigator.pop(context);
    } catch (e) {
      showMsgDialog(context, "Error", e.toString());
    } finally {
      setState(() {
        _isLoading = false;
      });
    }
  }

  isDataChanged() {
    Rate _rate = new Rate(
      deliveryFee: double.parse(_deliveryFee.text),
      freeDeliveryWeight: double.parse(_minWeight.text),
      volumetricRatio: double.parse(_volumetricRatio.text),
      diffDiscountWeight: double.parse(_diffDiscountWeight.text),
      diffWeightRate: double.parse(_diffWeightRate.text),
    );
    return rate.isChangedForEdit(_rate);
  }
}
