package com.inepex.ineom.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.inepex.inei18n.shared.CurrentLang;
import com.inepex.ineom.shared.descriptor.DescriptorBase;
import com.inepex.ineom.shared.descriptor.DescriptorStore;
import com.inepex.ineom.shared.descriptor.FDesc;
import com.inepex.ineom.shared.descriptor.ObjectDesc;

public class MultiLangDescStore implements DescriptorStore {

	private final String DEFAULT_LANG = "en"; 
	
	private Map<String, DescriptorStore> storeByLang = new HashMap<String, DescriptorStore>();
	private final Provider<CurrentLang> currLangProvider;
	
	@Inject
	public MultiLangDescStore(Provider<CurrentLang> currLangProvider) {
		this.currLangProvider = currLangProvider;
	}
	
	public void addStore(String lang, DescriptorStore store) {
		storeByLang.put(lang, store);
	}
	
	private DescriptorStore get() {
		String lang = null;
		if (currLangProvider == null)
			lang = DEFAULT_LANG;
		else
			lang = currLangProvider.get().getCurrentLang();
		return storeByLang.get(lang);
	}

	// Bridge methods follow
	
	@Override
	public ObjectDesc getOD(String objectDescriptorName) {
		return get().getOD(objectDescriptorName);
	}


	@Override
	public void registerObjectDesc(ObjectDesc descriptor) {
		get().registerObjectDesc(descriptor);
	}

	@Override
	public FDesc getRelatedFieldDescrMultiLevel(ObjectDesc baseOD, List<String> path) {
		return get().getRelatedFieldDescrMultiLevel(baseOD, path);
	}

	@Override
	public <D extends DescriptorBase> D getDefaultTypedDesc(String objDescName, Class<D> clazz) {
		return get().getDefaultTypedDesc(objDescName, clazz);
	}

	@Override
	public <D extends DescriptorBase> D getNamedTypedDesc(String objDescName, String namedDescName, Class<D> clazz) {
		return get().getNamedTypedDesc(objDescName, namedDescName, clazz);
	}

	@Override
	public <D extends DescriptorBase> void addDefaultTypedDesc(String objDescName, D defaultDesc) {
		get().addDefaultTypedDesc(objDescName, defaultDesc);
	}

	@Override
	public <D extends DescriptorBase> void addNamedTypedDesc(String objDescName, String namedDescName, D namedDesc) {
		get().addNamedTypedDesc(objDescName, namedDescName, namedDesc);
	}

	@Override
	public void registerDescriptors(ObjectDesc descriptor, DescriptorBase... defaultDescriptors) {
		get().registerDescriptors(descriptor, defaultDescriptors);
	}
	
	public DescriptorStore getCurrentLanguageDescriptorStore(){
		return get();
	}

	@Override
	public String getOdNames() {
		StringBuilder sb = new StringBuilder();
		for(String lang : storeByLang.keySet()) {
			if(sb.length()>0)
				sb.append("\n");
			sb.append(lang);
			sb.append(": (");
			sb.append(storeByLang.get(lang).getOdNames());
			sb.append(")");
		}
		
		return sb.toString();
	}
}


@@ -99,16 +99,74 @@
 l.Map;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0A
 import c
@@ -190,16 +190,16 @@
 Inject;%0A
-
 import c
@@ -271,16 +271,81 @@
 ntLang;%0A
+import com.inepex.ineom.shared.descriptor.ClientDescriptorStore;%0A
 import c
@@ -617,16 +617,108 @@
 rStore %7B
+%0A%09%0A%09private static final Logger _logger = LoggerFactory.getLogger(MultiLangDescStore.class);
 %0A%0A%09priva
@@ -1248,16 +1248,16 @@
 %0A%09%09else%0A
-
 %09%09%09lang 
@@ -1299,16 +1299,246 @@
 Lang();%0A
+%09%09if (!storeByLang.containsKey(lang))%7B%0A%09%09%09_logger.warn(%22MultiLangDescStore hasn't been initialized%22);%0A%09%09%09ClientDescriptorStore localizedDescStore = new ClientDescriptorStore();%0A%09%09%09storeByLang.put(lang, localizedDescStore);%09%09%09%0A%09%09%7D%0A
 %09%09return

handle when multilangdescstore is not initialized