package de.geis.pdfdownloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;



public class PropertyDaten extends Thread {
		
	String m_zwischenSpeicher = null;
	
	File file = null;
	
	private File getFile() {
		return file;
	}

	public PropertyDaten(String file) {
		m_zwischenSpeicher = file;
	}
	
	private FileInputStream loadFileStream (File fileName) {
		return loadFileStream(fileName, 0);
	}
	
	private FileInputStream loadFileStream (File fileName, int zaehler) {
		FileInputStream fileInputStream = null;
		if (zaehler == Parameters.getVersuche()) {
			return null;
		}
		try {
			fileInputStream = new FileInputStream(fileName);
		} catch (Throwable e) {
			try {
				if(zaehler < 2) { 
					TimeUnit.MILLISECONDS.sleep(Parameters.getDelayTime());
				} else {
					TimeUnit.MILLISECONDS.sleep(Parameters.getDelayTime() + 10);
				}
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			zaehler++;
			return loadFileStream(fileName, zaehler);
		} 
		return fileInputStream;
	}
	
	private int counter = 0;
	
	
	private void loadConfigDatein() throws Exception{
		
		try {			
		Map<File, Property>propertyMap = new HashMap<File, Property>();
		Properties props = null;
		FileInputStream fileInputStream = null;
		Property property = null;
		DownloadPDF downloadMap = null;

		counter = 0;
			try {	
//				setFile(new File(m_zwischenSpeicher));	
				props = new Properties();
				counter++;
				property = new Property();
				counter++;
				fileInputStream = loadFileStream(getFile());
				counter++;
				props.load(fileInputStream);
				counter++;
				property.setStatus(props.getProperty("STATUS"));
				counter++;
				property.setPdfname(props.getProperty("PDFNAME"));
				counter++;
				property.setURL(props.getProperty("URL"));
				counter++;
				property.setPrinterName(props.getProperty("PRINTERNAME"));
				counter++;
				property.setJobNummer(props.getProperty("JOBNUMMER"));
				counter++;
				property.setSpoolName(props.getProperty("SPOOLNAME"));
				counter++;
				property.setEmulate(props.getProperty("PDFEMULATE"));
				counter++;
				property.setCopies(props.getProperty("COPIES"));
				counter++;
				property.setZebraPrint(props.getProperty("ZEBRAPRINT"));
				counter++;
				propertyMap.put(file, property);
				counter++;	
			} catch (Exception e) {
				createErrorProperty(e);
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					fileInputStream = null;
				}
				Files.move(Paths.get(file.getPath()), Paths.get("C:\\FTPRoot\\orginal\\ORGINAL_" + m_zwischenSpeicher), StandardCopyOption.REPLACE_EXISTING);
			}

		if (propertyMap.size() > 0) {
			try {
				downloadMap = new DownloadPDF(propertyMap);
				downloadMap.downloadAndCopy();	
			}  catch (Exception e) {
				createErrorProperty(e);
			}
		}
		}catch (Exception e) {
			createErrorProperty(e);
		}
	}
	
	private void createErrorProperty(Exception e) throws IOException {
		FileOutputStream fileOutputStream = null;
		Properties props = new Properties();
		
		try {
			System.out.println("ERROR: "+ m_zwischenSpeicher + "Counter:" + counter +"\n " + e.getStackTrace());
			props.setProperty("STATUS DOWNLOAD", ErrorCodes.DOKUMENTE.getValue());
			props.setProperty("Exception", e.toString());
			props.setProperty("COUNTER", counter + "");
			fileOutputStream = new FileOutputStream(Parameters.getErrorFolder() + "\\Downloader_" + m_zwischenSpeicher);
			props.store(fileOutputStream, null);
			
			Files.move(Paths.get(file.getPath()), Paths.get("C:\\FTPRoot\\orginal\\ORGINAL_" + m_zwischenSpeicher), StandardCopyOption.REPLACE_EXISTING);
			
		} catch (Throwable ex) {
			System.out.println(ErrorCodes.DOKUMENTE.getValue() + ":" + m_zwischenSpeicher + "\n" + ex.getStackTrace());
		} finally {
			fileOutputStream.close();
		}
		
	}
	
	@Override
	public void run() {		
		try {
			System.out.println("--- LADE PROPERTIES ---" + m_zwischenSpeicher);
			file = new File(Parameters.getDownloadFolder() + "\\" + m_zwischenSpeicher);
			loadConfigDatein();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
	}
}
