/*
 * Copyright 2017 Hammock and its contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ws.ament.hammock.bootstrap.weld3;

import org.jboss.weld.bean.builtin.BeanManagerProxy;
import org.jboss.weld.environment.servlet.Listener;
import org.jboss.weld.environment.servlet.WeldServletLifecycle;
import org.jboss.weld.manager.BeanManagerImpl;
import ws.ament.hammock.bootstrap.Bootstrapper;
import ws.ament.hammock.web.api.WebServer;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import javax.enterprise.inject.spi.CDI;

import static org.jboss.weld.environment.servlet.Container.CONTEXT_PARAM_CONTAINER_CLASS;

public class Weld3Bootstrapper implements Bootstrapper{
    private SeContainer seContainer;
    private SeContainerInitializer seContainerInitializer;

    public Weld3Bootstrapper() {
        seContainerInitializer = SeContainerInitializer.newInstance();
    }

    @Override
    public void start() {
        seContainer = seContainerInitializer.initialize();
    }

    @Override
    public void stop() {
        seContainer.close();
    }

    @Override
    public void configure(WebServer webServer) {
        BeanManagerImpl beanManagerImpl = BeanManagerProxy.unwrap(CDI.current().getBeanManager());
        webServer.addServletContextAttribute(WeldServletLifecycle.BEAN_MANAGER_ATTRIBUTE_NAME, beanManagerImpl);
        webServer.addServletContextAttribute(org.jboss.weld.Container.CONTEXT_ID_KEY, beanManagerImpl.getContextId());
        webServer.addInitParameter(CONTEXT_PARAM_CONTAINER_CLASS, HammockContainer.class.getName());
        webServer.addListener(Listener.class);
    }
}

@@ -651,16 +651,118 @@
 weld3;%0A%0A
+import org.eclipse.microprofile.config.Config;%0Aimport org.eclipse.microprofile.config.ConfigProvider;%0A
 import o
@@ -806,16 +806,16 @@
 rProxy;%0A
-
 import o
@@ -1207,16 +1207,68 @@
 i.CDI;%0A%0A
+import static org.jboss.weld.environment.se.Weld.*;%0A
 import s
@@ -1501,16 +1501,43 @@
 ializer;
+%0A    private Config config;
 %0A%0A    pu
@@ -1630,24 +1630,69 @@
 Instance();%0A
+        config = ConfigProvider.getConfig();%0A
     %7D%0A%0A    @
@@ -1738,44 +1738,670 @@
     
-seContainer = seContainerInitializer
+boolean isolation = getBoolean(ARCHIVE_ISOLATION_SYSTEM_PROPERTY, false);%0A        boolean devMode = getBoolean(DEV_MODE_SYSTEM_PROPERTY, false);%0A        boolean shutdownHook = getBoolean(SHUTDOWN_HOOK_SYSTEM_PROPERTY, true);%0A        boolean implicit = getBoolean(JAVAX_ENTERPRISE_INJECT_SCAN_IMPLICIT, false);%0A        seContainer = seContainerInitializer%0A                .addProperty(ARCHIVE_ISOLATION_SYSTEM_PROPERTY, isolation)%0A                .addProperty(DEV_MODE_SYSTEM_PROPERTY, devMode)%0A                .addProperty(SHUTDOWN_HOOK_SYSTEM_PROPERTY, shutdownHook)%0A                .addProperty(JAVAX_ENTERPRISE_INJECT_SCAN_IMPLICIT, implicit)%0A                
 .ini
@@ -2984,24 +2984,24 @@
 getName());%0A
-
         webS
@@ -3040,11 +3040,353 @@
 );%0A    %7D
+%0A%0A    private boolean getBoolean(String key, boolean defaultValue) %7B%0A        return config.getOptionalValue(key, Boolean.class).orElse(defaultValue);%0A    %7D%0A%0A    public SeContainer getSeContainer() %7B%0A        return seContainer;%0A    %7D%0A%0A    public SeContainerInitializer getSeContainerInitializer() %7B%0A        return seContainerInitializer;%0A    %7D
 %0A%7D%0A

Allow Weld container to be configured via MP Config properties. (#113)