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

import de.ivu.elect.business.email.boundary.EmailLayoutExtendedDAO;
import de.ivu.elect.business.email.entity.EmailLayout;
import de.ivu.elect.business.email.entity.EmailLayoutConstants;
import de.ivu.elect.business.email.entity.EmailLayoutVerwendungType;
import de.ivu.elect.business.email.entity.EmailVorlage;
import de.ivu.elect.jee.faces.EntityConverter;
import de.ivu.elect.jee.presentation.dynamicform.AbstractSelectOneMenuProperty;
import de.ivu.elect.jee.presentation.dynamicform.Property;
import de.ivu.elect.presentation.email.EmailLayoutUiVisibility;

import javax.faces.convert.Converter;
import javax.inject.Inject;
import java.io.Serializable;
import java.util.List;

public class EmailVorlageEmailLayoutZuordnenProperties implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient EmailLayoutExtendedDAO emailLayoutDao;

  @Inject
  private transient EmailLayoutUiVisibility emailLayoutUiVisibility;

  private Boolean emailLayoutsVisible = null;

  public Property getEmailVorlageEmailLayoutProperty(EmailVorlage entity) {
    return new EmailVorlageEmailLayoutProperty(entity, emailLayoutDao.findByAllOrderByBezeichnung(), null);
  }

  public Property getEmailVorlageEmailLayoutWithDefaultValueProperty(EmailVorlage entity) {
    EmailLayout defaultEmailLayout = null;
    List<EmailLayout> vorausgewaehlteEmailLayouts = emailLayoutDao.findByVerwendungTyp(EmailLayoutVerwendungType.VORAUSGEWAEHLT);
    if (vorausgewaehlteEmailLayouts.size() > 0) {
      defaultEmailLayout = vorausgewaehlteEmailLayouts.get(0);
    }
    return new EmailVorlageEmailLayoutProperty(entity, emailLayoutDao.findByAllOrderByBezeichnung(), defaultEmailLayout);
  }

  public class EmailVorlageEmailLayoutProperty extends AbstractSelectOneMenuProperty<EmailLayout> {

    private final EmailVorlage entity;

    private final List<EmailLayout> emailLayouts;

    private final EmailLayout defaultEmailLayout;

    public EmailVorlageEmailLayoutProperty(EmailVorlage entity, List<EmailLayout> emailLayouts, EmailLayout defaultEmailLayout) {
      this.entity = entity;
      this.emailLayouts = emailLayouts;
      this.defaultEmailLayout = defaultEmailLayout;
      if (hasDefaultValue()) {
        entity.setEmailLayout(getDefaultValue());
      }
    }

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

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

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

    @Override
    public EmailLayout getValue() {
      return entity.getEmailLayout();
    }

    @Override
    public void setValue(EmailLayout value) {
      entity.setEmailLayout(value);
    }

    @Override
    public List<EmailLayout> getValues() {
      return emailLayouts;
    }

    @Override
    public String getItemLabel(EmailLayout item) {
      if (item == null) {
        return "";
      }
      return item.getBezeichnung();
    }

    @Override
    public boolean isVisible() {
      return super.isVisible() && isEmailLayoutsVisible();
    }

    @Override
    public boolean hasNoSelectionItem() {
      return true; // EmailLayout ist optional
    }

    @Override
    public EmailLayout getDefaultValue() {
      return defaultEmailLayout;
    }

    private boolean isEmailLayoutsVisible() {
      if (emailLayoutsVisible == null) {
        emailLayoutsVisible = emailLayoutUiVisibility.isEmailLayoutAuswahlVisible();
      }
      return emailLayoutsVisible;
    }
  }
}