import 'package:redux/redux.dart';
import 'package:invoiceninja_flutter/data/models/models.dart';
import 'package:invoiceninja_flutter/redux/company/company_actions.dart';
import 'package:invoiceninja_flutter/redux/ui/entity_ui_state.dart';
import 'package:invoiceninja_flutter/redux/ui/list_ui_state.dart';
import 'package:invoiceninja_flutter/redux/stub/stub_actions.dart';
import 'package:invoiceninja_flutter/redux/stub/stub_state.dart';

EntityUIState stubUIReducer(StubUIState state, dynamic action) {
  return state.rebuild((b) => b
    ..listUIState.replace(stubListReducer(state.listUIState, action))
    ..editing.replace(editingReducer(state.editing, action))
    ..selectedId = selectedIdReducer(state.selectedId, action));
}

Reducer<int> selectedIdReducer = combineReducers([
  TypedReducer<int, ViewStub>(
      (int selectedId, dynamic action) => action.stubId),
  TypedReducer<int, AddStubSuccess>(
      (int selectedId, dynamic action) => action.stub.id),
]);

final editingReducer = combineReducers<StubEntity>([
  TypedReducer<StubEntity, SaveStubSuccess>(_updateEditing),
  TypedReducer<StubEntity, AddStubSuccess>(_updateEditing),
  TypedReducer<StubEntity, RestoreStubSuccess>(_updateEditing),
  TypedReducer<StubEntity, ArchiveStubSuccess>(_updateEditing),
  TypedReducer<StubEntity, DeleteStubSuccess>(_updateEditing),
  TypedReducer<StubEntity, EditStub>(_updateEditing),
  TypedReducer<StubEntity, UpdateStub>(_updateEditing),
  TypedReducer<StubEntity, SelectCompany>(_clearEditing),
]);

StubEntity _clearEditing(StubEntity stub, dynamic action) {
  return StubEntity();
}

StubEntity _updateEditing(StubEntity stub, dynamic action) {
  return action.stub;
}


final stubListReducer = combineReducers<ListUIState>([
  TypedReducer<ListUIState, SortStubs>(_sortStubs),
  TypedReducer<ListUIState, FilterStubsByState>(_filterStubsByState),
  TypedReducer<ListUIState, FilterStubs>(_filterStubs),
  TypedReducer<ListUIState, FilterStubsByCustom1>(_filterStubsByCustom1),
  TypedReducer<ListUIState, FilterStubsByCustom2>(_filterStubsByCustom2),
  TypedReducer<ListUIState, FilterStubsByEntity>(_filterStubsByClient),
]);

ListUIState _filterStubsByClient(
    ListUIState stubListState, FilterStubsByEntity action) {
  return stubListState.rebuild((b) => b
  ..filterEntityId = action.entityId
  ..filterEntityType = action.entityType);
}

ListUIState _filterStubsByCustom1(
    ListUIState stubListState, FilterStubsByCustom1 action) {
  if (stubListState.custom1Filters.contains(action.value)) {
    return stubListState
        .rebuild((b) => b..custom1Filters.remove(action.value));
  } else {
    return stubListState.rebuild((b) => b..custom1Filters.add(action.value));
  }
}

ListUIState _filterStubsByCustom2(
    ListUIState stubListState, FilterStubsByCustom2 action) {
  if (stubListState.custom2Filters.contains(action.value)) {
    return stubListState
        .rebuild((b) => b..custom2Filters.remove(action.value));
  } else {
    return stubListState.rebuild((b) => b..custom2Filters.add(action.value));
  }
}

ListUIState _filterStubsByState(
    ListUIState stubListState, FilterStubsByState action) {
  if (stubListState.stateFilters.contains(action.state)) {
    return stubListState.rebuild((b) => b..stateFilters.remove(action.state));
  } else {
    return stubListState.rebuild((b) => b..stateFilters.add(action.state));
  }
}

