package systems.sdw;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import org.mustangproject.BankDetails;
import org.mustangproject.Contact;
import org.mustangproject.Invoice;
import org.mustangproject.Item;
import org.mustangproject.Product;
import org.mustangproject.TradeParty;
import org.mustangproject.ZUGFeRD.IZUGFeRDExporter;
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA1;
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3;
import org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromPDFA;
import org.mustangproject.ZUGFeRD.ZUGFeRDImporter;

public class Readertest {

	public static void main(String[] args) {

		File pdf=new File("/Users/benediktwismans/tmp/73788e01-e5ff-440b-877c-f3b7c4f937ff.pdf");
		
		try {
			checkZugferdDaten(pdf);
		} catch (IOException e) {
			e.printStackTrace();
		}   
	}

	
	
	
	private static void write(PDPageContentStream contentStream, int textx, int texty, String text) throws IOException  {
	
		contentStream.beginText();
		contentStream.setFont(new PDType1Font(Standard14Fonts.FontName.COURIER), 12);

		contentStream.newLineAtOffset(textx,texty);
		contentStream.showText(text);
		contentStream.endText();
	}
	
	
	

	private static void checkZugferdDaten(File pdf) throws IOException {
		
		ZUGFeRDImporter zi=new ZUGFeRDImporter(pdf.getCanonicalPath());
		
		System.out.println("getTaxTotalAmount " + zi.getTaxTotalAmount());
		System.out.println("getLineTotalAmount " + zi.getLineTotalAmount());
		
		System.out.println("getIBAN " + zi.getIBAN());
		System.out.println("getBIC " + zi.getBIC());
		System.out.println("getAmount " + zi.getAmount());
		
		System.out.println("getDueDate " + zi.getDueDate());
		System.out.println("getInvoiceCurrencyCode " + zi.getInvoiceCurrencyCode());
		System.out.println("getInvoiceID " + zi.getInvoiceID());
		
	
		PDDocument doc = Loader.loadPDF(pdf);
		PDPage page = new PDPage(PDRectangle.A4);
		doc.addPage(page);

		PDPageContentStream contentStream = new PDPageContentStream(doc, page);

		write(contentStream, 30,750, "Diese Rechnung im PDF-Format enthält einen maschinenlesbaren Teil, der");
		write(contentStream, 30,735, "dem ZUGFeRD-Standard Version 2 entspricht.");

		write(contentStream, 30,720, "Diese Seite, die nicht zur Rechnung gehört, dient dazu, die Integrität");
		write(contentStream, 30,705, "dieser Daten zu dokumentieren. Dazu wurden die relevantesten Daten ");
		write(contentStream, 30,690, "NACH dem Hinzufügen zu diesem PDF-Dokument wieder ausgelesen und");
		write(contentStream, 30,675, "sind im folgenden aufgeführt.");

		
		write(contentStream, 100,600, "getInvoiceID " + zi.getInvoiceID());
		write(contentStream, 100,580, "getInvoiceCurrencyCode " + zi.getInvoiceCurrencyCode());
		write(contentStream, 100,560, "getDueDate " + zi.getDueDate());
		write(contentStream, 100,540, "getIBAN " + zi.getIBAN() );
		write(contentStream, 100,520, "getBIC " + zi.getBIC());
		write(contentStream, 100,500, "getLineTotalAmount " + zi.getLineTotalAmount());
		write(contentStream, 100,480, "getTaxTotalAmount " + zi.getTaxTotalAmount());
		write(contentStream, 100,460, "getAmount " + zi.getAmount());

		contentStream.close();
		
		doc.save(pdf);
		
	}

	
	
	
	
	

}
