
package de.ivu.elect.presentation.wahlvorschlag;

import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.presentation.gruppeimgebiet.ReferendumVorlageImGebietEditView;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.wahlvorschlag.form.ReferendumOptionViewFormBuilder;

@OnlineHilfeId(value = "783", mehrfachVergebenOk = true)
@Named(AbstractReferendumOptionEditNoScopeView.COMPONENT_NAME)
public class ReferendumOptionEditView extends AbstractReferendumOptionEditView {

  private static final long serialVersionUID = 1L;

  private static final String PARENT_VIEW_ID = ReferendumVorlageImGebietEditView.VIEW_ID;

  @Inject
  private ReferendumOptionViewFormBuilder viewFormBuilder;

  public ReferendumOptionEditView() {
    super();
  }

  @Override
  @PreConditionCheck
  public String checkPreCondition() {
    if (isReferendumOptionForGebietSelected()) {
      return null;
    }

    return cancel();
  }

  @Override
  protected boolean isGebietBearbeitenRechtPruefen() {
    return true;
  }

  @Override
  protected String getParentViewId() {
    return PARENT_VIEW_ID;
  }

  private boolean isReferendumOptionForGebietSelected() {
    return isReferendumOptionSelected() && isGebietSelected() && getSelectedGebiet().equals(getEntity().getGebiet());
  }

  private boolean isReferendumOptionSelected() {
    return isEntitySet();
  }

  public String getReferendumOptionLabel() {
    return getEntity().getLabel();
  }

  public String getReferendumVorlageName() {
    return getEntity().getKurznameAufGebiet();
  }

  public String getGebietName() {
    return getSelectedGebietNameFormatted();
  }

  @Override
  public void addProperties(FormBuilder formBuilder, ReferendumOption referendumOption) {
    viewFormBuilder.addProperties(formBuilder, referendumOption);
  }
}
