package org.cheminfo.actelion.client;

//import java.io.Reader;
//import java.io.StringReader;

import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.ExportPackage;
import org.timepedia.exporter.client.Exportable;

//import com.actelion.research.chem.StereoMolecule;
import com.google.gwt.core.client.JavaScriptObject;

@ExportPackage(value="actelion")
@Export
public class SDFileParser implements Exportable {

	
//	public static JavaScriptObject parseString(String sdfile) {
//		return parse(new StringReader(sdfile));
//	}
//	
//	private static JavaScriptObject parse(Reader reader) {
//		JavaScriptObject resultArray = JavaScriptObject.createArray();
//		com.actelion.research.io.SDFileParser parser = new com.actelion.research.io.SDFileParser(reader);
//		
//		while (parser.next()) {
//			addMolecule(resultArray, parser.getMolecule(), parser.getNextMolFile());
//		}
//		
//		return resultArray;
//	}
//	
//	private static native void addMolecule(JavaScriptObject array, StereoMolecule mol, String molfile) /*-{
//		array[array.length] = {
//			molecule: new $wnd.actelion.Molecule(mol),
//			molfile: molfile
//		};
//	}-*/;
	
	public static JavaScriptObject parseString(String sdfile, JavaScriptObject options) {
		return parseNative(sdfile, options);
	}
	
	private static native JavaScriptObject parseNative(String sdfile, JavaScriptObject options) /*-{
		
		options = options || {
			includeMolfile: true,
			includeLabels: true
		};
		
		var header=sdfile.substr(0,1000);
		var crlf="\n";
		if (header.indexOf("\r\n")>-1) {
 			crlf="\r\n";
		} else if (header.indexOf("\r")>-1) {
			crlf="\r";
		}
		var sdfParts=sdfile.split(crlf+"$$$$"+crlf);
		var molecules=[], i = 0, l = sdfParts.length;
		for ( ; i < l ; i++) {
			var sdfPart=sdfParts[i];
			var parts=sdfPart.split(crlf+">");
			if (parts.length>0 && parts[0].length>10) {
				var molecule={};
				molecules[molecules.length] = molecule;
				var molfile = parts[0]+crlf;
				if(options.includeMolfile)
					molecule.molfile=molfile;
				molecule.molecule = $wnd.actelion.Molecule.fromMolfile(molfile);
				if(options.includeLabels) {
					var labels = {};
					molecule.labels = labels;
					for (var j=1, jj=parts.length; j<jj; j++) {
						var lines=parts[j].split(crlf);
						var from=lines[0].indexOf("<");
						var to=lines[0].indexOf(">");
						var label=lines[0].substring(from+1,to);
						for (var k=1; k<lines.length-1; k++) {
							if (labels[label]) {
								labels[label]+=crlf+lines[k];
							} else {
								labels[label]=lines[k];
							}
						}
					}
				}
			}
		}
		return molecules;
	}-*/;
	

	
}

@@ -36,35 +36,8 @@
 t;%0A%0A
-//import java.io.Reader;%0A//
 impo
@@ -61,16 +61,16 @@
 Reader;%0A
+
 %0Aimport 
@@ -209,18 +209,16 @@
 table;%0A%0A
-//
 import c
@@ -259,16 +259,16 @@
 lecule;%0A
+
 import c
@@ -405,16 +405,61 @@
 ble %7B%0A%0A%09
+com.actelion.research.io.SDFileParser parser;
 %0A//%09publ
@@ -1161,16 +1161,16 @@
 %0A//%09%09%7D;%0A
-
 //%09%7D-*/;
@@ -1172,16 +1172,725 @@
 %7D-*/;%0A%09%0A
+%09public SDFileParser(String sdf) %7B%0A%09%09this(sdf, null);%0A%09%7D%0A%09%0A%09public SDFileParser(String sdf, String%5B%5D fields) %7B%0A%09%09parser = new com.actelion.research.io.SDFileParser(new StringReader(sdf), fields);%0A%09%7D%0A%09%0A%09public boolean next() %7B%0A%09%09return parser.next();%0A%09%7D%0A%09%0A%09public Molecule getMolecule() %7B%0A%09%09return new Molecule(parser.getMolecule());%0A%09%7D%0A%09%0A%09public String getField(String name) %7B%0A%09%09String%5B%5D names = parser.getFieldNames();%0A%09%09for(int i = 0; i %3C names.length; i++) %7B%0A%09%09%09if(names%5Bi%5D.equals(name)) %7B%0A%09%09%09%09return parser.getFieldData(i);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return null;%0A%09%7D%0A%09%0A%09public String%5B%5D getFieldNames() %7B%0A%09%09return parser.getFieldNames();%0A%09%7D%0A%09%0A%09public String getFieldData(int idx) %7B%0A%09%09return parser.getFieldData(idx);%0A%09%7D%0A%09%0A
 %09public 

add methods to SDFileParser