package org.openoffice.wysiwyg.uju;

import ag.ion.bion.officelayer.application.*;
import ag.ion.bion.officelayer.document.DocumentDescriptor;
//import ag.ion.bion.officelayer.document.IDocument;
import ag.ion.bion.officelayer.document.IDocumentDescriptor;
import ag.ion.bion.officelayer.text.ITextDocument;
import ag.ion.bion.officelayer.internal.presentation.PresentationDocument;
import ag.ion.bion.officelayer.filter.HTMLFilter;
import ag.ion.bion.officelayer.filter.TextFilter;
import ag.ion.bion.officelayer.filter.IFilter;
//import ag.ion.bion.officelayer.filter.TextFilter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
//import java.io.FileNotFoundException;
import java.io.IOException;
import ag.ion.bion.officelayer.internal.spreadsheet.SpreadsheetDocument;
import java.io.FileReader;
import java.io.File;
import java.util.*;
//import org.openoffice.java.accessibility.Application;

public class ClsMain {
	
	private static String officeHome = "/";
	private static String pathFile = "/";
	private static String logFile = "/log.txt";
	private static String outPath = "/";

	public static String runLocal() {
		ClsMain.pathFile = "\\singles\\tomcat\\webapps\\wy\\Testdateien\\Pfade.txt";
		ClsMain.officeHome = "c:/bla/blubb";
		return "local done";
	}
	
	//wird zurzeit nicht aufgerufen
/*	public static String runTomcat() {
		String returnString = "";
		
		ClsMain.pathFile = "/www/tomcat/webapps/wy/Testdateien/Pfade.txt";
		ClsMain.officeHome = "/opt/openoffice.org2.1/program/";
		ClsMain.logFile = "/www/tomcat/webapps/wy/log.txt";
		ClsMain.outPath = "/www/tomcat/webapps/wy/Dateiablage/";
	
		try {
			//returnString = returnString + ClsMain.run();
		} catch (Exception e) {
			//returnString = returnString + e.getMessage();
		}
		// Datei auslesen
		//File f = new File(Pfade);	
		//String absPath = f.getCanonicalPath();
		
		return returnString;
	}	*/

	public static String run(String offHo, String DatAblage, String path) throws IOException {
		String returnString = "";
		String officeHome = offHo;
		String Pfade = path;
		
		//BufferedWriter logWriter = new BufferedWriter(new FileWriter(ClsMain.logFile));
		// Datei auslesen
		//File f = new File(Pfade);	
		//String absPath = f.getCanonicalPath();
		//logWriter überschreibt bisheriges Logfile
		//logWriter.write("Testweise");
		/*logWriter.newLine();
		 * logWriter.write("TestweiseUlle ");
		logWriter.newLine();
		*/
		FileReader fr = new FileReader(Pfade);
		BufferedReader br = new BufferedReader(fr);
		String record = null;
		try {
			while ( (record=br.readLine()) != null ) { 
				
				//Dateinamen ermitteln
				String dEndung = "";
				String dName = "";
				char[] chars = new char[ 3 ];
				record.getChars(record.length()-3, record.length(), chars, 0);
				dEndung = new String(chars);
				int indBSlash = record.lastIndexOf("\\");
				int indDot = record.lastIndexOf(".");
				//Berechne die Laege des Dateinamens
				int rechne = indDot - (indBSlash+1);
				char[] filename = new char [rechne];
				record.getChars(indBSlash+1, indDot, filename, 0);
				dName = new String(filename);
				//dName ist Dateiname; dEndung die dazugehoerige Endung
				returnString = returnString + dName + "." + dEndung + "\n";				
		
				//Pfad zum Abspeichern; hier: das Tomcat-Webverzeichnis
				String saveHtml = DatAblage + dName + "_conv.html";
				String saveTxt = DatAblage + dName + "_conv.txt";
				//handelt es sich um ein TextDocument, dann im Writer oeffnen
				if (dEndung.compareTo("txt") == 0 || dEndung.compareTo("doc") == 0 || dEndung.compareTo("odt") == 0) {
					returnString = returnString + "Writer conversion to" + saveTxt + "\n";
					returnString = returnString + wrap(officeHome, record, saveTxt, TextFilter.FILTER );
				}
				
				//handelt es sich um ein Sheet, dann im Calc oeffnen, ACHTUNG: csv-Format noch nicht getestet!
				if (dEndung.compareTo("xls") == 0 || dEndung.compareTo("ods") == 0 || dEndung.compareTo("csv") == 0) {
					returnString = returnString + "Calc conversion to" + saveTxt + "\n";
					wrap_format(officeHome, record, saveTxt, TextFilter.FILTER);
				}
				
				// handelt es sich um eine Praesi, dann im Impress oeffnen
				if (dEndung.compareTo("ppt") == 0 || dEndung.compareTo("odp") == 0) {
					returnString = returnString + "Impress conversion to" + saveHtml + "\n";
					wrap_pres(officeHome, record, saveHtml, HTMLFilter.FILTER);
				}
			}
		} catch (IOException e) {
			returnString = returnString + e.getMessage();
			// 
		}
		//logWriter.close();
		
		return returnString;
	}
	
