package de.ivu.elect.presentation.system;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;
import de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte;
import de.ivu.elect.business.mandant.boundary.MandantExtendedDAO;
import de.ivu.elect.business.mandant.entity.AbstractMandant;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;

@RequestScoped
@Named(SystemMessage.COMPONENT_NAME)
public class SystemMessage {

  public static final String COMPONENT_NAME = "systemMessage";

  private String systemMessage;

  private String mandantMessage;

  @Inject
  private transient ElectCommonPrincipal electCommonPrincipal;

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  @Inject
  private transient MandantExtendedDAO mandantExtendedDAO;

  public boolean isSystemMessagePresent() {
    return StringUtils.isNotBlank(getSystemMessage());
  }

  public String getSystemMessage() {
    if (systemMessage == null) {
      systemMessage = konfigurationLoader.resolveStringValue(AbstractMandant.getSystemMandant(mandantExtendedDAO), MandantKonfigurationWerte.SYSTEM_MESSAGE);
      if (StringUtils.isBlank(systemMessage)) {
        systemMessage = "";
      }
    }
    return systemMessage;
  }

  public boolean isMandantMessagePresent() {
    return StringUtils.isNotBlank(getMandantMessage());
  }

  public String getMandantMessage() {
    if (mandantMessage == null) {
      if (electCommonPrincipal.isLoggedIn() && !electCommonPrincipal.getMandant().getArt().isSystemMandant()) {
        mandantMessage = konfigurationLoader.resolveStringValue(electCommonPrincipal.getMandant(), MandantKonfigurationWerte.SYSTEM_MESSAGE);
      }
      if (StringUtils.isBlank(mandantMessage)) {
        mandantMessage = "";
      }
    }

    return mandantMessage;
  }

}
