package ca.carleton.gcrc.atlas.module.impl;

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

import ca.carleton.gcrc.atlas.module.DataSource;
import ca.carleton.gcrc.atlas.module.Layer;
import ca.carleton.gcrc.atlas.module.Module;
import ca.carleton.gcrc.atlas.module.WidgetFactory;
import ca.carleton.gcrc.atlas.module.formula.FormulaFactory;
import ca.carleton.gcrc.atlas.module.formula.impl.FormulaFactoryImpl;
import ca.carleton.gcrc.atlas.module.widgets.MapWidget;
import ca.carleton.gcrc.atlas.module.widgets.TextWidget;
import ca.carleton.gcrc.atlas.module.widgets.text.TextBlockHeader;
import ca.carleton.gcrc.atlas.module.widgets.text.TextBlockPara;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringAttribute;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringBreak;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringEmphasis;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringList;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringListItem;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringPeer;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringStrong;
import ca.carleton.gcrc.atlas.module.widgets.text.TextStringTextElement;

public class ModuleImpl extends TopicContainerImpl implements Module,WidgetFactory {
	private String name;
	private Map<String,Layer> layers = new HashMap<String,Layer>();
	private List<DataSource> dataSources = new Vector<DataSource>();

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	public List<Layer> getLayers() {
		List<Layer> result = new Vector<Layer>();
		result.addAll(layers.values());
		return result;
	}

	@Override
	public Layer getLayerFromName(String layerName) {
		Layer layer = layers.get(layerName);
		
		if( null == layer ) {
			layer = new LayerImpl(layerName);
			layers.put(layerName, layer);
		}
		
		return layer;
	}

	public List<DataSource> getDataSources() {
		return dataSources;
	}

	@Override
	public void addDataSource(DataSource dataSource) throws Exception {
		if( true == dataSources.contains(dataSource) ) {
			throw new Exception("Can not add same data source multiple times");
		}
		
		dataSources.add(dataSource);
	}

	@Override
	public void removeDataSource(DataSource dataSource) throws Exception {
		if( false == dataSources.contains(dataSource) ) {
			throw new Exception("Can not find data source for removal");
		}
		
		dataSources.remove(dataSource);
	}
	
	@Override
	public WidgetFactory getWidgetFactory() {
		return this;
	}

	@Override
	public TextWidget createTextWidget() {
		return new TextWidgetImpl();
	}

	@Override
	public MapWidget createMapWidget() {
		return new MapWidgetImpl();
	}

	@Override
	public TextBlockPara createTextBlockPara() {
		return new TextBlockParaImpl();
	}

	@Override
	public TextBlockHeader createTextBlockHeader(int headerLevel) {
		return new TextBlockHeaderImpl(headerLevel);
	}

	@Override
	public TextStringTextElement createTextElement() {
		return new TextStringTextElementImpl();
	}

	@Override
	public TextStringTextElement createTextElement(String text) {
		return new TextStringTextElementImpl(text);
	}

	@Override
	public TextStringBreak createTextStringBreak() {
		return new TextStringBreakImpl();
	}

	@Override
	public TextStringAttribute createTextStringAttribute() {
		return new TextStringAttributeImpl();
	}

	@Override
	public TextStringPeer createTextStringPeer() {
		return new TextStringPeerImpl();
	}

	@Override
	public TextStringList createTextStringList(int listType) throws Exception {
		return new TextStringListImpl(listType);
	}

	@Override
	public TextStringListItem createTextStringListItem() {
		return new TextStringListItemImpl();
	}

	@Override
	public TextStringEmphasis createTextStringEmphasis() {
		return new TextStringEmphasisImpl();
	}

	@Override
	public TextStringStrong createTextStringStrong() {
		return new TextStringStrongImpl();
	}

	@Override
	public FormulaFactory getFormulaFactory() {
		return new FormulaFactoryImpl();
	}

}

