package controller.commande;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.sig.fw.db.corrida.model.Commande;
import org.sig.fw.db.corrida.model.Commande_Content;
import org.sig.fw.log.common.LogTools;
import org.sig.fw.spdf.common.PDF;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.number.NumberTools;

import view.commande.Commande_Pdf_View;

public class Commande_Pdf_Controller extends PDF
{
	private Commande_Pdf_View vw_commande_pdf;
	private Commande commande;
	private List<Commande_Content> contents;
	
	private float page_width = 0;
	private float page_height = 0;
	
	private int y;
	
	public Commande_Pdf_Controller(Commande commande, List<Commande_Content> contents)
	{
		super();
		this.commande = commande;
		this.contents = contents;
		this.vw_commande_pdf = new Commande_Pdf_View(this);
		this.vw_commande_pdf.setVisible(true);
	}

	public void drawPdf()
	{
		new Thread(new Runnable() {
			
			@Override
			public void run() 
			{
				try
				{		
					vw_commande_pdf.setValue("Création du document", 0);
					createPage();
					vw_commande_pdf.setValue("Ajout de l'entête", 20);
					createHead();
					y = 600;
					vw_commande_pdf.setValue("Ajout du contenu", 80);
					createTable();
					vw_commande_pdf.setValue("Finalisation du document", 100);
					
					closeStream();
					save(commande.getDossier().getDirectory()+"\\09-Factures-Caution", "BON DE COMMANDE");
					closeDocument();
				}
				catch (IOException e) 
				{
					LogTools.addErrorLog("PDF02", e);
				}
				vw_commande_pdf.dispose();
			}
		}).start();
	}
	
	private void createPage() throws IOException
	{
		this.page = new PDPage(PDRectangle.A4);
		this.document.addPage(page);
		
        this.page_width = this.page.getMediaBox().getWidth();
        this.page_height = this.page.getMediaBox().getHeight();
        this.contentStream = new PDPageContentStream(document, page, AppendMode.OVERWRITE, false);
        
		this.y = (int) (this.page_height - 10);
	}
	
	private void createHead() throws IOException
	{
		/* ENTETE GAUCHE : Logo + Adresse Entreprise */
		BufferedImage image = null;
		if(commande.getSociety().getDbId() == 1)
		{
			image = ImageIO.read(Commande_Pdf_Controller.class.getResource("/logo/sondefor_logo.png"));
			this.contentStream.drawImage(LosslessFactory.createFromImage(this.document, image), 10, this.page_height-10-35, 197, 35);
		}
		else if(commande.getSociety().getDbId() == 2)
		{
			image = ImageIO.read(Commande_Pdf_Controller.class.getResource("/logo/sbf_logo.png"));
			this.contentStream.drawImage(LosslessFactory.createFromImage(this.document, image), 10, this.page_height-10-35, 197, 35);
		}

		int fontSize = 0;
		float textWidth = 0;
		float textHeight = 0;
		String text = "";
		int x = (int) (this.page_width - 20);
		
		y -= 40;
		
		this.contentStream.beginText();
		text = commande.getSociety().getCapital();
		fontSize = 6;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(20, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y-= 5;
		
		if(!this.commande.getSociety().getAddress().getStreet1().isEmpty())
		{
			text = this.commande.getSociety().getAddress().getStreet1();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.commande.getSociety().getAddress().getStreet2().isEmpty())
		{
			text = this.commande.getSociety().getAddress().getStreet2();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.commande.getSociety().getAddress().getStreet3().isEmpty())
		{
			text = this.commande.getSociety().getAddress().getStreet3();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}

		text = this.commande.getSociety().getAddress().getZipcode() + " " + this.commande.getSociety().getAddress().getCity();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(20, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		/* ENTETE DROITE : Contact + Code SIRET / RCS / ... */
		
		y = (int) (this.page_height-10);
		
		if(!this.commande.getSociety().getPhoneNumber().isEmpty())
		{
			text = "Téléphone : " + this.commande.getSociety().getPhoneNumber();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x-textWidth, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		text = "E-mail : " + this.commande.getSociety().getEmail();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "Siret : " + commande.getSociety().getSiret();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "RCS : " + commande.getSociety().getRcs();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "N° Intracom : " + commande.getSociety().getTvaIntracom();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		/* ENTETE Client */
		
		y = 720;
		
		this.contentStream.beginText();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(commande.getSociety().getAddress().getCity() + " le,") / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		contentStream.showText(commande.getSociety().getAddress().getCity() + " le,");
		contentStream.endText();
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(commande.getFacturedDate().getFormattedDate(SDate.FORMAT_FR));
		contentStream.endText();
		
		y -= textHeight;
		y -= textHeight;
		
		this.contentStream.beginText();
		textWidth = normal_font.getStringWidth("Doit") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		contentStream.showText("Doit");
		contentStream.endText();
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.commande.getCc().getCorridaClient().getCompany());
		contentStream.endText();
		
		y -= textHeight;
		
		if(!this.commande.getCc().getCorridaClient().getAddress().getStreet1().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
			contentStream.showText(this.commande.getCc().getCorridaClient().getAddress().getStreet1());
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.commande.getCc().getCorridaClient().getAddress().getStreet2().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
			contentStream.showText(this.commande.getCc().getCorridaClient().getAddress().getStreet2());
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.commande.getCc().getCorridaClient().getAddress().getStreet3().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5+ 10, y-textHeight);
			contentStream.showText(this.commande.getCc().getCorridaClient().getAddress().getStreet3());
			contentStream.endText();
			y -= textHeight;
		}
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.commande.getCc().getCorridaClient().getAddress().getZipcode() + " " + this.commande.getCc().getCorridaClient().getAddress().getCity());
		contentStream.endText();
		y -= textHeight;

		y -= textHeight;

		this.contentStream.beginText();
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Commande N°") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		contentStream.showText("Commande N°");
		contentStream.endText();
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.commande.getCommandeNumber());
		contentStream.endText();
		
