
package de.ivu.elect.presentation.kandidatur.form;

import javax.annotation.Nullable;

import de.ivu.elect.business.kandidatur.entity.Direktkandidatur;
import de.ivu.elect.business.kandidatur.entity.DirektkandidaturConstants;
import de.ivu.elect.business.kandidatur.entity.Ersatzkandidatur;
import de.ivu.elect.business.kandidatur.entity.ErsatzkandidaturConstants;
import de.ivu.elect.business.kandidatur.entity.Kandidatur;
import de.ivu.elect.business.kandidatur.entity.KandidaturConstants;
import de.ivu.elect.business.kandidatur.entity.Listenkandidatur;
import de.ivu.elect.business.kandidatur.entity.ListenkandidaturConstants;
import de.ivu.elect.jee.presentation.dynamicform.AbstractStringProperty;

public class KandidaturTypProperty<E extends Kandidatur> extends AbstractStringProperty {

  private final E kandidatur;

  public KandidaturTypProperty(E kandidatur) {
    super();

    this.kandidatur = kandidatur;
  }

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

  @Nullable
  @Override
  public String getValue() {
    String key = getValueKey();

    if (key != null) {
      return getMessage(key);
    }

    return null;
  }

  @Nullable
  public String getValueKey() {
    if (kandidatur != null) {
      if (kandidatur.isInstanceOf(Listenkandidatur.class)) {
        return ListenkandidaturConstants.MESSAGE_KEY_ENTITY_EINZAHL;
      }

      if (kandidatur.isInstanceOf(Direktkandidatur.class)) {
        return DirektkandidaturConstants.MESSAGE_KEY_ENTITY_EINZAHL;
      }

      if (kandidatur.isInstanceOf(Ersatzkandidatur.class)) {
        return ErsatzkandidaturConstants.MESSAGE_KEY_ENTITY_EINZAHL;
      }
    }

    return null;
  }

  @Override
  public void setValue(String value) {
    throw new UnsupportedOperationException("CANNOT set a readonly property");
  }

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

  @Override
  public String getDefaultLabel() {
    return getMessage(KandidaturConstants.MESSAGE_KEY_ENTITY_EINZAHL);
  }

  @Override
  public int getDefaultMaxLength() {
    return 0;
  }
}
