package txt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import util.Pair;

public class TXT2OMLTextData {

	public static void generateOML(String textid, String filePath, String outputPath, String version, String iri,
			Pair<String,String> uses, Pair<String,String> ext) throws FileNotFoundException, IOException {
		
		StringBuffer sb = new StringBuffer();
		StringBuffer lines = new StringBuffer();
		StringBuffer sbline = new StringBuffer();
		BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
		String prefix = ext.getSnd();
		
		// header
		sb.append("description " + iri + " as txt {\n");
		sb.append("\tuses "+ uses.getFst() + " as " +  uses.getSnd() + "\n");
		sb.append("\textends "+ ext.getFst() + " as " +  ext.getSnd() + "\n\n");
		
		// ref
		sb.append("\tref ci " + prefix + ":" + textid + " [\n" ); 
	
		String line = "";
		int i = 0;
		while ((line = br.readLine()) != null) {
			sb.append("\t\ttext:hasTextLine " + textid + "-line-" + i + "\n");
			lines.append(generateTxtLine(prefix, textid,i,line,sbline));
			i++;
		}
		sb.append("\t]\n\n");
		sb.append(lines.toString());
		sb.append("}\n");
		br.close();
		FileWriter fw = new FileWriter(new File(outputPath));
		fw.write(sb.toString());
		fw.close();
		
		
	}

	private static String generateTxtLine(String prefix, String textid, int lineNumber, String content, StringBuffer sbline) {
		sbline.delete(0, sbline.length());
		sbline.append("\tci " + textid + "-line-" + lineNumber + " : text:TextLine [\n" );
		sbline.append("\t\ttext:isInTextFile " + prefix + ":" + textid + "\n");
		sbline.append("\t\ttext:isOnLine " + lineNumber + "\n");
		sbline.append("\t\ttext:holdsTextData \"" + content + "\"\n");
		sbline.append("\t]\n\n");
		return sbline.toString();
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		generateOML("drivetrain-limitation-v1", "src/main/resources/txt/limitations.txt", "target/limitations.oml", 
				"v1", "<http://ua.com.be/sdo2l/description/drivetrain/artifacts/limitations#>", 
				new Pair<String, String>("<http://ua.com.be/sdo2l/vocabulary/base/text#>", "text"),
				new Pair<String, String>("<http://ua.com.be/sdo2l/description/drivetrain/artifacts/artifacts#>", "art"));
		generateOML("test-requirements-v1", "src/main/resources/txt/requirements.txt", "target/requirements.oml", 
				"v1", "<http://ua.com.be/sdo2l/description/drivetrain/artifacts/requirements#>", 
				new Pair<String, String>("<http://ua.com.be/sdo2l/vocabulary/base/text#>", "text"),
				new Pair<String, String>("<http://ua.com.be/sdo2l/description/drivetrain/artifacts/artifacts#>", "art"));
	}
	
	
}