	public static String wrap(String offHome, String indexLis, String htmlExport, IFilter ifil) {
		String returnString = ""; 
		HashMap hashMap = new HashMap(3);
		//hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.REMOTE_APPLICATION);
        //hashMap.put(IOfficeApplication.APPLICATION_HOST_KEY, "localhost");
		//hashMap.put(IOfficeApplication.APPLICATION_PORT_KEY, "80");	
		//FileOutputStream fso = new FileOutputStream(new File(DaNa));
		
		hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
		hashMap.put(IOfficeApplication.APPLICATION_HOME_KEY, offHome);
		
		try {
			IDocumentDescriptor docuDes = new DocumentDescriptor();
			docuDes.setHidden(true);
			//IOfficeApplication officeApp = OfficeApplicationRuntime.getLocalOfficeApplication();
			//officeApp.setConfiguration(hashMap);
			IOfficeApplication officeApp = OfficeApplicationRuntime.getApplication(hashMap);
			officeApp.activate();
			
			ITextDocument txtDoc = (ITextDocument) officeApp.getDocumentService().loadDocument(indexLis, docuDes);
			txtDoc.getPersistenceService().export(htmlExport, ifil);
			txtDoc.close();
			//Eigentlich hier die OpenOffice Applikation beenden
			//officeApp.deactivate();
			//officeApp.getDesktopService().terminate();
		}
		catch (Throwable ta) {
			returnString = returnString + ta.getMessage();
			ta.printStackTrace();
		}
		
		return returnString;
	}
	
	public static void wrap_format(String offHome, String indexLis, String htmlExport, IFilter ifil) {
		HashMap hashMap = new HashMap(3);
		hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
		hashMap.put(IOfficeApplication.APPLICATION_HOME_KEY, offHome);
		
		try {
			IDocumentDescriptor docuDes = new DocumentDescriptor();
			docuDes.setHidden(true);
			IOfficeApplication officeApp = OfficeApplicationRuntime.getApplication(hashMap);
			officeApp.activate();
			SpreadsheetDocument ssd = (SpreadsheetDocument) officeApp.getDocumentService().loadDocument(indexLis, docuDes);
			ssd.getPersistenceService().export(htmlExport, ifil);
			ssd.close();
			//officeApp.deactivate();
			//officeApp.getDesktopService().terminate();
		}
		catch (Throwable ta) {
			ta.printStackTrace();
		}
	}

	public static void wrap_pres(String offHome, String indexLis, String htmlExport, IFilter ifil) {
		HashMap hashMap = new HashMap(3);
		hashMap.put(IOfficeApplication.APPLICATION_TYPE_KEY, IOfficeApplication.LOCAL_APPLICATION);
		hashMap.put(IOfficeApplication.APPLICATION_HOME_KEY, offHome);
		
		try {
			IDocumentDescriptor docuDes = new DocumentDescriptor();
			docuDes.setHidden(true);
			IOfficeApplication officeApp = OfficeApplicationRuntime.getApplication(hashMap);
			officeApp.activate();
			PresentationDocument pdoc = (PresentationDocument) officeApp.getDocumentService().loadDocument(indexLis, docuDes);
			pdoc.getPersistenceService().export(htmlExport, ifil);
			pdoc.close();
			//officeApp.deactivate();
			//officeApp.getDesktopService().terminate();
		}
		catch (Throwable ta) {
			ta.printStackTrace();
		}
	}
}

