import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:invoiceninja_flutter/redux/app/app_actions.dart';
import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart';
import 'package:invoiceninja_flutter/ui/stub/stub_screen.dart';
import 'package:invoiceninja_flutter/utils/platforms.dart';
import 'package:redux/redux.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/ui/app/dialogs/error_dialog.dart';
import 'package:invoiceninja_flutter/ui/stub/view/stub_view_vm.dart';
import 'package:invoiceninja_flutter/redux/stub/stub_actions.dart';
import 'package:invoiceninja_flutter/data/models/stub_model.dart';
import 'package:invoiceninja_flutter/ui/stub/edit/stub_edit.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';

class StubEditScreen extends StatelessWidget {
  const StubEditScreen({Key key}) : super(key: key);
  static const String route = '/stub/edit';

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, StubEditVM>(
      converter: (Store<AppState> store) {
        return StubEditVM.fromStore(store);
      },
      builder: (context, viewModel) {
        return StubEdit(
          viewModel: viewModel,
          key: ValueKey(viewModel.stub.id),
        );
      },
    );
  }
}

class StubEditVM {
  StubEditVM({
    @required this.state,
    @required this.stub,
    @required this.company,
    @required this.onChanged,
    @required this.isSaving,
    @required this.origStub,
    @required this.onSavePressed,
    @required this.onCancelPressed,
    @required this.isLoading,
  });

  factory StubEditVM.fromStore(Store<AppState> store) {
    final state = store.state;
    final stub = state.stubUIState.editing;

    return StubEditVM(
      state: state,
      isLoading: state.isLoading,
      isSaving: state.isSaving,
      origStub: state.stubState.map[stub.id],
      stub: stub,
      company: state.company,
      onChanged: (StubEntity stub) {
        store.dispatch(UpdateStub(stub));
      },
    onCancelPressed: (BuildContext context) {
      createEntity(context: context, entity: StubEntity(), force: true);
    },
      onSavePressed: (BuildContext context) {
        final Completer<StubEntity> completer = new Completer<StubEntity>();
        store.dispatch(SaveStubRequest(completer: completer, stub: stub));
        return completer.future.then((savedStub) {
          if (isMobile(context)) {
                store.dispatch(UpdateCurrentRoute(StubViewScreen.route));
              if (stub.isNew) {
                Navigator.of(context).pushReplacementNamed(StubViewScreen.route);
              } else {
                Navigator.of(context).pop(savedStub);
              }
          } else {
              viewEntity(context: context, entity: savedStub, force: true);
          }
        }).catchError((Object error) {
          showDialog<ErrorDialog>(
              context: context,
              builder: (BuildContext context) {
                return ErrorDialog(error);
              });
        });
      },
    );
  }

  final StubEntity stub;
  final CompanyEntity company;
  final Function(StubEntity) onChanged;
  final Function(BuildContext) onSavePressed;
  final Function(BuildContext) onCancelPressed;
  final bool isLoading;
  final bool isSaving;
  final StubEntity origStub;
  final AppState state;
}
