package de.ivu.elect.presentation;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Nullable;

import de.ivu.elect.business.mandant.entity.Mandant;

public class NlSystemProperties implements Serializable {

  private static final long serialVersionUID = 1L;


  public NlElectionLevel getDefaultElectionLevel(Mandant mandant) {
    NlElectionLevel nlElectionLevel = NlElectionLevel.UNBEKANNT;
    try {
      // ELECT-9903
      nlElectionLevel = NlElectionLevel.valueOf(System.getProperty("nl.election.level"));

    } catch (Exception e) {
      // ignorieren
    }

    if (nlElectionLevel.isLevelGesetzt()) {
      return nlElectionLevel;
    }

    // Fallback für Demo-System
    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return extrahiereElectionLevelAusMandantName(mandant.getName());
    }

    return nlElectionLevel;
  }

  private NlElectionLevel extrahiereElectionLevelAusMandantName(String name) {

    Set<NlElectionLevel>  result = new HashSet<>();

    try {
      for (String s : name.split("_")) {
        try {
          result.add(NlElectionLevel.valueOf(s));
        } catch (Exception e) {
          // ignore
        }
      }
    } catch (Exception e) {
      // ignore
    }

    if (result.isEmpty()) {
      return NlElectionLevel.UNBEKANNT;
    }

    return NlElectionLevel.kombiniere(result);
  }



  public boolean isPreventShutdown(@Nullable Mandant mandant) {
    if (Boolean.TRUE.toString().equalsIgnoreCase(System.getProperty("nl.prevent.shutdown"))) {
      return true;
    }

    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return true;
    }

    return false;
  }

}
