import 'dart:async';
import 'package:invoiceninja_flutter/ui/app/snackbar_row.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:invoiceninja_flutter/redux/ui/ui_actions.dart';
import 'package:invoiceninja_flutter/utils/completers.dart';
import 'package:invoiceninja_flutter/ui/stub/stub_screen.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';
import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:invoiceninja_flutter/redux/stub/stub_actions.dart';
import 'package:invoiceninja_flutter/data/models/stub_model.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/ui/stub/view/stub_view.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';

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

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, StubViewVM>(
      converter: (Store<AppState> store) {
        return StubViewVM.fromStore(store);
      },
      builder: (context, vm) {
        return StubView(
          viewModel: vm,
        );
      },
    );
  }
}

class StubViewVM {

  StubViewVM({
    @required this.state,
    @required this.stub,
    @required this.company,
    @required this.onEntityAction,
    @required this.onEditPressed,
    @required this.onBackPressed,
    @required this.onRefreshed,
    @required this.isSaving,
    @required this.isLoading,
    @required this.isDirty,
  });

  factory StubViewVM.fromStore(Store<AppState> store) {
    final state = store.state;
    final stub = state.stubState.map[state.stubUIState.selectedId] ??
        StubEntity(id: state.stubUIState.selectedId);

    Future<Null> _handleRefresh(BuildContext context) {
      final completer = snackBarCompleter(
          context, AppLocalization.of(context).refreshComplete);
      store.dispatch(LoadStub(completer: completer, stubId: stub.id));
      return completer.future;
    }

    return StubViewVM(
        state: state,
        company: state.selectedCompany,
        isSaving: state.isSaving,
        isLoading: state.isLoading,
        isDirty: stub.isNew,
        stub: stub,
        onEditPressed: (BuildContext context) {
          final Completer<StubEntity> completer = Completer<StubEntity>();
          store.dispatch(EditStub(
              stub: stub, context: context, completer: completer));
          completer.future.then((stub) {
            Scaffold.of(context).showSnackBar(SnackBar(
                content: SnackBarRow(
                  message: AppLocalization.of(context).updatedStub,
                )));
          });
        },
        onRefreshed: (context) => _handleRefresh(context),
        onBackPressed: () {
          if (state.uiState.currentRoute.contains(StubScreen.route)) {
            store.dispatch(UpdateCurrentRoute(StubScreen.route));
          }
        },
      onEntityAction: (BuildContext context, EntityAction action) =>
          handleStubAction(context, stub, action),
        );
  }

  final AppState state;
  final StubEntity stub;
  final CompanyEntity company;
  final Function(BuildContext, EntityAction) onEntityAction;
  final Function(BuildContext) onEditPressed;
  final Function onBackPressed;
  final Function(BuildContext) onRefreshed;
  final bool isSaving;
  final bool isLoading;
  final bool isDirty;
}
