package de.geis.pdfdownloader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;


public class DownloadPDF {
	
	//Download PDF und Kopiere in Testing Ordner
	public void downloadAndCopy() throws MalformedURLException {
		try {
			System.out.println("Anzahl: " + m_propertyMap.keySet());
			for (File elem : m_propertyMap.keySet()) {
				URL url = new URL(m_propertyMap.get(elem).getURL());
				try (InputStream in = url.openStream()) {
					//Download
					Files.copy(in, Paths.get(Parameters.getFileFolder() + "\\" + m_propertyMap.get(elem).getPdfname() + ".pdf"), 
							StandardCopyOption.REPLACE_EXISTING);
					
					//Kopie in Test Ordner legen
					Files.copy(Paths.get(Parameters.getFileFolder() + "\\" + m_propertyMap.get(elem).getPdfname() + ".pdf"),
							Paths.get(Parameters.getTestFolder() + "\\" + m_propertyMap.get(elem).getPdfname() + ".pdf"), 
							StandardCopyOption.REPLACE_EXISTING);
				} catch (IOException e) {
					System.out.println("ERROR: " + m_propertyMap.get(elem).getPdfname() + " - " + e.toString());
					createErrorProperty(elem, e);
					continue;
				} 
				writePropertyData(elem);
			}
		} catch (Throwable e) {
			System.out.println("ERROR: File nicht gefunden! ");
			e.printStackTrace();
		}
	}
	
	
	//Property Datei fuer den Druckdienst wird geschrieben
	public void writePropertyData(File elem) throws IOException {
		try {
			List<String> lines = Arrays.asList( "STATUS:false",
												"PRINTERNAME:" + m_propertyMap.get(elem).getPrinterName(),
												"JOBNUMMER:" + m_propertyMap.get(elem).getJobNummer(),
												"PDFNAME:" + m_propertyMap.get(elem).getPdfname() + ".pdf",
												"SPOOLNAME:" + m_propertyMap.get(elem).getSpoolName(),
												"PDFEMULATE:" + m_propertyMap.get(elem).getEmulate(),
												"COPIES:" + m_propertyMap.get(elem).getCopies(),
												"ZEBRAPRINT:" + m_propertyMap.get(elem).getZebraPrint()
												);
			
			Path file = Paths.get(Parameters.getConfigFolder() + "\\" + m_propertyMap.get(elem).getPdfname() + ".property");
			Files.write(file, lines, StandardCharsets.UTF_8);
			System.out.println("SUCCESS: Property File geschrieben! " + m_propertyMap.get(elem).getPdfname());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("ERROR: Property File geschrieben!");
		}
	}
	
	private final Map<File, Property> m_propertyMap;
	public DownloadPDF(Map<File, Property> propertyMap) {
		m_propertyMap = propertyMap;
	}
	
	
	private void createErrorProperty(File elem, Exception e) throws IOException {
		FileOutputStream fileOutputStream = null;
		Properties props = new Properties();
		
		try {
			props.setProperty("STATUS DOWNLOAD", ErrorCodes.DOWNLOAD.getValue());
			props.setProperty("URL", m_propertyMap.get(elem).getURL());
			props.setProperty("PDFNAME", m_propertyMap.get(elem).getPdfname());
			props.setProperty("Exception", e.getStackTrace().toString());
			
			fileOutputStream = new FileOutputStream(Parameters.getErrorFolder() + "\\Downloader_" + m_propertyMap.get(elem).getPdfname());
			props.store(fileOutputStream, null);
			
		} catch (Throwable ex) {
			System.out.println(ErrorCodes.DOWNLOAD.getValue() + "-" + ex.getStackTrace());
		} finally {
			fileOutputStream.close();
		}
		
	}

}
