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

import java.io.File;

import javax.annotation.Nullable;
import javax.persistence.NoResultException;

import de.ivu.elect.business.konfiguration.boundary.AbstractKonfigurationDAO;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.jee.exception.ElectCommonException;

public abstract class AbstractKonfigurationLoader
    extends
    AbstractCustomParameterLoader<ElectSystemMandantKonfigurationWerte, Konfiguration, KonfigurationLoaderContext> {

  private static final long serialVersionUID = 1L;

  public AbstractKonfigurationLoader() {
    super();
  }

  public void setValue(ElectKonfigurationWerte template, String wert) {
    setValue(createSystemMandantContext(template), wert);
  }

  public String resolveRequiredStringValue(ElectKonfigurationWerte template) {
    return resolveRequiredStringValue(createSystemMandantContext(template));
  }

  public String resolveStringValue(ElectKonfigurationWerte template) {
    return resolveStringValue(createSystemMandantContext(template));
  }

  public long resolveLongValue(ElectKonfigurationWerte template) {
    return resolveLongValue(createSystemMandantContext(template));
  }

  @Nullable
  public Long resolveLongValueOptional(ElectKonfigurationWerte template) {
    return resolveLongValueOptional(createSystemMandantContext(template));
  }

  public int resolveIntegerValue(ElectKonfigurationWerte template) {
    return resolveIntegerValue(createSystemMandantContext(template));
  }

  public int resolveIntegerValue(ElectKonfigurationWerte template, int defaultValue) {
    return resolveIntegerValue(createSystemMandantContext(template), defaultValue);
  }

  public boolean resolveBooleanValue(ElectKonfigurationWerte template) {
    return resolveBooleanValue(createSystemMandantContext(template));
  }

  public File resolveFileValue(ElectKonfigurationWerte template) {
    return resolveFileValue(createSystemMandantContext(template));
  }

  public <V extends Enum<?>> V resolveEnumValue(ElectKonfigurationWerte template, Class<V> enumClass) {
    return resolveEnumValue(createSystemMandantContext(template), enumClass);
  }


  public void setValue(Mandant mandant, MandantKonfigurationWerte template, String wert) {
    setValue(createCustomerMandantContext(mandant, template), wert);
  }

  public String resolveRequiredStringValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveRequiredStringValue(createCustomerMandantContext(mandant, template));
  }

  public String resolveStringValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveStringValue(createCustomerMandantContext(mandant, template));
  }

  public Long resolveLongValueOptional(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveLongValueOptional(createCustomerMandantContext(mandant, template));
  }

  public long resolveLongValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveLongValue(createCustomerMandantContext(mandant, template));
  }

  public int resolveIntegerValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveIntegerValue(createCustomerMandantContext(mandant, template));
  }

  public int resolveIntegerValue(Mandant mandant, MandantKonfigurationWerte template, int defaultValue) {
    return resolveIntegerValue(createCustomerMandantContext(mandant, template), defaultValue);
  }

  public boolean resolveBooleanValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveBooleanValue(createCustomerMandantContext(mandant, template));
  }

  public File resolveFileValue(Mandant mandant, MandantKonfigurationWerte template) {
    return resolveFileValue(createCustomerMandantContext(mandant, template));
  }

  public <V extends Enum<?>> V resolveEnumValue(Mandant mandant, MandantKonfigurationWerte template, Class<V> enumClass) {
    return resolveEnumValue(createCustomerMandantContext(mandant, template), enumClass);
  }

  private KonfigurationLoaderContext createSystemMandantContext(ElectKonfigurationWerte template) {
    return new KonfigurationLoaderContext(null, template);
  }

  private KonfigurationLoaderContext createCustomerMandantContext(Mandant mandant, MandantKonfigurationWerte template) {
    return new KonfigurationLoaderContext(mandant, template);
  }

  @Override
  protected void handleParameterNichtNichtGesetz(Object template) {
    throw new ElectCommonException(KonfigurationNichtGesetzException.MESSAGE_KEY, template);
  }

  @Override
  @Nullable
  protected Konfiguration getParameter(KonfigurationLoaderContext context) {
    return getParameter(context.getMandant(), context.getTemplate());
  }

  protected Konfiguration getParameter(Mandant mandant, ElectSystemMandantKonfigurationWerte template) {
    if (template instanceof ElectKonfigurationWerte) {
      return getParameter((ElectKonfigurationWerte) template);
    }
    if (template instanceof MandantKonfigurationWerte) {
      return getParameter(mandant, (MandantKonfigurationWerte) template);
    }
    throw new IllegalStateException("Unbekanntes Template: " + template);

  }

  public Konfiguration getParameter(ElectKonfigurationWerte template) {
    try {
      return getKonfigurationDao().findBySystemSchluessel(template.getKey());
    } catch (NoResultException ignored) {
    }

    return null;
  }

  public Konfiguration getParameter(Mandant mandant, MandantKonfigurationWerte template) {
    try {
      return getKonfigurationDao().findByMandantSchluessel(mandant, template.getKey());
    } catch (NoResultException ignored) {
    }

    return null;
  }

  protected abstract AbstractKonfigurationDAO getKonfigurationDao();
}
