package de.ivu.elect.presentation.internet;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.internet.boundary.InternetSeiteConversationSelect;
import de.ivu.elect.business.internet.boundary.InternetWidgetExtendedDAO;
import de.ivu.elect.business.internet.control.InternetWidgetCustomDataFactory;
import de.ivu.elect.business.internet.entity.InternetCustomDataCreateContext;
import de.ivu.elect.business.internet.entity.InternetSeite;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetBreite;
import de.ivu.elect.business.internet.entity.InternetWidgetCustomData;
import de.ivu.elect.business.internet.entity.InternetWidgetStatus;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.entity.customdata.CustomDataSerializerFactory;
import de.ivu.elect.jee.faces.PreConditionCheck;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.View;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;
import de.ivu.elect.presentation.internet.form.InternetWidgetViewFormBuilder;

@RequestScoped
@Named(AbstractInternetWidgetCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractInternetWidgetCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.INTERNET_WIDGET_CREATE)
@OnlineHilfeId("748")
public class InternetWidgetCreateView extends AbstractInternetWidgetCreateView {

  @Inject
  private transient InternetSeiteConversationSelect internetSeiteConversationSelect;

  @Inject
  private transient InternetWidgetViewFormBuilder viewFormBuilder;

  @Inject
  private transient InternetWidgetCustomDataFactory internetWidgetCustomDataFactory;

  @Inject
  private transient InternetWidgetExtendedDAO internetWidgetExtendedDAO;

  @View
  @Inject
  private transient InternetWidgetEditView internetWidgetEditView;

  @PreConditionCheck
  public String checkWahlSelected() {
    if (internetSeiteConversationSelect.isSelected()) {
      return null;
    }

    return InternetSeiteSearchView.VIEW_ID;
  }

  @Override
  @PostConstruct
  public void init() {
    super.init();

    initEntity();
  }

  private void initEntity() {
    InternetWidget internetWidget = getEntity();
    InternetSeite internetSeite = internetSeiteConversationSelect.getEntity();
    internetWidget.setInternetSeite(internetSeite);
    internetWidget.setStatus(InternetWidgetStatus.KONFIGURATION_NOTWENDIG); // CustomData muss erst konfiguriert werden
    if (internetSeite != null) {
      Long maxZeile = internetWidgetExtendedDAO.findByInternetSeiteMaxZeile(internetSeite);
      if (maxZeile == null) {
        maxZeile = 0L;
      }
      internetWidget.setZeile(maxZeile + 1);
      internetWidget.setPosition(1L);
      if (internetWidget.getZeile() == 0) {
        internetWidget.setBreite(InternetWidgetBreite.WIDTH_6_6);
      } else {
        internetWidget.setBreite(InternetWidgetBreite.WIDTH_1_6);
      }
    }
  }

  @Override
  public void addProperties(FormBuilder formBuilder, InternetWidget internetWidget) {
    viewFormBuilder.addProperties(formBuilder, internetWidget);
  }

  @Override
  protected void callSave(InternetWidget internetWidget) {
    InternetWidgetCustomData customData = internetWidgetCustomDataFactory.createCustomData(internetWidget.getWidgetTyp(), new InternetCustomDataCreateContext(messages, getSelectedWahl(), internetWidget.getInternetSeite().getSeitenTyp(), internetWidget.getSprungmarke()));
    customData.setStorageEntity(internetWidget.getCustomDataStorage());
    customData.setTechnicalEntity(internetWidget);
    internetWidget.setCustomDataXml(CustomDataSerializerFactory.getCustomDataSerializer().serializeCustomData(customData));
    internetWidget.getCustomDataStorage().clearCustomDataCache();
    super.callSave(internetWidget);
  }

  @Override
  @Redirect
  public String save() {
    InternetWidget internetWidget = getEntity();

    String target = super.save();

    addInfoMessage("internet.widget.created.activate.configure");

    // Spezifische Konfiguration muss noch gesetzt werden, wenn in CustomData gefordert siehe callSave()
    if (!internetWidget.getStatus().isAktiv() && hasPermission(UseCasePermission.INTERNET_WIDGET_EDIT)) {
      return internetWidgetEditView.edit(getEntity());
    }

    return target;
  }
}
