package org.cobbzilla.wizard.server.listener;

import lombok.extern.slf4j.Slf4j;
import org.cobbzilla.util.http.URIUtil;
import org.cobbzilla.wizard.server.RestServer;
import org.cobbzilla.wizard.server.RestServerLifecycleListenerBase;
import org.cobbzilla.wizard.server.config.RestServerConfiguration;

import java.awt.*;

import static java.awt.Desktop.isDesktopSupported;
import static org.cobbzilla.util.daemon.ZillaRuntime.die;

@Slf4j
public class BrowserLauncherListener extends RestServerLifecycleListenerBase {

    @Override public void onStart(RestServer server) {
        final RestServerConfiguration config = server.getConfiguration();
        final String baseUri = config.getPublicUriBase();
        final Thread appThread = new Thread(new Runnable() {
            @Override public void run() {
                final Desktop desktop = isDesktopSupported() ? Desktop.getDesktop() : null;
                if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
                    try {
                        desktop.browse(URIUtil.toUri(baseUri));
                    } catch (Exception e) {
                        final String msg = "onStart: error launching default browser with url '" + baseUri + "':  " + e;
                        log.error(msg, e);
                        die(msg, e);
                    }
                } else {
                    // no browser. tell the user where the server is listening via log statement
                    final String versionInfo = server.getConfiguration().hasVersion()
                            ? "\nVersion: " + server.getConfiguration().getVersion()
                            : "";
                    log.info("\n\n"+server.getConfiguration().getServerName()+" Successfully Started"+versionInfo+"\n\nNot launching browser: System lacks a browser and/or desktop window manager.\n\nWeb UI is: "+baseUri+"\nAPI is: "+baseUri+"/api\nHit Control-C to stop the server\n");
                }
            }
        });
        appThread.setName(BrowserLauncherListener.class.getSimpleName());
        appThread.setDaemon(true);
        appThread.start();

        super.onStart(server);
    }
}

@@ -315,16 +315,38 @@
 a.awt.*;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -390,16 +390,62 @@
 ported;%0A
+import static java.lang.Boolean.parseBoolean;%0A
 import s
@@ -493,16 +493,76 @@
 ime.die;
+%0Aimport static org.cobbzilla.util.daemon.ZillaRuntime.empty;
 %0A%0A@Slf4j
@@ -642,16 +642,230 @@
 Base %7B%0A%0A
+    public static final String DEFAULT_DISABLE_BROWSER_LAUNCH_ENV_VAR = %22DISABLE_BROWSER_AUTO_LAUNCH%22;%0A%0A    public String getDisableBrowserAutoLaunchEnvVarName() %7B return DEFAULT_DISABLE_BROWSER_LAUNCH_ENV_VAR; %7D%0A%0A
     @Ove
@@ -1091,71 +1091,91 @@
 ead(
-new Runnable() %7B%0A            @Override public void run() %7B%0A    
+() -%3E %7B%0A            final boolean allowLaunch = configAllowsBrowserLaunch(config);%0A
     
@@ -1205,16 +1205,31 @@
 esktop =
+ allowLaunch &&
  isDeskt
@@ -1273,20 +1273,16 @@
 : null;%0A
-    
         
@@ -1374,22 +1374,14 @@
     
- 
-   
 try %7B%0A
-    
     
@@ -1452,20 +1452,16 @@
         
-    
 %7D catch 
@@ -1488,36 +1488,32 @@
                 
-   
- 
 final String msg
@@ -1609,28 +1609,24 @@
             
-    
 log.error(ms
@@ -1652,20 +1652,16 @@
         
-    
 die(msg,
@@ -1657,36 +1657,32 @@
    die(msg, e);%0A
-    
                 
@@ -1695,20 +1695,16 @@
         
-    
 %7D else %7B
@@ -1704,20 +1704,16 @@
  else %7B%0A
-    
         
@@ -1805,36 +1805,32 @@
                 
-    
 final String ver
@@ -1903,20 +1903,16 @@
         
- 
-   
 ? %22%5CnVer
@@ -1988,22 +1988,14 @@
     
-    
 : %22%22;%0A
-    
     
@@ -2272,34 +2272,16 @@
 er%5Cn%22);%0A
-                %7D%0A
         
@@ -2472,10 +2472,469 @@
 ;%0A    %7D%0A
+%0A    public boolean configAllowsBrowserLaunch(RestServerConfiguration config) %7B%0A        final Map%3CString, String%3E env = config.getEnvironment();%0A        final String varName = getDisableBrowserAutoLaunchEnvVarName();%0A        if (empty(varName)) %7B%0A            log.warn(%22configAllowsBrowserLaunch: getDisableBrowserAutoLaunchEnvVarName() returned empty, not disabling%22);%0A            return true;%0A        %7D%0A        return !parseBoolean(env.get(varName));%0A    %7D%0A%0A
 %7D%0A

allow env override to disable browser launch