package gr.uoa.di.webui.search;

import eu.dnetlib.domain.functionality.Searchable;
import eu.dnetlib.functionality.webInterface.app.IndexLayoutManager;
import eu.dnetlib.functionality.webInterface.app.WebLayoutManager;
import gr.uoa.di.driver.web.utils.LocaleDescriptionUtil;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * Contains all the needed vocabularies for all the Searchable objects
 * of the web layout.
 * @author kiatrop
 *
 */

public class LayoutManager {

	private Logger logger = Logger.getLogger(LayoutManager.class);

	private WebLayoutManager webLayoutManager = null;
	private IndexLayoutManager indexLayoutManager = null;
	
	private Map<String, Map<Locale, String>> name2Display = new HashMap<String, Map<Locale, String>>();
//	private Map<String, String> display2Name = new HashMap<String, String>();
	private Map<String, String> name2Short = new HashMap<String, String>();
	private Map<String, String> short2Name = new HashMap<String, String>();
	private Map<String, String> name2IndexType = new HashMap<String, String>();
	private Map<String, String> indexType2Name = new HashMap<String, String>();

	public LayoutManager(
			WebLayoutManager webLayoutManager,
			IndexLayoutManager indexLayoutManager) {
		this.webLayoutManager = webLayoutManager;
		this.indexLayoutManager = indexLayoutManager;

		this.initializeVocabularies();
	}
	
	private void initializeVocabularies() {
		Map<Locale,String> old = null;
		
		logger.debug("Initializing vocabularies...");
		for (Searchable s : webLayoutManager.getAllFields()) {
			old = name2Display.put(s.getName(), s.getDescriptionMap());
			
			if (old != null) {
				throw new RuntimeException(
						"Duplicate searchable name " + s.getName());
			}
		/*	old = display2Name.put(s.getDescription(), s.getName());
			if (old != null) {
				throw new RuntimeException(
						"Duplicate searchable description " + s.getName());
			}
			
			name2Short.put(s.getName(), s.getShortDescription());
			old = short2Name.put(s.getShortDescription(), s.getName());
			if (old != null) {
				throw new RuntimeException(
						"Duplicate searchable short description " + s.getName());
			}
		*/	
			name2IndexType.put(s.getName(), s.getIndexType());
			String oldString = null;
			for(String type : s.getIndexType().split(",")) {
				if (!TextUtils.isValidFieldName(type)) {
					throw new RuntimeException(
							"Bad index type " + s.getIndexType());
				}
				oldString = indexType2Name.put(type, s.getName());
				if (oldString != null) {
					throw new RuntimeException(
							"Duplicate map to index field " + type);
				}
			}
		}

	}

	public WebLayoutManager getWebLayoutManager() {
		return webLayoutManager;
	}

	public IndexLayoutManager getIndexLayoutManager() {
		return indexLayoutManager;
	}

	/* TODO remove them please
	public void setWebLayoutManager(WebLayoutManager wlm) {
		webLayoutManager = wlm;
	}

	public void setIndexLayoutManager(IndexLayoutManager ilm) {
		indexLayoutManager = ilm;
	}
	*/

	public String getDisplayFromName(String name, Locale locale) {
		return LocaleDescriptionUtil.getDescription(name2Display.get(name), locale);
	}
	
/*	public String getNameFromDisplay(String display) {
		return display2Name.get(display);
	}
*/
	public String getShortDescFromName(String name) {
		return name2Short.get(name);
	}

	public String getNameFromShortDesc(String shortDesc) {
		return short2Name.get(shortDesc);
	}

	public String getIndexTypeFromName(String name) {
		return name2IndexType.get(name);
	}

	public String getNameFromIndexType(String indexType) {
		return indexType2Name.get(indexType);
	}
}
