import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:built_collection/built_collection.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/redux/app/app_actions.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
import 'package:invoiceninja_flutter/utils/localization.dart';
import 'package:invoiceninja_flutter/utils/completers.dart';

class ViewStubList implements PersistUI {
  ViewStubList(this.context);

  final BuildContext context;
}

class ViewStub implements PersistUI {
  ViewStub({this.stubId, this.context});

  final int stubId;
  final BuildContext context;
}

class EditStub implements PersistUI {
  EditStub({this.stub, this.context, this.completer, this.trackRoute = true});

  final StubEntity stub;
  final BuildContext context;
  final Completer completer;
  final bool trackRoute;
}

class UpdateStub implements PersistUI {
  UpdateStub(this.stub);

  final StubEntity stub;
}

class LoadStub {
  LoadStub({this.completer, this.stubId, this.loadActivities = false});

  final Completer completer;
  final int stubId;
  final bool loadActivities;
}

class LoadStubActivity {
  LoadStubActivity({this.completer, this.stubId});

  final Completer completer;
  final int stubId;
}

class LoadStubs {
  LoadStubs({this.completer, this.force = false});

  final Completer completer;
  final bool force;
}

class LoadStubRequest implements StartLoading {}

class LoadStubFailure implements StopLoading {
  LoadStubFailure(this.error);

  final dynamic error;

  @override
  String toString() {
    return 'LoadStubFailure{error: $error}';
  }
}

class LoadStubSuccess implements StopLoading, PersistData {
  LoadStubSuccess(this.stub);

  final StubEntity stub;

  @override
  String toString() {
    return 'LoadStubSuccess{stub: $stub}';
  }
}

class LoadStubsRequest implements StartLoading {}

class LoadStubsFailure implements StopLoading {
  LoadStubsFailure(this.error);

  final dynamic error;

  @override
  String toString() {
    return 'LoadStubsFailure{error: $error}';
  }
}

class LoadStubsSuccess implements StopLoading, PersistData {
  LoadStubsSuccess(this.stubs);

  final BuiltList<StubEntity> stubs;

  @override
  String toString() {
    return 'LoadStubsSuccess{stubs: $stubs}';
  }
}


class SaveStubRequest implements StartSaving {
  SaveStubRequest({this.completer, this.stub});

  final Completer completer;
  final StubEntity stub;
}

class SaveStubSuccess implements StopSaving, PersistData, PersistUI {
  SaveStubSuccess(this.stub);

  final StubEntity stub;
}

class AddStubSuccess implements StopSaving, PersistData, PersistUI {
  AddStubSuccess(this.stub);

  final StubEntity stub;
}

class SaveStubFailure implements StopSaving {
  SaveStubFailure (this.error);

  final Object error;
}

class ArchiveStubRequest implements StartSaving {
  ArchiveStubRequest(this.completer, this.stubId);

  final Completer completer;
  final int stubId;
}

class ArchiveStubSuccess implements StopSaving, PersistData {
  ArchiveStubSuccess(this.stub);

  final StubEntity stub;
}

class ArchiveStubFailure implements StopSaving {
  ArchiveStubFailure(this.stub);

  final StubEntity stub;
}

class DeleteStubRequest implements StartSaving {
  DeleteStubRequest(this.completer, this.stubId);

  final Completer completer;
  final int stubId;
}

class DeleteStubSuccess implements StopSaving, PersistData {
  DeleteStubSuccess(this.stub);

  final StubEntity stub;
}

class DeleteStubFailure implements StopSaving {
  DeleteStubFailure(this.stub);

  final StubEntity stub;
}

class RestoreStubRequest implements StartSaving {
  RestoreStubRequest(this.completer, this.stubId);

  final Completer completer;
  final int stubId;
}

class RestoreStubSuccess implements StopSaving, PersistData {
  RestoreStubSuccess(this.stub);

  final StubEntity stub;
}

class RestoreStubFailure implements StopSaving {
  RestoreStubFailure(this.stub);

  final StubEntity stub;
}




class FilterStubs {
  FilterStubs(this.filter);

  final String filter;
}

class SortStubs implements PersistUI {
  SortStubs(this.field);

  final String field;
}

class FilterStubsByState implements PersistUI {
  FilterStubsByState(this.state);

  final EntityState state;
}

class FilterStubsByCustom1 implements PersistUI {
  FilterStubsByCustom1(this.value);

  final String value;
}

class FilterStubsByCustom2 implements PersistUI {
  FilterStubsByCustom2(this.value);

  final String value;
}

class FilterStubsByEntity implements PersistUI {
  FilterStubsByEntity({this.entityId, this.entityType});

  final int entityId;
  final EntityType entityType;
}


void handleStubAction(
    BuildContext context, StubEntity entity, EntityAction action) {
  final store = StoreProvider.of<AppState>(context);
  final state = store.state;
  final CompanyEntity company = state.selectedCompany;
  final localization = AppLocalization.of(context);

  switch (action) {
    case EntityAction.edit:
      store.dispatch(EditStub(context: context, stub: stub));
      break;
    case EntityAction.restore:
      store.dispatch(RestoreStubRequest(
          snackBarCompleter(context, localization.restoredStub), stub.id));
      break;
    case EntityAction.archive:
      store.dispatch(ArchiveStubRequest(
          snackBarCompleter(context, localization.archivedStub), stub.id));
      break;
    case EntityAction.delete:
      store.dispatch(DeleteStubRequest(
          snackBarCompleter(context, localization.deletedStub), stub.id));
      break;
  }
}