package com.actelion.research.gwt.core;

import java.io.StringReader;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.js.*;

@JsType
@JsNamespace("$wnd.OCL")
@JsExport
public class SDFileParser {

	private com.actelion.research.chem.io.SDFileParser parser;
	
	@JsExport
	public SDFileParser(String sdf, String[] fields) {
		parser = new com.actelion.research.chem.io.SDFileParser(new StringReader(sdf), fields);
	}
	
	public boolean next() {
		return parser.next();
	}
	
	public Molecule getMolecule() {
		return new Molecule(parser.getMolecule());
	}
	
	public String getMolfile() {
		return parser.getNextMolFile();
	}
	
	public String getField(String name) {
		String[] names = parser.getFieldNames();
		for(int i = 0; i < names.length; i++) {
			if(names[i].equals(name)) {
				return parser.getFieldData(i);
			}
		}
		return null;
	}
	
	public String[] getFieldNames() {
		return parser.getFieldNames();
	}
	
	public String getFieldData(int idx) {
		return parser.getFieldData(idx);
	}
	
	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;
	}-*/;
	

	
}

@@ -1028,1469 +1028,7 @@
 %0A%09%7D%0A
-%09%0A%09public static JavaScriptObject parseString(String sdfile, JavaScriptObject options) %7B%0A%09%09return parseNative(sdfile, options);%0A%09%7D%0A%09%0A%09private static native JavaScriptObject parseNative(String sdfile, JavaScriptObject options) /*-%7B%0A%09%09%0A%09%09options = options %7C%7C %7B%0A%09%09%09includeMolfile: true,%0A%09%09%09includeLabels: true%0A%09%09%7D;%0A%09%09%0A%09%09var header=sdfile.substr(0,1000);%0A%09%09var crlf=%22%5Cn%22;%0A%09%09if (header.indexOf(%22%5Cr%5Cn%22)%3E-1) %7B%0A %09%09%09crlf=%22%5Cr%5Cn%22;%0A%09%09%7D else if (header.indexOf(%22%5Cr%22)%3E-1) %7B%0A%09%09%09crlf=%22%5Cr%22;%0A%09%09%7D%0A%09%09var sdfParts=sdfile.split(crlf+%22$$$$%22+crlf);%0A%09%09var molecules=%5B%5D, i = 0, l = sdfParts.length;%0A%09%09for ( ; i %3C l ; i++) %7B%0A%09%09%09var sdfPart=sdfParts%5Bi%5D;%0A%09%09%09var parts=sdfPart.split(crlf+%22%3E%22);%0A%09%09%09if (parts.length%3E0 && parts%5B0%5D.length%3E10) %7B%0A%09%09%09%09var molecule=%7B%7D;%0A%09%09%09%09molecules%5Bmolecules.length%5D = molecule;%0A%09%09%09%09var molfile = parts%5B0%5D+crlf;%0A%09%09%09%09if(options.includeMolfile)%0A%09%09%09%09%09molecule.molfile=molfile;%0A%09%09%09%09molecule.molecule = $wnd.actelion.Molecule.fromMolfile(molfile);%0A%09%09%09%09if(options.includeLabels) %7B%0A%09%09%09%09%09var labels = %7B%7D;%0A%09%09%09%09%09molecule.labels = labels;%0A%09%09%09%09%09for (var j=1, jj=parts.length; j%3Cjj; j++) %7B%0A%09%09%09%09%09%09var lines=parts%5Bj%5D.split(crlf);%0A%09%09%09%09%09%09var from=lines%5B0%5D.indexOf(%22%3C%22);%0A%09%09%09%09%09%09var to=lines%5B0%5D.indexOf(%22%3E%22);%0A%09%09%09%09%09%09var label=lines%5B0%5D.substring(from+1,to);%0A%09%09%09%09%09%09for (var k=1; k%3Clines.length-1; k++) %7B%0A%09%09%09%09%09%09%09if (labels%5Blabel%5D) %7B%0A%09%09%09%09%09%09%09%09labels%5Blabel%5D+=crlf+lines%5Bk%5D;%0A%09%09%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09%09%09labels%5Blabel%5D=lines%5Bk%5D;%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return molecules;%0A%09%7D-*/;%0A%09%0A%0A%09
 %0A%7D%0A

remove broken native SDF parser