ListUIState _filterStubs(ListUIState stubListState, FilterStubs action) {
  return stubListState.rebuild((b) => b..filter = action.filter
  ..filterClearedAt = action.filter == null
       ? DateTime.now().millisecondsSinceEpoch
       : stubListState.filterClearedAt);
}

ListUIState _sortStubs(ListUIState stubListState, SortStubs action) {
  return stubListState.rebuild((b) => b
    ..sortAscending = b.sortField != action.field || !b.sortAscending
    ..sortField = action.field);
}

final stubsReducer = combineReducers<StubState>([
  TypedReducer<StubState, SaveStubSuccess>(_updateStub),
  TypedReducer<StubState, AddStubSuccess>(_addStub),
  TypedReducer<StubState, LoadStubsSuccess>(_setLoadedStubs),
  TypedReducer<StubState, LoadStubSuccess>(_setLoadedStub),
  TypedReducer<StubState, ArchiveStubRequest>(_archiveStubRequest),
  TypedReducer<StubState, ArchiveStubSuccess>(_archiveStubSuccess),
  TypedReducer<StubState, ArchiveStubFailure>(_archiveStubFailure),
  TypedReducer<StubState, DeleteStubRequest>(_deleteStubRequest),
  TypedReducer<StubState, DeleteStubSuccess>(_deleteStubSuccess),
  TypedReducer<StubState, DeleteStubFailure>(_deleteStubFailure),
  TypedReducer<StubState, RestoreStubRequest>(_restoreStubRequest),
  TypedReducer<StubState, RestoreStubSuccess>(_restoreStubSuccess),
  TypedReducer<StubState, RestoreStubFailure>(_restoreStubFailure),
]);

StubState _archiveStubRequest(
    StubState stubState, ArchiveStubRequest action) {
  final stub = stubState.map[action.stubId]
      .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch);

  return stubState.rebuild((b) => b..map[action.stubId] = stub);
}

StubState _archiveStubSuccess(
    StubState stubState, ArchiveStubSuccess action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _archiveStubFailure(
    StubState stubState, ArchiveStubFailure action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _deleteStubRequest(
    StubState stubState, DeleteStubRequest action) {
  final stub = stubState.map[action.stubId].rebuild((b) => b
    ..archivedAt = DateTime.now().millisecondsSinceEpoch
    ..isDeleted = true);

  return stubState.rebuild((b) => b..map[action.stubId] = stub);
}

StubState _deleteStubSuccess(
    StubState stubState, DeleteStubSuccess action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _deleteStubFailure(
    StubState stubState, DeleteStubFailure action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _restoreStubRequest(
    StubState stubState, RestoreStubRequest action) {
  final stub = stubState.map[action.stubId].rebuild((b) => b
    ..archivedAt = null
    ..isDeleted = false);
  return stubState.rebuild((b) => b..map[action.stubId] = stub);
}

StubState _restoreStubSuccess(
    StubState stubState, RestoreStubSuccess action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _restoreStubFailure(
    StubState stubState, RestoreStubFailure action) {
  return stubState.rebuild((b) => b..map[action.stub.id] = action.stub);
}

StubState _addStub(StubState stubState, AddStubSuccess action) {
  return stubState.rebuild((b) => b
    ..map[action.stub.id] = action.stub
    ..list.add(action.stub.id));
}

StubState _updateStub(StubState stubState, SaveStubSuccess action) {
  return stubState.rebuild((b) => b
    ..map[action.stub.id] = action.stub);
}

StubState _setLoadedStub(
    StubState stubState, LoadStubSuccess action) {
  return stubState.rebuild((b) => b
    ..map[action.stub.id] = action.stub);
}

StubState _setLoadedStubs(
    StubState stubState, LoadStubsSuccess action) {
  final state = stubState.rebuild((b) => b
    ..lastUpdated = DateTime.now().millisecondsSinceEpoch
    ..map.addAll(Map.fromIterable(
      action.stubs,
      key: (dynamic item) => item.id,
      value: (dynamic item) => item,
    )));

  return state.rebuild((b) => b..list.replace(state.map.keys));
}
