import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.siemens.checklist.export.GenericTag;
import com.siemens.checklist.utils.DateFormatter;

public class Dirk 
{
	protected static Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 
			12, Font.NORMAL, new Color(255, 0, 0));
	//=========================================================================
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Document document = new Document(PageSize.A4, 40, 25, 75, 40);
		PdfWriter pdfWriter = null;
		try {
			pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("Dirk.pdf"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		pdfWriter.setPageEvent(new GenericTag());
		
		document.open();
		List dates = new ArrayList();
		for ( int i=0; i<13; i++ )
		{
			GregorianCalendar gcal = (GregorianCalendar)Calendar.getInstance();
    		gcal.setTime(new Date(System.currentTimeMillis()));
    		gcal.add(GregorianCalendar.DATE, i);
    		dates.add(gcal.getTime());
		}//end for i
		
		try {
			Chapter mainChapter = new Chapter(new Paragraph(""), 0);
			mainChapter.setNumberDepth(0);
			
			buildConditionSection(mainChapter);
			buildMeetingDateSection(mainChapter, dates, document);
			
			document.add(mainChapter);

			//document.add(createTableForMeetingDates(dates, "Dates"));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		document.close();
	}//end main
	//-------------------------------------------------------------------------

	protected static Section buildConditionSection(Chapter mainChapter)
	{
		Paragraph conditionTitle = new Paragraph("Conditions", chapterFont);
		Section sec = mainChapter.addSection(conditionTitle);
        //sec.add(new Paragraph("\n"));	// Add some extra space between section title and content.
		//sec.add(new Paragraph(" "));
        sec.setIndentation(20f);
		
		//Chunk lastConditionChunk = new Chunk(" ");
		//lastConditionChunk.setGenericTag("lastCondition");
		
		sec.add(new Paragraph("<köjdnvkadnvkadjvakdfjvbkafvklöadfvökandfövkandf\n\nlakjndfvkjad"));
		//sec.add(lastConditionChunk);
		
		PdfPTable aTable = new PdfPTable(new float[]{30, 5, 65});
		PdfPCell leftCell = new PdfPCell(new Phrase("left"));
		PdfPCell middleCell = new PdfPCell(new Phrase("middle"));
		PdfPCell rightCell = new PdfPCell(new Phrase("right"));
		aTable.addCell(leftCell);
		aTable.addCell(middleCell);
		aTable.addCell(rightCell);
		sec.add(aTable);
		//sec.add(lastConditionChunk);
		
		//Add some extra space to have spacing to the following section(title).
		//Chunk emptyChunk = new Chunk("\n");
		//sec.add(emptyChunk);
		sec.add(new Paragraph("\n"));

		return sec;
	}//end buildConditionSection
	//-------------------------------------------------------------------------

	protected static Section buildMeetingDateSection(Chapter mainChapter, List dates, Document doc)
	{
		mainChapter.add(Chunk.NEXTPAGE);
		doc.setPageSize(PageSize.A4.rotate());
		Paragraph conditionTitle = new Paragraph("Conditions", chapterFont);
		Section sec = mainChapter.addSection(conditionTitle);
        //sec.add(new Paragraph("\n"));	// Add some extra space between section title and content.
		//sec.add(new Paragraph(" "));
        sec.setIndentation(20f);
		
        try {
        	sec.add(new Paragraph("\n"));
        	sec.add(createTableForMeetingDates(dates, "Date of meeting"));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		//Add some extra space to have spacing to the following section(title).
		//Chunk emptyChunk = new Chunk("\n");
		//sec.add(emptyChunk);
		sec.add(new Paragraph("\n"));

		return sec;
	}//end buildMeetingDateSection
	//-------------------------------------------------------------------------

	private static PdfPTable createTableForMeetingDates(List dates, String caption) 
		throws DocumentException 
	{
		int missingCols = 4 - dates.size() % 4;
		
		// the left column for the caption
		PdfPTable meetingDateTable = new PdfPTable(new float[]{30, 5, 65});
		PdfPCell leftCell = new PdfPCell(new Phrase(caption));
		leftCell.setMinimumHeight(20f);
		leftCell.setPadding(2f);
		leftCell.setBorderWidth(1);
		leftCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		leftCell.setVerticalAlignment(Element.ALIGN_TOP);
		leftCell.setBackgroundColor(new java.awt.Color(0x8e, 0xc8, 0xff));
		meetingDateTable.addCell(leftCell);
		
		// to align to the previously drew table
		PdfPCell middleCell = new PdfPCell();
		middleCell.setPadding(0);
		middleCell.setBorderWidth(0);
		meetingDateTable.addCell(middleCell);
	
		PdfPTable dateTable = null;
		if ( dates.size() > 0 )
		{
			// the right column for the meeting dates
	    	dateTable = new PdfPTable(4);
	    	dateTable.setWidthPercentage(100);
	    	PdfPCell pCell = null;
	    	int lineCounter = 0;
	    	for (int i=0; i<dates.size(); i++)
			{
				if ( dates.get(i) != null )
				{
					SimpleDateFormat dateFormat = DateFormatter.getStandardDateFormat();
					String resultDate = dateFormat.format((Date)dates.get(i));
					Phrase theDate = new Phrase(resultDate, 
							FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL));
					pCell = new PdfPCell(theDate);
					pCell.setPadding(2f);
					pCell.setBorderWidth(0);
					if ( lineCounter < 3 )
					{
						pCell.setBorderWidthRight(0.5f);
						lineCounter++;
					} else
					{
						lineCounter = 0;
					}
					pCell.setHorizontalAlignment(Element.ALIGN_CENTER);
					pCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
					
					dateTable.addCell(pCell);
				}//end if ( dates.get(i) != null )
			}//end for i
	    	// Filling up the PdfPTable's row becaus eonly full rows will be 
	    	// displayed by design.
	    	for ( int i=0; i<missingCols; i++ )
	    	{
	    		pCell = new PdfPCell();
	    		pCell.setPadding(2f);
	    		pCell.setBorderWidth(0);
				if ( lineCounter < 3 )
				{
					pCell.setBorderWidthRight(0.5f);
					lineCounter++;
				} else
				{
					lineCounter = 0;
				}
				pCell.setHorizontalAlignment(Element.ALIGN_CENTER);
				pCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
				dateTable.addCell(pCell);
	    	}//end for i
		}//end if ( dates.size() > 0 )
	
		PdfPCell rightCell = null;
		if ( dateTable == null )
		{
			rightCell = new PdfPCell(new Phrase());
		} else
		{
			rightCell = new PdfPCell(dateTable);
		}
		rightCell.setPadding(0);
		rightCell.setBorderWidth(0);
		rightCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		rightCell.setVerticalAlignment(Element.ALIGN_TOP);
		//rightCell.addElement(dateTable);
		
		meetingDateTable.addCell(rightCell);
		
		return meetingDateTable;
	}//end createTableForDates
	//-------------------------------------------------------------------------
}
