package de.ivu.elect.business.wahl.entity;

import de.ivu.elect.business.security.Customer;

import java.util.*;
import java.util.stream.Collectors;

import static de.ivu.elect.business.security.Customer.WVZ;
import static java.util.Arrays.asList;

public enum WahlterminCopyEntity {

  GEBIETE(WVZ),
  ANSCHRIFTEN_EINTRAEGE(WVZ),
  WAHLRECHTSKRITERIEN(WVZ),
  WAHLSTATISTIKKENNZEICHEN(WVZ),
  STIMMZETTELGEBIETE(WVZ);

  private final Set<Customer> customers;

  WahlterminCopyEntity(Customer... customers) {
    if (customers == null || customers.length == 0) {
      this.customers = Collections.emptySet();
    } else {
      this.customers = EnumSet.copyOf/*ELECT-16561*/(asList(customers));
    }
  }

  public static List<WahlterminCopyEntity> getEntitiesForCustomer(Customer customer) {
    return Arrays.stream(WahlterminCopyEntity.values())
        .filter(entity -> isFuerCustomer(entity, customer))
        .collect(Collectors.toList());
  }

  private static boolean isFuerCustomer(WahlterminCopyEntity entity, Customer customer) {
    return entity.customers.contains(customer) || customer.getParents().stream().anyMatch(parentCustomer -> isFuerCustomer(entity, parentCustomer));
  }

}
