package de.unikoblenz.isweb.xcosima;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.uniko.isweb.commlite.ASCIIContent;
import de.uniko.isweb.commlite.ASCIILocalization;
import de.uniko.isweb.commlite.ContentLocalization;
import de.uniko.isweb.commlite.ImageContent;
import de.uniko.isweb.commlite.StillRegionLocalization;
import de.unikoblenz.isweb.xcosima.agent.Address;
import de.unikoblenz.isweb.xcosima.agent.Agent;
import de.unikoblenz.isweb.xcosima.communication.Conversation;
import de.unikoblenz.isweb.xcosima.communication.Email;
import de.unikoblenz.isweb.xcosima.communication.InstantMessage;
import de.unikoblenz.isweb.xcosima.io.InformationObject;
import de.unikoblenz.isweb.xcosima.ontologies.RealModule;
import de.unikoblenz.isweb.xcosima.realization.FileSystemRealization;
import de.unikoblenz.isweb.xcosima.realization.HttpRealization;
import de.unikoblenz.isweb.xcosima.store.DataStore;
/**
 * Shows how to use X-COSIMA and the integrated COMMLite API to generate
 * metadata about conversations, realizations and annotations/classifications.
 * @author Thomas Franz
 * http://isweb.uni-koblenz.de
 *
 */
public class Example {
	protected static Log l = LogFactory.getLog(Example.class);



	public static void main(String[] args) throws Exception {
		// initializes the X-COSIM Datastore
		DataStore ds = DataStore.getInstance();		
		Example ex = new Example();
		// run the sample scenario
		ex.sampleScenario();
		ds.exportRepositoryN3(new FileOutputStream(new File("Example.n3")));
		System.out.println("Content of repository exported to Example.n3");		
	}

	/**
	 * A sample execution that makes use of several X-COSIMA and COMMLite
	 * objects. Intended for illustrations.
	 * @throws Exception
	 */
	public void sampleScenario() throws Exception {
		// Create some addresses and agents carsten and thomas

		Address addressCarsten = new Address("saathoff@uni-koblenz.de");
		Agent carsten = new Agent(addressCarsten);

		Address addressThomas = new Address("franz@uni-koblenz.de");
		Agent thomas = new Agent(addressThomas);

		// Create an information object representing the information given by some image
		InformationObject bikeImage = new InformationObject("bikeImage");

		// Create realization metadata for bikeImage:
		// Storage on harddisk, Format jpeg 
		FileSystemRealization bikeImageRealization = 
			new FileSystemRealization(
					bikeImage, 					
					new File(new URI("file:/myPics/bikeImage.jpg")),
					RealModule.I_jpeg);

		// Create a conversation
		Conversation conversation = new Conversation();

		// bikeImage was transferred as email attachment between thomas and
		// Carsten within the created conversation
		Email email = new Email(conversation, thomas, addressThomas, carsten, addressCarsten);
		email.addAttachment(bikeImageRealization);
		email.setBodyText("Hi Carsten, I attached some image of my new bike :)");

		// Carsten stores the attachment on the file system ...
		FileSystemRealization carstensRealization = 
			new FileSystemRealization(
					bikeImage, 
					new File(new URI("file:/home/carsten/pics/bikeImage.jpg")), 
					RealModule.I_jpeg);

		Address carstenIM=new Address("carsten@kater.uni-koblenz.de");
		carsten.addAddress(carstenIM);
		Address thomasIM=new Address("tom@kater.uni-koblenz.de");
		thomas.addAddress(thomasIM);
		// Carsten replies by instant message within the same conversation
		InstantMessage carstenReply = new InstantMessage(conversation, carsten, carstenIM, thomas,
				thomasIM);
		carstenReply.setText("Wow, that's really a nice bike! How much did you pay for it?");		
		
		// Carsten annotates the text "nice bike" contained in the instant message with the image of the bike
		
		// Get the content localization that is needed by COMM
		ContentLocalization messageContentLocation = 
			carstenReply.getMessageIO().getRealizations().get(0).toContentLocalization();

		// Create a COMMLite content object from the email so that it can be annotated using COMMLite
		ASCIIContent messageText = new ASCIIContent(
				carstenReply.getMessageIO().getResource().getJavaUri(), 
				messageContentLocation);
		// Create an ASCIILocalization starting at char 21 and ending at
		// character 21+9. This should locate the string "nice bike" in the email body
		ASCIILocalization niceBike=new ASCIILocalization(21, 9, messageContentLocation);
		// annotate the part of emailText specified by the niceBike localization with the bikeImage 
		messageText.addAnnotation(
				bikeImage.getResource().getJavaUri(), 
				niceBike);
		
		// Carsten annotates the image itself
		ImageContent ic = new ImageContent(carstensRealization.getRealizedInformation()
				.getResource().getJavaUri(), carstensRealization.toContentLocalization());

		// Carsten annotates the image as a whole with 'SportsBike'
		ic.addAnnotation(new URI("http://myBikeOntology/RedBike"));

		// Carsten annotates a particular polygon with '4-cylinder-engine'
		ic.addAnnotation(new URI("http://myBikeOntology/4-cylinder-engine"),
				new StillRegionLocalization(
						new Rectangle(140, 120), 
						carstensRealization.toContentLocalization()));

		// thomas transforms the image to the PNG format and 
		// puts it on a web server
		@SuppressWarnings({ "unused"})
		HttpRealization webServerRealization = 
			new HttpRealization(bikeImage, 
					new URL("http://www.x-media.org/pics/bike.png"), 
					RealModule.I_png);		
	}

}