import 'package:fcs/domain/entities/carton_size.dart';
import 'package:fcs/helpers/theme.dart';
import 'package:fcs/pages/main/util.dart';
import 'package:fcs/pages/widgets/input_text.dart';
import 'package:fcs/pages/widgets/length_picker.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:flutter_icons/flutter_icons.dart';
import 'package:provider/provider.dart';

import 'model/carton_size_model.dart';

class CartonSizeEditor extends StatefulWidget {
  final CartonSize cartonSize;
  CartonSizeEditor({this.cartonSize});

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

class _CartonSizeEditorState extends State<CartonSizeEditor> {
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _widthController = new TextEditingController();
  TextEditingController _heightController = new TextEditingController();
  TextEditingController _lengthController = new TextEditingController();

  bool _isLoading = false;
  CartonSize _cartonSize;
  bool _isNew;

  @override
  void initState() {
    super.initState();
    if (widget.cartonSize != null) {
      _cartonSize = widget.cartonSize;
      _isNew = false;
      _nameController.text = _cartonSize.name;
      _widthController.text = _cartonSize.width.toString();
      _heightController.text = _cartonSize.height.toString();
      _lengthController.text = _cartonSize.length.toString();
    } else {
      _lengthController.text = "12";
      _widthController.text = "12";
      _heightController.text = "12";
      _isNew = true;
    }
  }

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

  @override
  Widget build(BuildContext context) {
    final nameBox = InputText(
        labelTextKey: 'box.carton_size.name',
        iconData: Icons.text_format,
        controller: _nameController);

    final lengthBox = LengthPicker(
      controller: _lengthController,
      lableKey: "box.length",
    );
    final widthBox = LengthPicker(
      controller: _widthController,
      lableKey: "box.width",
    );
    final heightBox = LengthPicker(
      controller: _heightController,
      lableKey: "box.height",
    );

    final dimBox = Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(right: 8.0),
          child: Icon(FontAwesome.arrow_circle_right, color: primaryColor),
        ),
        SizedBox(child: lengthBox, width: 80),
        SizedBox(child: widthBox, width: 80),
        SizedBox(child: heightBox, width: 80),
      ],
    );

    final saveBtn = fcsButton(
      context,
      getLocalString(context, 'box.cargo.save.btn'),
      callack: () async {
        if (_nameController.text == "") {
          showMsgDialog(context, "Esrror", "Invalid carton size name!");
          return;
        }
        setState(() {
          _isLoading = true;
        });
        CartonSizeModel cartonSizeModel =
            Provider.of<CartonSizeModel>(context, listen: false);
        try {
          double l = double.parse(_lengthController.text, (s) => 0);
          double w = double.parse(_widthController.text, (s) => 0);
          double h = double.parse(_heightController.text, (s) => 0);

          if (_isNew) {
            CartonSize _cartonSize = CartonSize(
                name: _nameController.text, length: l, width: w, height: h);
            await cartonSizeModel.addCartonSize(_cartonSize);
          } else {
            CartonSize _cartonSize = CartonSize(
                id: widget.cartonSize.id,
                name: _nameController.text,
                length: l,
                width: w,
                height: h);
            await cartonSizeModel.updateCartonSize(_cartonSize);
          }

          Navigator.pop(context);
        } catch (e) {
          showMsgDialog(context, "Error", e.toString());
        } finally {
          setState(() {
            _isLoading = false;
          });
        }
      },
    );
    return LocalProgress(
      inAsyncCall: _isLoading,
      child: Scaffold(
        appBar: AppBar(
            centerTitle: true,
            leading: new IconButton(
              icon:
                  new Icon(CupertinoIcons.back, color: primaryColor, size: 30),
              onPressed: () {
                if (isDataChanged()) {
                  showConfirmDialog(context, "back.button_confirm", () {
                    Navigator.of(context).pop();
                  });
                } else {
                  Navigator.of(context).pop();
                }
              },
            ),
            shadowColor: Colors.transparent,
            backgroundColor: Colors.white,
            title: LocalText(
              context,
              "box.carton_size",
              fontSize: 20,
              color: primaryColor,
            )),
        body: Container(
          padding: EdgeInsets.all(18),
          child: ListView(
            shrinkWrap: true,
            children: <Widget>[
              nameBox,
              dimBox,
              SizedBox(height: 40),
              saveBtn,
              SizedBox(height: 20),
            ],
          ),
        ),
      ),
    );
  }

  isDataChanged() {
    if (_isNew) {
      return _nameController.text != "" ||
          _lengthController.text != "12" ||
          _widthController.text != "12" ||
          _heightController.text != "12";
    } else {
      double l = double.parse(_lengthController.text, (s) => 0);
      double w = double.parse(_widthController.text, (s) => 0);
      double h = double.parse(_heightController.text, (s) => 0);
      CartonSize _cartonSize = CartonSize(
          name: _nameController.text, length: l, width: w, height: h);

      return widget.cartonSize.isChangedForEdit(_cartonSize);
    }
  }
}
