package de.geis.printserver;

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


public class PropertyDaten {
		
	public InputStream loadFileStream (String fileName) {
		return loadFileStream(fileName, 0);
	}
	
	public InputStream loadFileStream (String fileName, int zaehler) {
		InputStream fileInputStream = null;
		
		if (zaehler == Parameters.getVersuche()) {
			return null;
		}
		
		try {
			fileInputStream = Files.newInputStream(Paths.get(Parameters.getConfigFolder() + "\\" + fileName));
			
		} catch (Exception e) {
			try {
				TimeUnit.MILLISECONDS.sleep(Parameters.getDelayTime());
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			zaehler++;
			return loadFileStream(fileName, zaehler);
		} 
		return fileInputStream;
	}
	
	
	public void loadConfigDatein() {
		File[] listoffiles = Parameters.getConfigFolder().listFiles();
		Map<File, Property>propertyMap = new HashMap<File, Property>();
		Properties props = null;
		InputStream fileInputStream = null;
		Property property = null;
		PrintMap printMap = null;
		
		for (File file : listoffiles) {
			try {				
				props = new Properties();
				property = new Property();
				fileInputStream = loadFileStream(file.getName());
				
				if(fileInputStream == null) {
					createErrorProperty(file);
					continue;
				}
				
				props.load(fileInputStream);
				
				property.setStatus(props.getProperty("STATUS"));
				property.setPrintername(props.getProperty("PRINTERNAME"));
				property.setJobnummer(props.getProperty("JOBNUMMER"));
				property.setPdfname(Parameters.getFileFolder() + "\\" + props.getProperty("PDFNAME"));
				property.setSpoolname(props.getProperty("SPOOLNAME"));
				property.setPdfemulate(Boolean.parseBoolean(props.getProperty("PDFEMULATE", "false")));
				property.setCopies(Integer.parseInt(props.getProperty("COPIES", "1")));
				property.setZebraPrinter(Boolean.parseBoolean(props.getProperty("ZEBRAPRINT", "false")));

				propertyMap.put(file, property);
				
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (fileInputStream != null) {
					try {
						fileInputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					fileInputStream = null;
				}
				file.delete();
			}

		}
		if (propertyMap.size() > 0) {
			printMap = new PrintMap(propertyMap);
			System.out.println("Start PRINT");
			printMap.start();	
			
		}
	}
	
	private void createErrorProperty(File file) throws IOException {
		FileOutputStream fileOutputStream = null;
		Properties props = new Properties();
		
		try {
			props.setProperty("STATUS", ErrorCodes.DOKUMENT.getValue());
			fileOutputStream = new FileOutputStream(Parameters.getErrorFolder() + "\\" + file.getName());
			props.store(fileOutputStream, null);
			
		} catch (Exception e) {
			System.out.println(ErrorCodes.DOKUMENT.getValue());
		} finally {
			fileOutputStream.close();
		}
		
	}
}
