import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:invoiceninja_flutter/ui/app/form_card.dart';
import 'package:invoiceninja_flutter/ui/stub/edit/stub_edit_vm.dart';
import 'package:invoiceninja_flutter/ui/app/buttons/action_icon_button.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';

class StubEdit extends StatefulWidget {
  const StubEdit({
    Key key,
    @required this.viewModel,
  }) : super(key: key);

  final StubEditVM viewModel;

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

class _StubEditState extends State<StubEdit> {
  static final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  // STARTER: controllers - do not remove comment

  List<TextEditingController> _controllers = [];

  @override
  void didChangeDependencies() {

    _controllers = [
      // STARTER: array - do not remove comment
    ];

    _controllers.forEach((controller) => controller.removeListener(_onChanged));

    final stub = widget.viewModel.stub;
    // STARTER: read value - do not remove comment

    _controllers.forEach((controller) => controller.addListener(_onChanged));

    super.didChangeDependencies();
  }

  @override
  void dispose() {
    _controllers.forEach((controller) {
      controller.removeListener(_onChanged);
      controller.dispose();
    });

    super.dispose();
  }

  void _onChanged() {
    final stub = widget.viewModel.stub.rebuild((b) => b
      // STARTER: set value - do not remove comment
    );
    if (stub != widget.viewModel.stub) {
      widget.viewModel.onChanged(stub);
    }
  }

  @override
  Widget build(BuildContext context) {
    final viewModel = widget.viewModel;
    final localization = AppLocalization.of(context);
    final stub = viewModel.stub;

    return WillPopScope(
      onWillPop: () async {
        viewModel.onBackPressed();
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(viewModel.stub.isNew
              ? localization.newStub
              : localization.editStub),
          actions: <Widget>[
                ActionIconButton(
                  icon: Icons.cloud_upload,
                  tooltip: localization.save,
                  isVisible: !stub.isDeleted,
                  isDirty: stub.isNew || stub != viewModel.origStub,
                  isSaving: viewModel.isSaving,
                  onPressed: () {
                    if (! _formKey.currentState.validate()) {
                      return;
                    }
                    viewModel.onSavePressed(context);
                  },
                ),
          ],
        ),
        body: Form(
            key: _formKey,
            child: Builder(builder: (BuildContext context) {
              return ListView(
                children: <Widget>[
                  FormCard(
                    children: <Widget>[
                      // STARTER: widgets - do not remove comment
                    ],
                  ),
                ],
              );
            })
        ),
      ),
    );
  }
}
