package infodoc.ui.common;

import infodoc.core.InfodocConstants;
import infodoc.core.container.InfodocContainerFactory;
import infodoc.core.dto.User;
import infodoc.core.ui.common.InfodocMainWindow;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import enterpriseapp.EnterpriseApplication;
import enterpriseapp.hibernate.ContainerFactory;
import enterpriseapp.ui.window.Module;

public class InfodocApplication extends EnterpriseApplication implements Serializable {
	
	private static final Logger logger = LoggerFactory.getLogger(InfodocApplication.class);
	
	private static final String BUILT_IN_MODULES = "infodoc.ui.module.configuration.ConfigurationModule,infodoc.ui.module.admin.AdminModule,infodoc.ui.module.help.HelpModule";

	private static final long serialVersionUID = 1L;
	
	private static boolean firstInit = true;
	
	@Override
	public void init() {
		super.init();
		
		ContainerFactory.init(new InfodocContainerFactory());
		setTheme("infodoc");
		
		User user = (User) getUser();
		
		if(user != null && user.getTimeZone() != null) {
			setTimeZoneId(user.getTimeZone());
		}
		
		InfodocMainWindow mainWindow = new InfodocMainWindow((User) getUser(), getModules((User) getUser()));
		setMainWindow(mainWindow);
		
		if(firstInit) {
			Bootstrap.run();
			firstInit = false;
		}
	}

	public List<Module> getModules(User user) {
		ArrayList<Module> modules = new ArrayList<Module>();
		String modulesString = BUILT_IN_MODULES;
		
		if(InfodocConstants.infodocModules != null && !InfodocConstants.infodocModules.isEmpty()) {
			modulesString = InfodocConstants.infodocModules.replace(" " , "") + "," + modulesString;
		}
		
		String[] moduleClasses = modulesString.split(",");
		
		for(String moduleClass : moduleClasses) {
			try {
				logger.info("Loading module " + moduleClass + "...");
				Module module = (Module) Class.forName(moduleClass).newInstance();
				modules.add(module);
				
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(InfodocConstants.uiErrorModuleNotFound + " " + moduleClass, e);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		
		return modules;
	}
	
}

@@ -279,66 +279,8 @@
 t;%0A%0A
-import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 impo
@@ -502,99 +502,8 @@
 %7B%0A%09%0A
-%09private static final Logger logger = LoggerFactory.getLogger(InfodocApplication.class);%0A%09%0A
 %09pri
@@ -1689,66 +1689,8 @@
 y %7B%0A
-%09%09%09%09logger.info(%22Loading module %22 + moduleClass + %22...%22);%0A
 %09%09%09%09

Delete log message. Too noisy.