
package de.ivu.elect.presentation.ui;

import static java.util.stream.Collectors.toMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ViewAccessScoped;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.ui.boundary.UITableColumnOrderExtendedDAO;
import de.ivu.elect.business.ui.boundary.UITableColumnOrderKonfiguration;
import de.ivu.elect.business.ui.boundary.UITableColumnReihenfolgeFestlegenEdit;
import de.ivu.elect.business.ui.entity.AbstractUITableColumnOrder;
import de.ivu.elect.business.ui.entity.UITableColumnOrder;
import de.ivu.elect.jee.entity.AbstractNoPersistentEntity;
import de.ivu.elect.jee.entity.tk.EntityUK;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamictable.ColumnBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.AbstractElectEditView;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@ViewAccessScoped
@OnlineHilfeId("371")
@Named(UITableColumnReihenfolgeFestlegenView.COMPONENT_NAME)
@View(viewId = UITableColumnReihenfolgeFestlegenView.VIEW_ID, useCase = UseCasePermission.UI_TABLE_COLUMN_EDIT)
public class UITableColumnReihenfolgeFestlegenView extends AbstractElectEditView<AbstractNoPersistentEntity<Object, EntityUK>> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "uiTableColumnReihenfolgeFestlegenView";

  public static final String VIEW_ID = "/ui/ui-table-column-reihenfolge-festlegen.xhtml";

  @Inject
  private transient UITableColumnOrderExtendedDAO uiTableColumnOrderExtendedDAO;

  @Inject
  private transient UITableColumnOrderKonfiguration uiTableColumnOrderKonfiguration;

  @Inject
  private transient UITableColumnReihenfolgeFestlegenEdit uiTableColumnReihenfolgeFestlegenEdit;

  private ColumnBuilder< ? > columnBuilder;

  private UITableColumnOrderPickListProperty uiTableColumnOrderPickListProperty;

  public UITableColumnReihenfolgeFestlegenView() {
    super();
  }

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

  @Nullable
  @PreConditionCheck
  public String preConditionCheck() {
    if (columnBuilder == null) {
      return goBack(getStartViewId());
    }

    return null;
  }

  @Redirect
  public String init(ColumnBuilder<Object> columnBuilder) {
    this.columnBuilder = columnBuilder;

    resetModel();

    return VIEW_ID;
  }

  public boolean isReihenfolgeAendernErlaubt() {
    return hasPermission(UseCasePermission.UI_TABLE_COLUMN_EDIT);
  }

  @Override
  protected AbstractNoPersistentEntity<Object, EntityUK> getEntity() {
    return new AbstractNoPersistentEntity<>();
  }

  @Override
  public void addProperties(FormBuilder formBuilder, AbstractNoPersistentEntity<Object, EntityUK> ignored) {
    List<UITableColumnOrder> columnsInDb = uiTableColumnOrderKonfiguration.collectColumnsInDb(getPrincipal(), getSelectedWahl(), getSelectedWahltermin(), columnBuilder, uiTableColumnOrderExtendedDAO);
    Map<String, UITableColumnOrder> columnInDbPerColumnKey = columnsInDb.stream().collect(toMap(UITableColumnOrder::getColumnKey, Function.identity()));

    columnBuilder.forceInit();

    List<UITableColumnOrder> columnsInColumnBuilder = uiTableColumnOrderKonfiguration.collectColumnsInColumnBuilderVerfuegbar(columnBuilder);
    Map<String, String> columnHeaderPerColumnKey = columnsInColumnBuilder.stream().collect(toMap(UITableColumnOrder::getColumnKey, AbstractUITableColumnOrder::getColumnHeader));

    List<UITableColumnOrder> columnsVerfuegbar = new ArrayList<>();
    List<UITableColumnOrder> columnsAngezeigt = new ArrayList<>();

    for (UITableColumnOrder uiTableColumnOrder : columnsInColumnBuilder) {
      columnsVerfuegbar.add(uiTableColumnOrder);

      if (uiTableColumnOrderKonfiguration.isColumnAnzuzeigen(columnInDbPerColumnKey, uiTableColumnOrder)) {
        columnsAngezeigt.add(uiTableColumnOrder);
      }
    }

    uiTableColumnOrderPickListProperty = new UITableColumnOrderPickListProperty(columnsVerfuegbar, columnsAngezeigt, columnHeaderPerColumnKey);

    formBuilder.addProperty(uiTableColumnOrderPickListProperty);
  }

  @Override
  protected boolean isSavePermissionVorhanden() {
    return isReihenfolgeAendernErlaubt();
  }

  @Override
  @Redirect
  public String save() {
    if (uiTableColumnOrderPickListProperty != null) {
      uiTableColumnReihenfolgeFestlegenEdit.save(getPrincipal(), getSelectedWahl(), getSelectedWahltermin(), columnBuilder, uiTableColumnOrderPickListProperty.getZugeordneteElemente(),
          uiTableColumnOrderPickListProperty.getVerfuegbareElemente());

      resetModel();

      addInfoMessage("table.columns.reihenfolge.gespeichert");
    }

    return goBack(getStartViewId());
  }

  @Override
  public boolean isBtnResetRendered() {
    return super.isBtnResetRendered() && isReihenfolgeAendernErlaubt();
  }

  @Nullable
  @Override
  @Redirect
  public String reset() {
    uiTableColumnReihenfolgeFestlegenEdit.reset(getPrincipal(), getSelectedWahl(), getSelectedWahltermin(), columnBuilder);

    resetModel();

    addInfoMessage("table.columns.reihenfolge.reset");

    super.reset();

    return goBack(getStartViewId());
  }

  @Override
  public boolean isBtnCancelRendered() {
    return true;
  }

  @Override
  @Redirect
  public String cancel() {
    return goBack(getStartViewId());
  }

  @Override
  public String delete() {
    throw new UnsupportedOperationException("Nicht unterstützt");
  }
}
