
package de.ivu.elect.presentation.dokumente;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.dokumente.boundary.DokumentVorlageExtendedDAO;
import de.ivu.elect.business.dokumente.entity.DokumentVorlage;
import de.ivu.elect.business.dokumente.entity.DokumentVorlagePK;
import de.ivu.elect.business.dokumente.entity.QDokumentVorlage;
import de.ivu.elect.business.wahl.boundary.WahlSessionSelector;
import de.ivu.elect.business.wahl.boundary.WahlterminSessionSelect;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.Wahltermin;
import de.ivu.elect.jee.entity.QueryDsl;
import de.ivu.elect.jee.faces.AbstractEntityFacesValidator;
import de.ivu.elect.jee.faces.FacesComponentUtil;
import de.ivu.elect.jee.faces.FacesMessageUtil;

@Named(value = DokumentVorlageUniqueUKFacesValidator.VALIDATOR_ID)
@RequestScoped
public class DokumentVorlageUniqueUKFacesValidator implements Validator, Serializable {

  private static final long serialVersionUID = 1L;

  public static final String VALIDATOR_ID = "dokumentVorlageUniqueUKFacesValidator";

  @Inject
  private DokumentVorlageExtendedDAO extendedDAO;

  @Inject
  private WahlterminSessionSelect wahlterminSessionSelect;

  @Inject
  private WahlSessionSelector wahlSessionSelect;

  public DokumentVorlageUniqueUKFacesValidator() {
    super();
  }

  @Override
  public void validate(FacesContext ctx, UIComponent component, Object value) {
    DokumentVorlagePK pk = getPk(component);

    if (!isValid(value, pk)) {
      throw new ValidatorException(FacesMessageUtil.createErrorMessage(ctx, AbstractEntityFacesValidator.SUMMARY_KEY,
          AbstractEntityFacesValidator.DETAIL_KEY, FacesComponentUtil.getLocalizedLabelOrId(component), value));
    }
  }

  private boolean isValid(Object value, DokumentVorlagePK pk) {
    Wahltermin wahltermin = wahlterminSessionSelect.getEntity();
    Wahl wahl = wahlSessionSelect.getEntity();

    QueryDsl queryDsl = extendedDAO.getQueryDsl();

    QDokumentVorlage dokumentVorlageTable = QDokumentVorlage.dokumentVorlage;
    queryDsl.from(dokumentVorlageTable);
    queryDsl.eqIfNotNull(dokumentVorlageTable.wahl, wahl);
    queryDsl.eqIfNotBlank(dokumentVorlageTable.name, (String) value);
    queryDsl.eqIfNotNull(dokumentVorlageTable.wahltermin, wahltermin);
    List<DokumentVorlage> list = queryDsl.list(dokumentVorlageTable);

    if (list.isEmpty()) {
      return true;
    }
    if (list.size() == 1) {
      DokumentVorlage dokumentVorlage = list.get(0);
      return dokumentVorlage.getPk().equals(pk);
    }

    return false;
  }

  private DokumentVorlagePK getPk(UIComponent component) {
    return (DokumentVorlagePK) FacesComponentUtil.getAttributeFromDynamicFormControlOrNormal(component,
        AbstractEntityFacesValidator.ENTITY_PK_ATTRIBUTE_NAME);
  }
}
