import lotus.domino.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;
import java.util.Vector;

import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

import dk.heisterberg.lekkim.blog.image_resource.*;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class JavaAgent extends AgentBase {

	Locale locale = Locale.US;
	Document pdfDoc;
	PdfPTable doutTable;
	PdfPCell cell;
	Paragraph outPara;
	String outLine;
	String pdfDirBase;
	String pdfDir;
	String pdfDirectory;
	String pdfName;
	File pdfFile;
	String path;
	String quoteDate;
	String companyName = "RBG Contracting and Maintenance";
	String companyAdd = "570 Orwell Street, Unit 1 & 2";
	String companyCity = "Mississauga";
	String companyProv = "Ontario";
	String companyPostal = "L5A 3V7";
	String companyPhone = "905-274-9887";
	String companyFax = "905-274-3912";
	String companyWeb = "www.rbgteam.com";
	DecimalFormat df = new DecimalFormat("#,###,##0.00");
	int pageNum = 1;
	float totHeight;
	float cellHeight;

	Vector iM;
	Vector dM;
	DateTime dt;
	String quoteNum;
	String quotePoNum;
	String quoteTerms;
	String quoteValidity;
	String quoteTo;
	String quoteAttn;
	String quoteSite;
	String quoteProj;

	public void NotesMain() {

		try {
			lotus.domino.Session session = getSession();
			AgentContext agentContext = session.getAgentContext();

			pdfDirBase = "C:\\RBGApp\\";
			pdfDir = "PDF\\";
			pdfDirectory = pdfDirBase + pdfDir;
			pdfName = "PDF-Quote" + quoteNum + ".pdf";
			boolean exists = (new File(pdfDirectory)).exists();

			if (exists)
			{
				print("Directory: Exists.");
			}
			else
			{
				print("Directory: Not There. Going to create.");
				boolean success = (new File(pdfDirectory)).mkdirs();
				if (!success)
				{
					print("Directory: Creation Failed for Directory: " + pdfDirectory);
				}
			}
//	Directory Processing ==================================================== End

//	PDF Processing ========================================================== Start
			pdfDoc = new Document(PageSize.LETTER, 25, 25, 0, 0);
			try {
				PdfWriter pdf = PdfWriter.getInstance(pdfDoc, new FileOutputStream(pdfDirectory + pdfName));
//	PDF Properties ======================= Start
				pdfDoc.addTitle(companyName + " : Quotation");
				pdfDoc.addSubject("Created from the " + companyName + " Quotation System");
				pdfDoc.addKeywords(companyName);
				pdfDoc.addCreator(companyName + " Quotation Agent");
				pdfDoc.addAuthor(companyName);
//	PDF Properties ======================= end
				pdfDoc.open();
				totHeight = 0.0f;
//	PDF Body ============================= Start
				float[] widths3 = {0.5f, 6.0f, 1.0f, 1.0f};
				doutTable = new PdfPTable(widths3);
				doutTable.setWidthPercentage(100);
				doutTable.setTotalWidth(24.0f);
				doutTable.deleteBodyRows();

	for (int nxt = 0; nxt < 10; nxt++)
	{
		outLine = " ";
		outPara = new Paragraph(outLine, FontFactory.getFont(FontFactory.HELVETICA, 10));

//		print("Counter = " + nxt + " / OutLine = " + outLine);
		cell = new PdfPCell(new Paragraph(outPara));
		cell.setBorder(Rectangle.BOX);
		cell.setHorizontalAlignment(Element.ALIGN_CENTER);
		doutTable.addCell(cell);

		outLine = "fsdfgf sdfg gsfg dsfg gsdfg sfg";				// 1 line
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 2 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 3 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 4 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 5 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 6 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 7 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 8 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 9 lines
		outLine = outLine + "\nfsdfgf sdfg gsfg dsfg gsdfg sfg";		// 10 lines
		outPara = new Paragraph(outLine, FontFactory.getFont(FontFactory.HELVETICA, 10));

		print("Counter = " + nxt + " / OutLine = \n" + outLine);
		cell = new PdfPCell(new Paragraph(outPara));
		cell.setBorder(Rectangle.BOX);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		doutTable.addCell(cell);

		outLine = " ";
		outPara = new Paragraph(outLine, FontFactory.getFont(FontFactory.HELVETICA, 10));

//		print("Counter = " + nxt + " / OutLine = " + outLine);
		cell = new PdfPCell(new Paragraph(outPara));
		cell.setBorder(Rectangle.BOX);
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		doutTable.addCell(cell);

		outLine = " ";
	     outPara = new Paragraph(outLine, FontFactory.getFont(FontFactory.HELVETICA, 10));

//		print("Counter = " + nxt + " / OutLine = " + outLine);
		cell = new PdfPCell(new Paragraph(outPara));
		cell.setBorder(Rectangle.BOX);
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		doutTable.addCell(cell);

		cellHeight = doutTable.getRowHeight(0);
		totHeight = totHeight + cellHeight;
		print("Counter = " + nxt + " = " + cellHeight + " / tot = " + totHeight);

		if (totHeight > 1050.0)
		{
			print("*************  Total Height = " +totHeight);
			totHeight = 636.0f;
		}

		pdfDoc.add(doutTable);
		doutTable.deleteBodyRows();
	}

			} catch (DocumentException de) {
				System.err.println(de.getMessage());
			} catch (IOException ioe) {
				System.err.println(ioe.getMessage());
			}
			pdfDoc.close();
			print("Completed");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	public static void print(String msg)
	{
		PrintWriter mssg = new PrintWriter(System.out, true);
		mssg.println("Agent Message = " + msg);
	}
}
