import { Selector } from '@ngxs/store';
import { RemissionState, RemissionStateModel } from '../state/remission.state';
import { RemissionProcessStatuses } from '../../models/remission-process-statuses.model';
import { isNullOrUndefined } from 'util';
import { RemissionResourceType } from '../../../modules/remission/models/remission-resource-type.model';
import { RemissionFinishingProcessStatus } from '../../../modules/remission/models/remission-finishing-process-status.enum';
import { RemissionSourceType } from '@isa/remission';

export class RemissionSelectors {
  @Selector([RemissionState])
  static getRemissionProcessStatuses(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    if (!isNullOrUndefined(remission)) {
      return <RemissionProcessStatuses>{
        created: remission.remissionProcessCreated,
        started: remission.remissionProcessStarted,
        completed: remission.remissionProcessCompleted,
      };
    }
  }

  @Selector([RemissionState])
  static getRemissionProcess(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.remissionProcess;
  }

  @Selector([RemissionState])
  static getRemissionShippingDocument(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.shippingDocument;
  }

  @Selector([RemissionState])
  static getRemissionCompleatedStatus(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.remissionProcessCompleted;
  }

  @Selector([RemissionState])
  static getRemissionStartedStatus(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.remissionProcessStarted;
  }

  @Selector([RemissionState])
  static getRemissionFinishingProcessStatus(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.remissionFinishingProcessStatus;
  }

  @Selector([RemissionState])
  static getRemissionContainerId(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.containerId;
  }

  @Selector([RemissionState])
  static shouldRemindForRemission(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return (
      remission.remissionProcessStarted &&
      !remission.remissionProcessCompleted &&
      !remission.blockReminder &&
      remission.remissionFinishingProcessStatus !== RemissionFinishingProcessStatus.containerScanned
    );
  }

  @Selector([RemissionState])
  static getRemissionShippingDocumentstatus(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.shippingDocumentCreated;
  }

  @Selector([RemissionState])
  static getRemissionShippingDocumentProductCount(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.shippingDocument ? remission.shippingDocument.products.length : 0;
  }

  @Selector([RemissionState])
  static getRemissionSelectedGroups(filterId = 'abteilungen') {
    return (state) => {
      const { remission } = state.remission;
      if (remission && remission.remissionProcess) {
        const source = remission.source;
        const target = remission.target;
        return remission.remissionProcess[source][target][filterId].filterGroups;
      }
    };
  }

  @Selector([RemissionState])
  static getRemissionSelectedOptions(remissionState: RemissionStateModel): string[] {
    const remission = remissionState.remission;
    if (remission && remission.remissionProcess) {
      const filter = remission.remissionProcess.filter;
      const source = filter.source;
      const target = filter.target.name;
      return filter.filter[source][target].abteilungen;
    }
  }

  @Selector([RemissionState])
  static getRemissionSelectedFilters(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    if (remission && remission.remissionProcess) {
      const filter = remission.remissionProcess.filter;
      const source = filter.source;
      const target = filter.target.name;
      const filters = remission.remissionProcess.filter.filter[source][target];

      if (source === RemissionResourceType.Overflow) {
        return filters.abteilungen;
      } else {
        const centralSelectedAktionenFilters = filters.aktionen || [];
        return [...filters.abteilungen, ...centralSelectedAktionenFilters];
      }
    }
  }

  @Selector([RemissionState])
  static getRemissionActiveFilters(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;

    if (remission.remissionProcess) {
      const filter = remission.remissionProcess.filter;
      const source = filter.source;
      const target = filter.target.name;

      return remission.remissionProcess.filter.filter[source][target];
    }
  }

  @Selector([RemissionState])
  static getRemissionFilters(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    if (remission && remission.remissionProcess) {
      const { filter } = remission.remissionProcess.filter;
      return filter;
    }
  }

  @Selector([RemissionState])
  static getRemissiontarget(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    if (remission) {
      return remission.target;
    }
  }

  @Selector([RemissionState])
  static getRemissionTargetFilter(source: RemissionSourceType) {
    return (state) => {
      const { remission } = state.remission;
      if (remission) {
        if (source === RemissionResourceType.Overflow) {
          return remission.overflowTarget;
        } else {
          return remission.centralTarget;
        }
      }
    };
  }

  @Selector([RemissionState])
  static getRemissionSource(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.source;
  }

  @Selector([RemissionState])
  static getRemissionSearchedProduct(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.searchedProduct;
  }

  @Selector([RemissionState])
  static getAllRemissions(remissionState: RemissionStateModel) {
    const existingRemissions = remissionState.existingRemissions;
    return existingRemissions.allRemissions;
  }

  @Selector([RemissionState])
  static getAllUncompletedRemissions(remissionState: RemissionStateModel) {
    const existingRemissions = remissionState.existingRemissions;
    return existingRemissions.openRemissions;
  }

  @Selector([RemissionState])
  static getIsLoading(remissionState: RemissionStateModel) {
    return remissionState.isLoading;
  }

  @Selector([RemissionState])
  static getRequiredCapacities(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    return remission.remissionProcess.capacities;
  }

  @Selector([RemissionState])
  static allowScanningShippingDocument(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;

    return remission.target === 'Blank';
  }

  @Selector([RemissionState])
  static getActiveView(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;

    return remission.activeView;
  }

  @Selector([RemissionState])
  static getRemissionSupplierId(remissionState: RemissionStateModel) {
    const remission = remissionState.remission;
    const supplier = remission.remissionProcess && remission.remissionProcess.filter && remission.remissionProcess.filter.target;

    return supplier && supplier.id;
  }
}