		/*TABLEAU GAUCHE : Ref Client et Ref Commande */

		y = 720;

		this.contentStream.addRect(10, y-3*textHeight, this.page_width/2 - 60, 3*textHeight);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Référence Client";
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset((this.page_width/2 - 60)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= 3;
		this.contentStream.addRect(10, y, this.page_width/2 - 60, 0);
		this.contentStream.stroke();
		y -= 2;
		
		this.contentStream.beginText();
		text = this.commande.getCc().getCorridaClient().getDbId() + " / " + this.commande.getDossier().getReferenceNumber();
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset((this.page_width/2 - 60)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= textHeight;
		
		this.contentStream.addRect(10, y-6*textHeight, this.page_width/2 - 60, 6*textHeight);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Référence Commande";
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset((this.page_width/2 - 60)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= 3;
		this.contentStream.addRect(10, y, this.page_width/2 - 60, 0);
		this.contentStream.stroke();
		y -= 2;

		text = commande.getCommandeReference().getShowText();
		int total_width = (int) (this.page_width/2 - 60-2);
		
	    List<String> lines = new ArrayList<String>();
	    int lastSpace = -1;
	    while (text.length() > 0)
	    {
	        int spaceIndex = text.indexOf(' ', lastSpace + 1);
	        if (spaceIndex < 0)
	            spaceIndex = text.length();
	        String subString = text.substring(0, spaceIndex);
	        float size = 10 * normal_font.getStringWidth(subString) / 1000;
	        if (size > total_width)
	        {
	            if (lastSpace < 0)
	                lastSpace = spaceIndex;
	            subString = text.substring(0, lastSpace);
	            lines.add(subString);
	            text = text.substring(lastSpace).trim();
	            lastSpace = -1;
	        }
	        else if (spaceIndex == text.length())
	        {
	            lines.add(text);
	            text = "";
	        }
	        else
	        {
	            lastSpace = spaceIndex;
	        }
	    }

	    textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
	    contentStream.beginText();
	    contentStream.setFont(normal_font, 10);
	    contentStream.newLineAtOffset(11, y-textHeight);
	    for(int i = 0; i < lines.size(); i++)
	    {
	    	if(i < 2)
	    	{
		        contentStream.showText(lines.get(i));
		        contentStream.newLineAtOffset(0, -textHeight);
		        y -= textHeight;
	    	}
	    }
	    contentStream.endText();
	    
	    this.contentStream.beginText();
	    String date = commande.getConstructionSite().getEstimatedStartDate().getFormattedDate(SDate.FORMAT_FR);
		text = "Production " + (date == null ? new SDate().getFormattedDate(SDate.FORMAT_FR).substring(3,10) : date.substring(3,10));
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(11, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		 y -= textHeight;

		 this.contentStream.beginText();
		text = commande.getConstructionSite().getResponsible().getName();
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(11, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
	}
	
	private void createTable() throws IOException
	{
		//y max (haut de page) = 680
		//y min (bas de page) = 80
		
		//largeur = 595... >< -> 550 <-> table
		
		int reference_width = 50;
		int designation_width = 310;
		int quantity_witdh = 70;
		int unit_price_width = 70;
		int amount_ht_width = 70;
		
		int x = 10;
		
		int height = 15;
		int width = 570;
		
		y -= height;
		
		this.contentStream.addRect(x, y, width, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		int fontSize = 10;
		float textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		float textWidth = bold_font.getStringWidth("Réf") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (reference_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Réf");
		contentStream.endText();
		
		x+= reference_width;
		
		this.contentStream.addRect(x, y, designation_width, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Désignation") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (designation_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Désignation");
		contentStream.endText();
		
		x+= designation_width;
		
		this.contentStream.addRect(x, y, quantity_witdh, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Qte") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (quantity_witdh/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Qte");
		contentStream.endText();
		
		x+= quantity_witdh;
		
		this.contentStream.addRect(x, y, unit_price_width, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textWidth = bold_font.getStringWidth("P.U HT") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (unit_price_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("P.U HT");
		contentStream.endText();
		
		x += unit_price_width;
		
		this.contentStream.beginText();
		textWidth = bold_font.getStringWidth("Montant HT") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (amount_ht_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Montant HT");
		contentStream.endText();
		
		//LIBELLE
		x = 10;
		y -= height;
		
		String text = "";
		double total_ht = 0;
		double total_tva = 0;
		
		for(Commande_Content cc : contents)
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			PDFont font = null;
			if(cc.getReference().getTypeId() > 0) {
				font = bold_font;
				
				this.contentStream.setNonStrokingColor(new Color(192,192,192));
				this.contentStream.addRect(x+1, y-1, width-1, textHeight+4);
				this.contentStream.fill();
				this.contentStream.setNonStrokingColor(Color.BLACK);
			} else {
				font = normal_font;
			}
			
			this.contentStream.beginText();
			text = cc.getReference().getShortText();
			textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.setFont(font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += reference_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = cc.getReference().getLongText();
			textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.setFont(font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(cc.getQuantity() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(cc.getQuantity());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + quantity_witdh, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			x += quantity_witdh;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(cc.getUnitPrice() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(cc.getUnitPrice());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + unit_price_width, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			x += unit_price_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(cc.getAmountHt() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(cc.getAmountHt());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + amount_ht_width, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			x+= amount_ht_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x = 10;
			y -= height;
			
			if(cc.getReference().getTypeId() == 0)
			{
				total_ht += cc.getAmountHt();
				total_tva += cc.getAmountTva();
			}
		}
		
		if(commande.getDirectPayment()) {
			
			
			this.contentStream.addRect(x, y, 0, height);
			x += reference_width;
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = "Paiement Direct";
			textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.setFont(bold_font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			x+= quantity_witdh;
			this.contentStream.addRect(x, y, 0, height);
			x+= unit_price_width;
			this.contentStream.addRect(x, y, 0, height);
			x+= amount_ht_width;
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
		}
		
		while(y > (100))
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += reference_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += quantity_witdh;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += unit_price_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += amount_ht_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
	
			y -= height;
			x = 10;
		}
		y += height;
		this.contentStream.addRect(x, y, width, 0);
		this.contentStream.stroke();
		
		int y_tva = y;
		
		y -= height;
		x = reference_width + designation_width + quantity_witdh + 10;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* TOTAL HT */
		this.contentStream.beginText();
		text = "Total HT";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (unit_price_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += unit_price_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = NumberTools.getNumberFormat(2).format(total_ht) + " €";
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x - textWidth - 2 + amount_ht_width, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += amount_ht_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		height += 15;
		
		y -= height;
		x = reference_width + designation_width + quantity_witdh + 10;
		
		/* TOTAL TVA */
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		if(commande.getAutoLiquidation())
			text = "Auto Liquidation";
		else
			text = "Total TVA";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		if(commande.getAutoLiquidation())
			contentStream.newLineAtOffset(x + ((unit_price_width+amount_ht_width)/2) - (textWidth/2), y + (textHeight/2) - 2);
		else
			contentStream.newLineAtOffset(x + (unit_price_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		if(commande.getAutoLiquidation())
		{
			y -= 8;
			this.contentStream.beginText();
			text = "CGI ann. II art.242 nonies A,I-13°";
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * 8;
			textWidth = normal_font.getStringWidth(text) / 1000 * 8;
			this.contentStream.setFont(normal_font, 8);
			contentStream.newLineAtOffset(x + ((unit_price_width+amount_ht_width)/2) - (textWidth/2), y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			y += 8;
			
			total_tva = 0;
		}
		
		x += unit_price_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		if(commande.getAutoLiquidation())
			text = "";
		else
			text = NumberTools.getNumberFormat(2).format(total_tva) + " €";
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x - textWidth - 2 + amount_ht_width, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += amount_ht_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		
		y -= height;
		x = reference_width + designation_width + quantity_witdh + 10;
		
		/* TOTAL NET */
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Total NET";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (unit_price_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += unit_price_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = NumberTools.getNumberFormat(2).format(total_tva + total_ht) + " €";
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x - textWidth - 2 + amount_ht_width, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += amount_ht_width;
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = reference_width + designation_width + quantity_witdh + 10;
		this.contentStream.addRect(x, y, unit_price_width+amount_ht_width, 0);
		this.contentStream.stroke();
		
		/* PARTIE TAB TVA */
		
		if(commande.getAutoLiquidation())
			return;
		x = 10;
		y = y_tva - 10;
		height -= 15;
		
		this.contentStream.addRect(x, y, reference_width+amount_ht_width+amount_ht_width, 0);
		this.contentStream.stroke();
		
		y -= height;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Tx TVA";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (reference_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x+= reference_width;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Montant HT";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (amount_ht_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x+= amount_ht_width;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Montant TVA";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (amount_ht_width/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x+= amount_ht_width;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = 10;
		
		this.contentStream.addRect(x, y, reference_width+amount_ht_width+amount_ht_width, 0);
		this.contentStream.stroke();
		
		y -= height;
		
		HashMap<Double, List<Commande_Content>> tvas = new HashMap<Double, List<Commande_Content>>();
		
		for(Commande_Content cc : contents)
		{
			if(cc.getTva() != 0)
			{
				if(!tvas.containsKey(cc.getTva()))
				{
					List<Commande_Content> list = new ArrayList<Commande_Content>();
					list.add(cc);
					tvas.put(cc.getTva(), list);
				}
				else
				{
					tvas.get(cc.getTva()).add(cc);
				}
			}
		}
		
		for(Map.Entry<Double, List<Commande_Content>> entry : tvas.entrySet())
		{
			double tot_ht = 0;
			double tot_tva = 0;
			double tva = entry.getKey();
			for(Commande_Content cc : entry.getValue())
			{
				tot_ht += cc.getAmountHt();
				tot_tva += cc.getAmountTva();
			}
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = NumberTools.getNumberFormat(2).format(tva);
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x + (reference_width/2) - (textWidth/2), y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			
			x+= reference_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = NumberTools.getNumberFormat(2).format(tot_ht);
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x + (amount_ht_width/2) - (textWidth/2), y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			
			x+= amount_ht_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = NumberTools.getNumberFormat(2).format(tot_tva);
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x + (amount_ht_width/2) - (textWidth/2), y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			
			x+= amount_ht_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x = 10;	
			y -= height;
		}
		
		y += height;
		x = 10;
		
		this.contentStream.addRect(x, y, reference_width+amount_ht_width+amount_ht_width, 0);
		this.contentStream.stroke();
	}

	@Override
	public void createDocument() throws IOException {
		// TODO Auto-generated method stub
	}
	
}
