import { Selector } from '@ngxs/store';
import { ProcessStateModel, ProcessState } from '../state/process.state';
import { mapToIterable, getProperty } from '../../utils/app.utils';
import { AppState, AppStateModel } from '../state/app.state';
import { ModuleSwitcher } from '../../models/app-switcher.enum';
import { Process } from '../../models/process.model';

export class ProcessSelectors {
  @Selector([ProcessState])
  static getProcesses(state: ProcessStateModel) {
    return mapToIterable(state.processes);
  }

  @Selector([ProcessState])
  static getProcessesObject(state: ProcessStateModel) {
    return state.processes;
  }

  @Selector([ProcessState])
  static getProcessesCount(state: ProcessStateModel) {
    if (state.processes) {
      return mapToIterable(state.processes).length;
    }
    return 0;
  }

  @Selector([AppState, ProcessState])
  static getCurrentProcess(state: AppStateModel, processState: ProcessStateModel): Process {
    return processState.processes[state.currentProcesssId];
  }

  @Selector([AppState, ProcessState])
  static getCurrentProcessId(state: AppStateModel, processState: ProcessStateModel): number {
    return processState.processes[state.currentProcesssId].id;
  }

  @Selector([ProcessState])
  static getRecentProducts(state: ProcessStateModel) {
    return state.recentArticles;
  }

  @Selector([AppState, ProcessState])
  static getSelectedProduct(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess) {
      return currentProcess.selectedItem;
    }
  }

  @Selector([AppState, ProcessState])
  static getCurrentProcessCustomerNotificationFlag(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess) {
      return currentProcess.customerNotificationFlag;
    }
  }

  @Selector([AppState, ProcessState])
  static getScrollPositionForProduct(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess) {
      return currentProcess.productScrollTo;
    }
  }

  @Selector([AppState, ProcessState])
  static getActiveUserId(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess) {
      return currentProcess.activeCustomer;
    }
  }

  @Selector([AppState, ProcessState])
  static getDetailsUserId(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess && (!currentProcess.onlineCustomerCreationError || currentProcess.onlineCustomerCreationError.error === false)) {
      return currentProcess.detailsCustomer;
    }
  }

  @Selector([AppState, ProcessState])
  static getUniqueIdentifier(state: AppStateModel, processState: ProcessStateModel) {
    const currentProcess = processState.processes[state.currentProcesssId];
    if (currentProcess) {
      return currentProcess.uniqueIdentifier;
    }
  }

  @Selector([AppState, ProcessState])
  static getCustomerSearch(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].customerSearch;
  }

  @Selector([AppState, ProcessState])
  static getSearch(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].search;
  }

  @Selector([AppState, ProcessState])
  static getCustomerIds(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].customerIds;
  }

  @Selector([AppState, ProcessState])
  static getFinishedOrders(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].finishedOrder;
  }

  @Selector([AppState, ProcessState])
  static getArticleSearchErrorStatus(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].articleSearchErrorStatus;
  }

  @Selector([AppState, ProcessState])
  static getPreviousRoute(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].previousRoute;
  }

  @Selector([AppState, ProcessState])
  static getCurrentRoute(state: AppStateModel, processState: ProcessStateModel) {
    return getProperty(processState, `processes.${state.currentProcesssId}.currentRoute`);
  }

  @Selector([AppState, ProcessState])
  static getProductFilters(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].productSearchFilters;
  }

  @Selector([AppState, ProcessState])
  static getShelfSearch(state: AppStateModel, processState: ProcessStateModel) {
    return processState.processes[state.currentProcesssId].shelfSearch;
  }
}
