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

import java.util.List;
import java.util.Vector;

import ca.carleton.gcrc.atlas.module.Topic;
import ca.carleton.gcrc.atlas.module.TopicContainer;
import ca.carleton.gcrc.atlas.module.Widget;

public class TopicImpl extends TopicContainerImpl implements Topic {

	static private int idCount = 0;
	static synchronized private String getStaticId() {
		String id = "_topic_"+idCount;
		++idCount;
		return id;
	}

	private String title;
	private TopicContainer container;
	private List<WidgetImpl> widgets = new Vector<WidgetImpl>();
	
	public TopicImpl() {
		// Assign a default id
		setId( getStaticId() );
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	@Override
	public TopicContainer getContainer() {
		return container;
	}
	public void setContainer(TopicContainer container) {
		this.container = container;
	}
	
	public List<Widget> getWidgets() {
		List<Widget> result = new Vector<Widget>();
		result.addAll(widgets);
		return result;
	}
	
	@Override
	public void addWidget(Widget widget) throws Exception {
		if( false == (widget instanceof WidgetImpl) ) {
			throw new Exception("Trying to add a widget with wrong class hierarchy: "+widget.getClass().getName());
		}
		WidgetImpl widgetImpl = (WidgetImpl)widget;
		widgets.add(widgetImpl);
	}
	@Override
	public void removeWidget(Widget widget) throws Exception {
		if( false == widgets.contains(widget) ) {
			throw new Exception("Can not remove widget because it is not found");
		}
		
		widgets.remove(widget);
	}

}
