package de.uka.ipd.consensus.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import de.uka.ipd.consensus.foundation.ontology.OntologyManagerException;
import de.uka.ipd.consensus.foundation.rating.RatingManager;
import de.uka.ipd.consensus.foundation.rights.DynamicRightsManagerException;
import de.uka.ipd.consensus.foundation.schema.Rateable;
import de.uka.ipd.consensus.foundation.schema.Rating;
import de.uka.ipd.consensus.foundation.schema.User;
import de.uka.ipd.consensus.foundation.schema.Version;

/**
 * TODO
 * @author Thomas Much
 * @version 2006-06-14
 * @deprecated slow... SQLRatingImpl is used instead
 *
 */
public class TM4JRatingImpl extends TM4JObjectWrapper implements Rating {

	private static final org.tm4j.topicmap.Topic[] EMPTY_SCOPE = new org.tm4j.topicmap.Topic[0];
	
	private User creator = null;
	
	
	public TM4JRatingImpl(OntologyManagerImpl omi, org.tm4j.topicmap.TopicMapObject tmObject) {
		super(omi, tmObject);
		throw new UnsupportedOperationException("DO NOT USE - INCOMPLETE AND SLOW IMPLEMENTATION");
	}

	
	protected org.tm4j.topicmap.Topic getWrappedTopic() {
		return (org.tm4j.topicmap.Topic)this.getWrappedObject();
	}
	
	
	private String getValueId() {
		return this.getObjectId() + ".value";
	}


	private org.tm4j.topicmap.Occurrence getValueOccurrence() {
		Collection c = this.getWrappedTopic().getOccurrences();
		if (c != null) {
			final String VALUE_ID = this.getValueId();
			Iterator it = c.iterator();
			while (it.hasNext()) {
				org.tm4j.topicmap.Occurrence occ = (org.tm4j.topicmap.Occurrence)it.next();
				if (VALUE_ID.equals(occ.getID())) {
					return occ;
				}
			}
		}
		return null;
	}
	
	
	private double getValueFromString(String data) {
		try {
			return Double.valueOf( data ).doubleValue();
		}
		catch (Exception e) {
			this.getLoggingManager().warn("Could not convert rating value " + data + " (" + this.toString() + ")");
			return NEUTRAL;
		}
	}

	
	public double getValue() {
		org.tm4j.topicmap.Occurrence occ = this.getValueOccurrence();
		if (occ != null) {
			return this.getValueFromString( occ.getData() );
		}
		else {
			return NEUTRAL;
		}
	}
	
	
	protected void initValue(double value) throws Exception {
		this.getWrappedTopic().createOccurrence( this.getValueId(), null, null, String.valueOf( value ), EMPTY_SCOPE );
	}


	public void setValue(double value) {
		if (this.isHistorical()) return;
		
		RatingManager rm = this.getOntologyManager().getRatingManager();
		
		if ((rm == null) || (!rm.changesAllowed()) || !(rm instanceof TM4JRatingManagerImpl)) return;
		
		TM4JRatingManagerImpl rmi = (TM4JRatingManagerImpl)rm;

		try {
			org.tm4j.topicmap.Occurrence occ = this.getValueOccurrence();

			Rateable rateable = this.getRateable();
			double oldValue = NEUTRAL;

			if (occ != null) {
				oldValue = this.getValueFromString( occ.getData() );
				if (value == oldValue) return;
				occ.setData( String.valueOf( value ) );
			}
			else {
				if (value == oldValue) return;
				this.initValue( value );
			}
			
			if (rateable != null) {
				rmi.fireRatingChanged( this, rateable, oldValue );
			}
		}
		catch (Exception e) {
			this.getLoggingManager().error( "Error setting rating value: " + e );
		}
	}


	public Rateable getRateable() {
		RatingManager rm = this.getOntologyManager().getRatingManager();
		if ((rm != null) && (rm instanceof TM4JRatingManagerImpl)) {
			return ((TM4JRatingManagerImpl)rm).getRateable( this );
		}
		else {
			return null;
		}
	}


	public User getCreator() {
		if (this.creator == null) {
			this.creator = this.getOntologyManager().getCreator( this.getWrappedTopic() );
		}
		return this.creator;
	}


	public Version getVersion() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public String getComment() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void setComment(User setter, String comment) throws OntologyManagerException,DynamicRightsManagerException {
		if (this.isHistorical()) return;

		if ((setter == null) || (!setter.equals( this.getCreator() ))) {
			throw new DynamicRightsManagerException("Only the user himself may change his ratings' comments");
		}
		
		if (comment == null) {
			comment ="";
		}
		
		// TODO
	}


	public Version getRatedVersion() {
		return null;
	}

	
	public Collection getHistory() {
		return Collections.EMPTY_SET;
	}


	public boolean isHistorical() {
		return false;
	}


	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}


	public String toString() {
		// TODO: Version etc.
		return "Rating for " + this.getRateable() + " by " + this.getCreator() + " (value " + this.getValue() + ")";
	}

}
