package net.explorviz.settings.server.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.annotation.WebListener;
import net.explorviz.settings.model.FlagSetting;
import net.explorviz.settings.model.RangeSetting;
import net.explorviz.settings.model.Setting;
import net.explorviz.settings.services.SettingsRepository;
import org.glassfish.jersey.server.monitoring.ApplicationEvent;
import org.glassfish.jersey.server.monitoring.ApplicationEvent.Type;
import org.glassfish.jersey.server.monitoring.ApplicationEventListener;
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.server.monitoring.RequestEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.morphia.Datastore;

/**
 * Primary starting class - executed, when the servlet context is started.
 */
@WebListener
public class SetupApplicationListener implements ApplicationEventListener {

  private static final Logger LOGGER = LoggerFactory.getLogger(SetupApplicationListener.class);

  private static final String ADMIN_NAME = "admin";

  @Inject
  private Datastore datastore;

  @Inject
  private SettingsRepository settingRepo;

  @Override
  public void onEvent(final ApplicationEvent event) {

    // After this type, CDI (e.g. injected LandscapeExchangeService) has been
    // fullfilled
    final Type t = Type.INITIALIZATION_FINISHED;


    if (event.getType().equals(t)) {
      this.addDefaultSettings();
    }

  }

  @Override
  public RequestEventListener onRequest(final RequestEvent requestEvent) {
    return null;
  }


  /**
   * Adds the default settings to the database
   */
  private void addDefaultSettings() {
    final String origin = "backend";

    // Workaround: Assign hardcoded ids
    final List<Setting> defaults = new ArrayList<Setting>(Arrays.asList(
        new FlagSetting("showFpsCounter", "Show FPS Counter",
            "\'Frames Per Second\' metrics in visualizations", origin, false),
        new FlagSetting("appVizTransparency", "App Viz Transparency",
            "Transparency effect for selection (left click) in application visualization", origin,
            true),
        new FlagSetting("keepHighlightingOnOpenOrClose", "Keep Highlighting On Open Or Close",
            "Toggle if highlighting should be resetted on double click in application"
                + " visualization",
            origin, true),
        new FlagSetting("enableHoverEffects", "Enable Hover Effects",
            "Hover effect (flashing entities) for mouse cursor", origin, true),
        new RangeSetting("appVizCommArrowSize", "Arrow Size in Application Visualization",
            "Arrow Size for selected communications in application visualization", origin, 1.0, 0.0,
            5.0),
        new RangeSetting("appVizTransparencyIntensity",
            "Transparency Intensity in Application Visualization",
            "Transparency effect intensity (\'App Viz Transparency\' must be enabled)", origin, 0.1,
            0.1, 0.5)));

    defaults.stream().forEach(this.settingRepo::createOrOverride);

  }

}

@@ -2071,23 +2071,22 @@
 ency%22, %22
-App Viz
+Enable
  Transpa
@@ -2088,18 +2088,28 @@
 ansparen
-cy
+t Components
 %22,%0A     
@@ -2999,15 +2999,14 @@
  (%5C'
-App Viz
+Enable
  Tra
@@ -3012,18 +3012,28 @@
 ansparen
-cy
+t Components
 %5C' must 
@@ -3045,16 +3045,28 @@
 abled)%22,
+%0A           
  origin,
@@ -3074,29 +3074,257 @@
 0.1,
-%0A            0.1, 0.5
+ 0.1, 0.5),%0A        new RangeSetting(%22appVizCurvyCommHeight%22, %22Curviness of the Communication Lines%22,%0A            %22If greater 0.0, communication lines are rendered arc-shaped with set height (Straight lines: 0.0)%22,%0A            origin, 0.0, 0.0, 50.0
 )));

Add new range-setting for curved communication lines