
package de.ivu.elect.presentation.gruppeimgebiet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

import de.ivu.elect.business.gruppe.entity.GruppeProperties;
import de.ivu.elect.business.gruppe.entity.ReferendumVorlageMeta;
import de.ivu.elect.business.gruppeimgebiet.boundary.GruppeImGebietConversationSelect;
import de.ivu.elect.business.gruppeimgebiet.boundary.ReferendumVorlageImGebietEdit;
import de.ivu.elect.business.gruppeimgebiet.entity.GruppeImGebiet;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.business.wahlvorschlag.boundary.ReferendumOptionConversationSelect;
import de.ivu.elect.business.wahlvorschlag.control.ReferendumOptionByPositionComparator;
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.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.gruppeimgebiet.form.ReferendumVorlageImGebietViewFormBuilder;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.wahlvorschlag.ReferendumOptionCreateView;

@OnlineHilfeId(value = "782", mehrfachVergebenOk = true)
@Named(ReferendumVorlageImGebietEditView.COMPONENT_NAME)
@View(viewId = ReferendumVorlageImGebietEditView.VIEW_ID, useCase = UseCasePermission.REFERENDUM_VORLAGE_EDIT)
public class ReferendumVorlageImGebietEditView extends AbstractGruppeImGebietEditView {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "referendumVorlageImGebietEditView";

  public static final String VIEW_ID = "/gruppeimgebiet/referendum-vorlage-im-gebiet-edit.xhtml";

  protected static final String PARENT_VIEW_ID = ReferendumVorlageImGebietSearchView.VIEW_ID;

  private static final Comparator<ReferendumOption> DEFAULT_REFERENDUM_OPTION_COMPARATOR = new ReferendumOptionByPositionComparator();

  @Inject
  private GruppeImGebietConversationSelect referendumVorlageImGebietSelect;

  @Inject
  private ReferendumOptionConversationSelect referendumOptionSelect;

  @Inject
  private ReferendumVorlageImGebietEdit referendumVorlageImGebietEdit;

  @Inject
  private ReferendumVorlageMeta referendumVorlageMeta;

  @Inject
  private ReferendumVorlageImGebietViewFormBuilder viewFormBuilder;

  public ReferendumVorlageImGebietEditView() {
    super();
  }

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

    return cancel();
  }

  private boolean isReferendumVorlageImGebietForGebietSelected() {
    return isReferendumVorlageSelected() && isGebietSelected() && getSelectedGebiet().equals(getEntity().getGebiet());
  }

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

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

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

  public ReferendumOption getReferendumOptionSelection() {
    return referendumOptionSelect.getEntity();
  }

  public void setReferendumOptionSelection(ReferendumOption referendumOption) {
    if (referendumOption != null) {
      referendumOptionSelect.setEntity(referendumOption);
    }
  }

  public List<ReferendumOption> getReferendumOptionSearchResult() {
    List<ReferendumOption> result = new ArrayList<>(getEntity().getReferendumOptionen());

    Collections.sort(result, getReferendumOptionSearchResultComparator());

    return result;
  }

  private Comparator<ReferendumOption> getReferendumOptionSearchResultComparator() {
    return DEFAULT_REFERENDUM_OPTION_COMPARATOR;
  }

  @Override
  protected String getViewId() {
    return VIEW_ID;
  }

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

  @Override
  public void addProperties(FormBuilder formBuilder, GruppeImGebiet referendumVorlageImGebiet) {
    viewFormBuilder.addProperties(formBuilder, referendumVorlageImGebiet);

    GruppeProperties referendumVorlageProperties = referendumVorlageImGebiet.getGruppe().getProperties();

    formBuilder.selectProperty(referendumVorlageProperties.getSchluesselProperty()).readonly();
  }

  public boolean isBtnCreateReferendumOptionRendered() {
    return hasPermission(UseCasePermission.REFERENDUM_OPTION_CREATE);
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.REFERENDUM_OPTION_CREATE)
  public String createReferendumOption() {
    referendumVorlageImGebietSelect.setEntity(getEntity());

    return ReferendumOptionCreateView.VIEW_ID;
  }

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.REFERENDUM_VORLAGE_EDIT);
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.REFERENDUM_VORLAGE_EDIT)
  public String save() {
    GruppeImGebiet referendumVorlageImGebiet = getEntity();

    referendumVorlageImGebiet.setVorgegebenePosition(referendumVorlageImGebiet.getRelativePosition());

    referendumVorlageImGebietEdit.save(referendumVorlageImGebiet);

    addMessageSaveSuccess(referendumVorlageImGebiet);

    return closeConversationStoreAndGoBack();
  }

  @Override
  protected void addMessageSaveSuccess(GruppeImGebiet entity) {
    addInfoMessage("entity.edit.action.save.success.message", referendumVorlageMeta.getEinzahlMessage());
  }

  @Override
  public boolean isBtnDeleteRendered() {
    return hasPermission(UseCasePermission.REFERENDUM_VORLAGE_DELETE);
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.REFERENDUM_VORLAGE_DELETE)
  public String delete() {
    GruppeImGebiet referendumVorlageImGebiet = getEntity();

    referendumVorlageImGebietEdit.delete(referendumVorlageImGebiet);

    addMessageDeleteSuccess();

    return closeConversationStoreAndGoBack();
  }

  @Override
  protected void addMessageDeleteSuccess() {
    addInfoMessage("entity.edit.action.delete.success.message", referendumVorlageMeta.getEinzahlMessage());
  }

  @Override
  @Redirect
  @UseCaseSecure(UseCasePermission.REFERENDUM_VORLAGE_DELETE)
  public String deleteAndGoBack() {
    return super.deleteAndGoBack();
  }

  private String closeConversationStoreAndGoBack() {
    conversationStore.close();

    return goBack(getParentViewId());
  }
}
