
package de.ivu.elect.presentation.benutzerverwaltung;

import java.util.List;
import java.util.stream.Collectors;

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

import de.ivu.elect.business.benutzerverwaltung.boundary.PermissionService;
import de.ivu.elect.business.benutzerverwaltung.entity.Rolle;
import de.ivu.elect.business.benutzerverwaltung.entity.RolleConstants;
import de.ivu.elect.business.benutzerverwaltung.entity.RolleProperties;
import de.ivu.elect.business.konfiguration.entity.KonfigurationExtendedLoader;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.boundary.RolleCreator;
import de.ivu.elect.business.security.entity.StandardRollenAnpassbarArt;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.dynamicform.ReadOnlyStringProperty;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;


import static de.ivu.elect.business.konfiguration.entity.MandantKonfigurationWerte.STANDARD_ROLLEN_ANPASSBAR;

@OnlineHilfeId(value = "488", mehrfachVergebenOk = true)
@Named(AbstractRolleEditView.COMPONENT_NAME)
public class RolleEditView extends AbstractRolleEditView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient RolleUniqueUKFacesValidator rolleUniqueUKFacesValidator;

  @Inject
  private transient PermissionService permissionService;

  @Inject
  private transient RolleCreator rolleCreator;

  @Inject
  private transient KonfigurationExtendedLoader konfigurationExtendedLoader;

  public RolleEditView() {
    super();
  }

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

  @Override
  public void addProperties(FormBuilder formBuilder, Rolle rolle) {
    RolleProperties rolleProperties = rolle.getProperties();

    boolean readonlyName = false;
    boolean readonlyRechte = false;

    boolean standardRolle = bestimmeIsStandardRolle(rolle);
    if (rolle.isAdminRolle()) {
      readonlyName = true;
      formBuilder.addProperty(new ReadOnlyStringProperty(getMessage(RolleConstants.MESSAGE_KEY_ROLLE_ART_EINZAHL), getMessage("edit.admin.rolle.message")));
    } else if (standardRolle) {
      readonlyName = true;
      StandardRollenAnpassbarArt standardRollenAnpassbarArt = konfigurationExtendedLoader
          .resolveEnumValue(rolle.getMandant(), STANDARD_ROLLEN_ANPASSBAR, StandardRollenAnpassbarArt.class);
      readonlyRechte = !standardRollenAnpassbarArt.isAnpassbar();
      formBuilder.addProperty(new ReadOnlyStringProperty(getMessage(RolleConstants.MESSAGE_KEY_ROLLE_ART_EINZAHL), getMessage("edit.standard.rolle.message")));
      formBuilder.addProperty(new ReadOnlyStringProperty(getMessage(STANDARD_ROLLEN_ANPASSBAR), getMessage(standardRollenAnpassbarArt)));
    } else {
      formBuilder.addProperty(rolleProperties.getRolleArtProperty()).readonly();
    }
    formBuilder.addProperty(rolleProperties.getNameProperty()).withValidatorAndPk(rolleUniqueUKFacesValidator, rolle.getPk()).withReadonly(readonlyName);
    formBuilder.addProperty(rolleProperties.getBeschreibungProperty());

    if (rolle.isFachlicheRolle()) {
      List<UseCasePermission> availablePermissionsForCurrentCustomer =
          permissionService.getAvailablePermissionsForCurrentCustomer(getMandant().getArt()).stream()/*ELECTKOM-1014*/.filter(this::hasPermission).collect(Collectors.toList());
      formBuilder.addProperty(rolle.getRollePermissionPickListProperty(availablePermissionsForCurrentCustomer, getMessages())).withReadonly(readonlyRechte);
    }
  }

  private boolean bestimmeIsStandardRolle(Rolle rolle) {
    return rolle.isStandardRolle(rolleCreator.getAnzulegendeKundenRollen());
  }
}
