
package de.ivu.elect.business.security.control;

import java.io.Serializable;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.jee.faces.DevelopmentStore;

public class NlAdminRolleVorhanden implements Serializable {

  private static final Logger LOG = LoggerFactory.getLogger(NlAdminRolleVorhanden.class);

  private static final long serialVersionUID = 1L;

  public static final String ADMIN_AUSNAHME_PROPERTY = "elect.nl.admin.ausnahme"; // https://confluence.ivu.de/display/ELECT/VAPP+-+Zauber-Systemproperties

  public boolean isAdminRolleVorhanden(Mandant mandant) {
    if (mandant.getArt().isSystemMandant() && !mandant.getArt().isCustomerMandant()) {
      LOG.info("Mandant: " + mandant + " - Systemmandant - Admin-Rolle anlegen");
      return true; // System-Mandant nur für Demosystem relevant
    }

    LOG.info("Mandant: " + mandant + " - isDevelopmentMode: " + isDevelopmentMode());
    LOG.info("Mandant: " + mandant + " - isMandantAlsAusnahmeDeklariert: " + isMandantAlsAusnahmeDeklariert(mandant));

    return isDevelopmentMode() || isMandantAlsAusnahmeDeklariert(mandant);
  }

  private boolean isDevelopmentMode() {
    return DevelopmentStore.isDevelopmentStage();
  }

  private boolean isMandantAlsAusnahmeDeklariert(Mandant mandant) {
    try {
      return Arrays.asList(System.getProperty(ADMIN_AUSNAHME_PROPERTY).split(",")).contains(Long.toString(mandant.getNummer()));
    } catch (Exception e) {
      return false;
    }
  }
}
