package test;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class DirkExporter
{
	//private Logger logger = Logger.getLogger( DirkExporter.class );

	protected Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 
			12, Font.NORMAL, new Color(255, 0, 0));
	
	private Color blue = new Color(0,0,128);
	
	private Document document;
	private PdfWriter pdfWriter;
	//=========================================================================
	
	protected PdfPTable initHeader()
	{
		//logger.debug("Initializing header...");
		// Header information
		String milestoneNameX = "milestone";
		String milestoneAddition = "-1";
		String customer = "customer";
		String projectTitle = "Title";
		StringBuffer project = new StringBuffer("Project (");
		project.append("p")
			   .append(") \n")
			   .append(projectTitle);

		int headTableFontsize = 9;
		Font headerFont = FontFactory.getFont(FontFactory.HELVETICA, headTableFontsize, Font.BOLD);
		Paragraph msNamePhrase = new Paragraph("Milestone " + milestoneNameX + " " + milestoneAddition, headerFont);
		Paragraph customerPhrase = new Paragraph("Customer " + customer, headerFont);
		Chunk projectChunk = new Chunk(project.toString(), headerFont);
		Paragraph projectPhrase = new Paragraph(projectChunk);

		/*
		Image image = null;
		try {
			//////////// Traffic light /////////////
			//	1=green,2=yellow, 3=red,4=grey
			GifImage traffic;
			//logger.debug("Getting traffic light image from: " + realPath + "/static/images/<image.gif>");
			switch (manager.getTrafficLightColor(dataContainer,dataID)) 
			{
				case 1:
					traffic = new GifImage(realPath + "/static/images/traffic_light_green-neu.gif");
					//traffic = new GifImage("/static/images/traffic_light_green-neu.gif");
					break;
				case 2:
					traffic = new GifImage(realPath + "/static/images/traffic_light_yellow-neu.gif");
					//traffic = new GifImage("/static/images/traffic_light_yellow-neu.gif");
					break;
				case 3:
					traffic = new GifImage(realPath + "/static/images/traffic_light_red-neu.gif");
					//traffic = new GifImage("/static/images/traffic_light_red-neu.gif");
					break;
				default:
					traffic = new GifImage(realPath + "/static/images/traffic_light_grey-neu.gif");
					//traffic = new GifImage("/static/images/traffic_light_grey-neu.gif");
					break;
			}
			image = traffic.getImage(1);	// 1 == frame
			image.setAlignment(Image.ALIGN_LEFT);
			image.setIndentationRight(15f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// Gifs couldn't be found.
			e.printStackTrace();
		}
		*/

		//Header for every Page :
		PdfPTable headTable = null;
		headTable = new PdfPTable(4);
		float[] widths = {5f, 20f, 50f, 25f};
		try {
			headTable.setWidths(widths);
		} catch (BadElementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        PdfPCell cell = null;

		//cell = new PdfPCell(image, false);
        cell = new PdfPCell(new Paragraph("img"));
		cell.setPadding(2f);
		cell.setBorderWidth(1);
		cell.setBorderWidthRight(Cell.NO_BORDER);
		cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		headTable.addCell(cell);

		cell = new PdfPCell(msNamePhrase);
		cell.setBorderWidth(1);
		cell.setBorderWidthLeft(Cell.NO_BORDER);
		cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		headTable.addCell(cell);

		cell = new PdfPCell(customerPhrase);
		cell.setBorderWidth(1);
		cell.setBorderWidthLeft(Cell.NO_BORDER);
		cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		headTable.addCell(cell);

		cell = new PdfPCell(projectPhrase);
		cell.setBorderWidth(1);
		cell.setBorderWidthLeft(Cell.NO_BORDER);
		cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		headTable.addCell(cell);

		return headTable;
	}//end initHeader
	//-------------------------------------------------------------------------

	protected Section buildConditionSection(Chapter mainChapter)
	{
		Paragraph conditionTitle = new Paragraph(mainChapter.indentationLeft() + 20f, "Conditions", chapterFont);
		Section sec = mainChapter.addSection(mainChapter.indentationLeft() + 20f, conditionTitle);
		
		Chunk lastConditionChunk = new Chunk(" ");
		lastConditionChunk.setGenericTag("lastCondition");
		
		Section conditionSection = sec.addSection("Condition section");
		PdfPTable t = new PdfPTable(5);
		PdfPCell cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("1"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("2"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("3"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("4"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("5"));
		t.addCell(cell);
		t.setHeaderRows(1);
		
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val1"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val2"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val3"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val4"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val5"));
		t.addCell(cell);
		
		conditionSection.add(new Chunk("\n"));
		conditionSection.add(t);
		conditionSection.add(lastConditionChunk);
		
		Section openissueSection = sec.addSection("Open issue section");
		t = new PdfPTable(5);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("1"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("2"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("3"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("4"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.setBackgroundColor(blue);
		cell.addElement(new Paragraph("5"));
		t.addCell(cell);
		t.setHeaderRows(1);
		
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val1"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val2"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val3"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val4"));
		t.addCell(cell);
		cell = new PdfPCell();
		cell.addElement(new Paragraph("val5"));
		t.addCell(cell);
		
		openissueSection.add(new Chunk("\n"));
		openissueSection.add(t);
		openissueSection.add(lastConditionChunk);
		
		//Add some extra space to have spacing to the following section(title).
		Chunk emptyChunk = new Chunk("\n");
		sec.add(emptyChunk);

		return sec;
	}//end buildConditionSection
	//-------------------------------------------------------------------------

	public void generateDoc() 
	{
		HeaderFooterCreator hfc = new HeaderFooterCreator(25, 25, 0, 0);

		// Create the document to be fille dwith content.
		document = new Document(PageSize.A4, 40, 25, 75, 40);
		
		// Provide some document properties (author, ...)
		//initDocumentProperties();

		// Define the writer to output the document's content.
		try {
			pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("Dirk.pdf"));
			pdfWriter.setViewerPreferences(PdfWriter.PageModeUseOutlines);
			pdfWriter.setPageEvent(hfc);
			pdfWriter.setPageEvent(new GenericTag());
	
			// Document start :
			document.open();
			//pdfWriter.add(new Rectangle(document.left(), document.bottom(), document.right(), document.top()));
			
			// Define the content of the document's header.
			PdfPTable t = initHeader();		
			hfc.setHeaderTable(t);
			
			// Create a main chapter which contains all sub-chapters (sections).
			// This is neccessary to avoid triggering new pages when creating a 
			// new chapter. If a chapter has little content it is almost unwanted 
			// to begin the next chapter on a new page.
			Chapter mainChapter = new Chapter(new Paragraph(""), 0);
			mainChapter.setNumberDepth(0);
			mainChapter.setIndentationLeft(0f);
			System.out.println(document.leftMargin());
			
			buildConditionSection(mainChapter);
			
			document.add(mainChapter);
			//document.newPage();
			//document.add(new Chunk(""));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally
		{
			//pdfWriter.close();
			document.close();
			System.out.println("Done!");
		}
	}

}
