package de.agilecoders.wicket.logging;

import de.agilecoders.wicket.logging.settings.ClientSideLoggingSettings;
import de.agilecoders.wicket.logging.settings.IClientSideLoggingSettings;
import de.agilecoders.wicket.webjars.util.WicketWebjars;
import org.apache.wicket.Application;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.util.lang.Args;

/**
 * base initialization class
 *
 * @author miha
 */
public final class ClientSideLogging {

    /**
     * The {@link org.apache.wicket.MetaDataKey} used to retrieve the {@link ClientSideLoggingSettings} from the Wicket {@link Appendable}.
     */
    private static final MetaDataKey<IClientSideLoggingSettings> METADATA_KEY = new MetaDataKey<IClientSideLoggingSettings>() {
    };

    /**
     * installs default settings on given application
     *
     * @param application the application to add the settings to
     */
    public static void install(final WebApplication application) {
        install(application, new ClientSideLoggingSettings());
    }

    /**
     * installs given settings on given application
     *
     * @param application the application to add the settings to
     * @param settings    the settings to add
     */
    public static void install(final WebApplication application, final IClientSideLoggingSettings settings) {
        Args.notNull(application, "application");

        if (settings(application) == null) {
            application.setMetaData(METADATA_KEY, Args.notNull(settings, "settings"));

            WicketWebjars.install(application);
        }
    }

    /**
     * returns the {@link ClientSideLoggingSettings} which are assigned to given application
     *
     * @param app The current application
     * @return assigned {@link ClientSideLoggingSettings}
     */
    public static IClientSideLoggingSettings settings(final Application app) {
        return app.getMetaData(METADATA_KEY);
    }

    /**
     * returns the {@link ClientSideLoggingSettings} which are assigned to current application
     *
     * @return assigned {@link ClientSideLoggingSettings}
     */
    public static IClientSideLoggingSettings settings() {
        if (Application.exists()) {
            return settings(Application.get());
        }

        throw new IllegalStateException("there is no active application assigned to this thread.");
    }

}

@@ -1041,38 +1041,11 @@
 n, n
-ew ClientSideLoggingSettings()
+ull
 );%0A 
@@ -1300,22 +1300,16 @@
 ication,
- final
  IClient
@@ -1432,24 +1432,134 @@
  == null) %7B%0A
+            if(settings == null) %7B%0A                settings = new ClientSideLoggingSettings();%0A            %7D%0A%0A
             
@@ -1600,42 +1600,16 @@
 EY, 
-Args.notNull(settings, %22settings%22)
+settings
 );%0A%0A

improve initialization