package de.uka.ipd.consensus.impl;

import java.util.Properties;

import de.uka.ipd.consensus.foundation.AbstractConsensusFoundationModule;
import de.uka.ipd.consensus.foundation.ConsensusFoundation;
import de.uka.ipd.consensus.foundation.ConsensusFoundationManager;
import de.uka.ipd.consensus.foundation.evolution.EvolutionEvent;
import de.uka.ipd.consensus.foundation.evolution.EvolutionListener;
import de.uka.ipd.consensus.foundation.evolution.EvolutionManager;
import de.uka.ipd.consensus.foundation.logging.LoggingManager;
import de.uka.ipd.consensus.foundation.ontology.OntologyEvent;
import de.uka.ipd.consensus.foundation.ontology.OntologyListener;
import de.uka.ipd.consensus.foundation.ontology.OntologyManager;
import de.uka.ipd.consensus.foundation.rating.RatingEvent;
import de.uka.ipd.consensus.foundation.rating.RatingListener;
import de.uka.ipd.consensus.foundation.rating.RatingManager;
import de.uka.ipd.consensus.foundation.user.UserEvent;
import de.uka.ipd.consensus.foundation.user.UserListener;
import de.uka.ipd.consensus.foundation.user.UserManager;

/**
 * 
 * TODO
 * @author Thomas Much
 * @version 2006-06-14
 */
public class LoggingManagerImpl extends AbstractConsensusFoundationModule implements LoggingManager,UserListener,OntologyListener,EvolutionListener,RatingListener {
	
	private static final String[] levelNames = { "off", "fatal", "error", "warn", "info", "debug", "all" };
	
	private int level = LEVEL_ERROR;

	private boolean running = false;

	
	public void init(Properties config) throws Exception {
		
		running = true;

		String loglevel = config.getProperty( ConsensusFoundation.CF_LOGLEVEL_KEY );
		
		if (loglevel != null) {
			for (int i = 0; i < levelNames.length; i++) {
				if (levelNames[i].equalsIgnoreCase(loglevel)) {
					this.level = i;
					break;
				}
			}
		}
		
		debug("LoggingManager init done. (Loglevel = \"" + levelNames[level] + "\")");
	}

	
	public void initCompleted(Properties config, ConsensusFoundationManager cfm) throws Exception {
		super.initCompleted(config, cfm);

		UserManager um = cfm.getUserManager();
		
		if (um != null) {
			um.addUserListener( this );
		}

		RatingManager rm = cfm.getRatingManager();
		
		if (rm != null) {
			rm.addRatingListener( this );
		}
		
		OntologyManager om = cfm.getOntologyManager();
		
		if (om != null) {
			om.addOntologyListener( this );
		}
		
		EvolutionManager cm = cfm.getEvolutionManager();
		
		if (cm != null) {
			cm.addEvolutionListener( this );
		}

		debug("LoggingManager initCompleted done.");
	}

	
	public void shutdown() {
		if (running) {
			running = false;
			debug("LoggingManager shutdown...");
			// if we opened a log file, we should close it here
		}
	}


	public void setLevel(int level) {
		if (level < LEVEL_OFF) {
			level = LEVEL_OFF;
		}
		else if (level > LEVEL_ALL) {
			level = LEVEL_ALL;
		}
		
		this.level = level;
	}

	
	public int getLevel() {
		return level;
	}

	
	public boolean isDebugEnabled() {
		return (level >= LEVEL_DEBUG);
	}

	
	public boolean isInfoEnabled() {
		return (level >= LEVEL_INFO);
	}

	
	public boolean isWarnEnabled() {
		return (level >= LEVEL_WARN);
	}

	
	public boolean isErrorEnabled() {
		return (level >= LEVEL_ERROR);
	}

	
	public boolean isFatalEnabled() {
		return (level >= LEVEL_FATAL);
	}


	protected void log(int level, Object message) {
		if (this.level >= level) {
			StringBuffer sb = new StringBuffer("[CF ");

			sb.append(levelNames[level]);
			sb.append("] ");
			sb.append(message);

			write(sb);
		}
	}

	
	protected void write(StringBuffer buffer) {
		System.out.println( buffer );
	}


	public void debug(Object message) {
		log(LEVEL_DEBUG,message);
	}

	
	public void info(Object message) {
		log(LEVEL_INFO,message);
	}

	
	public void warn(Object message) {
		log(LEVEL_WARN,message);
	}

	
	public void error(Object message) {
		log(LEVEL_ERROR,message);
	}

	
	public void fatal(Object message) {
		log(LEVEL_FATAL,message);
	}

	
	public void userCreated(UserEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void userLoggedIn(UserEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void userLoggedOut(UserEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void conceptRatingChanged(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void conceptRatingSet(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}

	
	public void conceptRatingRemoved(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}

	
	public void userRatingChanged(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void userRatingSet(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}
	
	
	public void userRatingRemoved(RatingEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void topicCreated(OntologyEvent e) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}
	
	
	public void associationCreated(OntologyEvent e) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen
	}


	public void conceptCommentChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void conceptNameChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void conceptTypeChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void topicClassChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void associationTopicAdded(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void associationTopicRemoved(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}

	
	public void attributeAdded(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void attributeValueChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}
	
	
	public void attributeRemoved(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void occurrenceCreated(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void occurrenceChanged(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void occurrenceRemoved(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void synonymAdded(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void synonymRemoved(OntologyEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void associationDeleted(EvolutionEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void topicDeleted(EvolutionEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public void topicsMerged(EvolutionEvent event) {
		// TODO: Event-Daten z.B. in DB-Tabelle loggen		
	}


	public String getVersion() {
		return "2006-06-14 tm (" + this.getClass().getName() + ")";
	}
}
