
package de.ivu.elect.business.benutzerverwaltung.entity;

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

import javax.faces.convert.Converter;

import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCasePermissionMessageComparator;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.presentation.dynamicform.AbstractPickListProperty;
import de.ivu.elect.presentation.security.ElectCommonPermissionConverter;

/**
 *  on 05.06.2015.
 */
public abstract class AbstractUseCasePermissionPickListProperty extends AbstractPickListProperty<UseCasePermission> {

  private final Messages messages;

  private final List<UseCasePermission> verfuegbareRechte;

  protected AbstractUseCasePermissionPickListProperty(List<UseCasePermission> verfuegbareRechte, Messages messages) {
    this.verfuegbareRechte = verfuegbareRechte;
    this.messages = messages;
  }

  @Override
  public Converter getDefaultConverter() {
    return ElectCommonPermissionConverter.getInstance();
  }

  @Override
  protected String getSourceCaptionMessageKey() {
    return "rolle.edit.permissions.source";
  }

  @Override
  protected List<UseCasePermission> getVerfuegbareElemente() {
    List<UseCasePermission> result = new ArrayList<>(verfuegbareRechte);

    result.removeAll(getZugeordneteElemente());

    return sortByLabel(result);
  }

  @Override
  public boolean getDefaultRequired() {
    return false;
  }

  @Override
  public String getItemLabel(UseCasePermission element) {
    return messages.lokalisiere(element);
  }

  protected List<UseCasePermission> sortByLabel(List<UseCasePermission> result) {
    Collections.sort(result, new UseCasePermissionMessageComparator(messages));

    return result;
  }
}
