/**
 * Copyright (c) 2014 openHAB UG (haftungsbeschraenkt) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.ui.zoo.internal.servlet;

import org.osgi.service.component.ComponentContext;
import org.osgi.service.http.HttpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Hashtable;

/**
 * This component registers the Zoo UI Webapp.
 * 
 * @author Sebastian Janzen - Initial contribution
 */
public class ZooUIApp {

    private static final Logger logger = LoggerFactory.getLogger(ZooUIApp.class);
    public static final String WEBAPP_ALIAS = "/zoo";
    public static final String WEB_DIST_FOLDER = "web_dist";
    public static final String WEB_FOLDER = "web";
    public static final String DEBUG_PARAMETER_NAME = "webDebug";
    private static final String DEFAULT_INFLUX_URI = "http://localhost:8086";
    public static final String INFLUX_URI_PARAMETER_NAME = "influxDbUri";

    protected HttpService httpService;

    protected void activate(ComponentContext componentContext) {
        try {
            boolean debugMode = isDebugModeEnabled();
            String serveFolder = debugMode ? WEB_FOLDER : WEB_DIST_FOLDER;
            
            //httpService.registerResources(WEBAPP_ALIAS, serveFolder, null);
            Hashtable<String, String> initParams = new Hashtable<>();
            final String absolutePath = getAbsolutePath(componentContext);
            if (absolutePath == null) {
            	logger.error("Could not determine absolute path of the bundle folder.");
            }
            initParams.put("basePath", absolutePath + "/" + serveFolder);
            
            httpService.registerServlet(WEBAPP_ALIAS, new FileServlet(), initParams, null);
            logger.info("Started Zoo UI at {} with debug mode = {}.", WEBAPP_ALIAS, debugMode);
            
            Hashtable<String, String> initParamsProxy = new Hashtable<>();
            initParamsProxy.put("targetUri", getInfluxUri());
            initParamsProxy.put("log","true");
            httpService.registerServlet("/zoo/influxproxy", new ProxyServlet(), initParamsProxy, null);
            
        } catch (Exception e) {
            logger.error("Error during servlet startup", e);
        }
    }

    private String getAbsolutePath(ComponentContext componentContext) {
    	final String location = componentContext.getBundleContext().getBundle().getLocation();
    	if (location.indexOf("/") >= 0) {
    		return location.substring(location.indexOf("/"));
    	}
		return null;
	}
    
    private String getInfluxUri() {
    	String influxUriSystemProperty = System.getProperty(INFLUX_URI_PARAMETER_NAME);
    	return influxUriSystemProperty == null ? DEFAULT_INFLUX_URI : influxUriSystemProperty;
    }

	private boolean isDebugModeEnabled() {
        boolean distFolderExists = this.getClass().getResource("/" + WEB_DIST_FOLDER) != null;
        boolean debugParameterSet = Boolean.parseBoolean(System.getProperty(DEBUG_PARAMETER_NAME));
        if (!distFolderExists) {
            logger.debug("Zoo UI has no /{} folder, falling back to debug mode.", WEB_DIST_FOLDER);
        } else if (debugParameterSet) {
            logger.debug("Zoo UI found a /{} folder, but forced to use /{} because of system parameter '{}'.",
                         WEB_DIST_FOLDER, WEB_FOLDER, DEBUG_PARAMETER_NAME);
        }

        return debugParameterSet || !distFolderExists;
    }

    protected void deactivate(ComponentContext componentContext) {
        httpService.unregister(WEBAPP_ALIAS);
        httpService.unregister("/zoo/influxproxy");
        logger.info("Stopped Zoo UI");
    }

    protected void setHttpService(HttpService httpService) {
        this.httpService = httpService;
    }

    protected void unsetHttpService(HttpService httpService) {
        this.httpService = null;
    }

}

@@ -990,16 +990,76 @@
 Debug%22;%0A
+    private static final String prefix = %22reference:file:%22;%0A
     priv
@@ -1128,16 +1128,16 @@
 :8086%22;%0A
-
     publ
@@ -1628,35 +1628,34 @@
     
-f
 in
-al String absolut
+itParams.put(%22bas
 ePath
- =
+%22,
  get
@@ -1688,201 +1688,8 @@
 ext)
-;%0A            if (absolutePath == null) %7B%0A            %09logger.error(%22Could not determine absolute path of the bundle folder.%22);%0A            %7D%0A            initParams.put(%22basePath%22, absolutePath
  + %22
@@ -2518,25 +2518,34 @@
 ion.
-indexOf(%22/%22) %3E= 0
+length() %3E prefix.length()
 ) %7B%0A
@@ -2580,28 +2580,22 @@
 ing(
-location.indexOf(%22/%22
+prefix.length(
 ));%0A

Revert "Fix absolute path determination"