/*
 * Created on Dec 20, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.lebow.report.workgroup;

import com.lebow.benchmark.Benchmark;
import com.lebow.benchmark.BenchmarkService;
import com.lebow.client.Client;
import com.lebow.client.Study;
import com.lebow.report.common.TitlePage;
import com.lebow.report.config.ReportConfig;
import com.lebow.report.graphics.Graphic1;
import com.lebow.report.graphics.Graphic2;
import com.lebow.report.graphics.Graphic3;
import com.lebow.report.graphics.Graphic4;
import com.lebow.report.graphics.Graphic5;
import com.lebow.report.graphics.Graphic6;
import com.lebow.report.graphics.Graphic7;
import com.lebow.report.graphics.Graphic8;
import com.lebow.report.graphics.Graphic9;
import com.lebow.report.graphics.Graphic10;
import com.lebow.report.graphics.Graphic11;
import com.lebow.report.tables.DescriptionOfSelfAndPeers;
import com.lebow.report.tables.DirectionManagementIsFacing; 
import com.lebow.report.tables.Graphic1Legend;
import com.lebow.report.tables.Graphic4Legend;
import com.lebow.report.tables.Graphic5Arrows;
import com.lebow.report.tables.Graphic5Legend;
import com.lebow.report.tables.Graphic6Arrows;
import com.lebow.report.tables.Graphic7Legend;
import com.lebow.report.tables.Graphic9Legend;
import com.lebow.report.tables.Graphic10Legend;
import com.lebow.report.tables.Graphic11Legend;
import com.lebow.report.tables.ImportanceDeliveryOfPeopleValues;
import com.lebow.report.tables.IntrinsicMotivationRatio;
import com.lebow.report.tables.JobAttributesRanking;
import com.lebow.report.tables.LevelOfPerformance;
import com.lebow.report.tables.LevelOfPersonalSatisfaction;
import com.lebow.report.tables.OrgDeliveryOfValues;
import com.lebow.report.tables.PersonalValuesNeeds;
import com.lebow.report.tables.TypeDescription;
import com.lebow.report.tables.VTI;
import com.lebow.report.tables.VTIScore;
import com.lebow.report.tables.VTIScoreRating;
import com.lebow.report.tables.VTISumRangeAnalysis;

import com.lebow.statistics.OrganizationStatistics;
import com.lebow.statistics.WorkgroupStatistics;
import com.lebow.statistics.WorkgroupStatisticsList;
import com.lebow.survey.Survey;
import com.lebow.survey.SurveyList;
import com.lebow.util.FileData;

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.Image;
import com.lowagie.text.Jpeg;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.FontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfPageEvent;
import com.lowagie.text.pdf.PdfPageLabels;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;

import com.modelgenerated.foundation.dataaccess.UserContext;
import com.modelgenerated.foundation.logging.Logger;
import com.modelgenerated.foundation.service.ServiceLocator;
import com.modelgenerated.lookup.LookupService;
import com.modelgenerated.lookup.LookupData;
import com.modelgenerated.util.Assert;
import com.modelgenerated.util.DateUtil;
import com.modelgenerated.util.FileUtil;
import com.modelgenerated.util.StringUtil;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.Color;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.TreeMap;

import org.jfree.chart.JFreeChart;


/**
 * @author kevin
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class TableImageProblem implements PdfPageEvent {

private static final int ONEINCH = 72;
private static final int PAGE_HEIGHT = 11 * ONEINCH;
private static final int PAGE_WIDTH = (int)(8.5 * ONEINCH);

private static final int CENTERLINE = PAGE_WIDTH / 2;

protected static final int MARGIN_LEFT = ONEINCH;
protected static final int MARGIN_RIGHT = PAGE_WIDTH - (ONEINCH * 3 / 4);


private static final int COLWIDTH_EQUIPMENTTYPE = 28;
private static final int COLWIDTH_TOTALUNITS = 11;
private static final int COLWIDTH_TESTEDUNITS = 11;
private static final int COLWIDTH_NUMBERUNITS = COLWIDTH_TOTALUNITS + COLWIDTH_TESTEDUNITS;
private static final int COLWIDTH_PASSEDYES = 8;
private static final int COLWIDTH_PASSEDNO = 8;
private static final int COLWIDTH_PASSEDNA = 9;
private static final int COLWIDTH_PASSED = COLWIDTH_PASSEDYES + COLWIDTH_PASSEDNO + COLWIDTH_PASSEDNA;
private static final int COLWIDTH_MODEL = 25;

private static final int YPOS_HEADING = 775;
private static final int XPOS_PAGENUMBER = 520;
private static final int YPOS_PAGENUMBER = 770;    
    

private static final int EXECSUMMARY_YPOS_TITLE = 770;    
private static final int EXECSUMMARY_YPOS_SUBTITLE = 750;    

protected static final int SIDE_LEFT = 1;    
protected static final int SIDE_RIGHT = 2;    
    
protected static final String REPORT_TITLE = "Workgroup Report";    
UserContext userContext;
Document document;
ReportConfig reportConfig; 
PdfWriter writer; 
Font tableFont = null;

BaseFont pageNumberFont = null;
BaseFont normalFont = null;
PdfTemplate pageNumberTemplate;
PdfContentByte cb = null;
ResourceBundle rb = PropertyResourceBundle.getBundle("com.lebow.report.orgwide.resource");
PdfPageLabels pageLabels = new PdfPageLabels();
OrganizationStatistics statistics;
Benchmark benchmark;
    
String clientName; 
String reportName; 
String studyName;
String studyDate = "";

public TableImageProblem() {
}
 
    
public byte[] buildReport(UserContext userContext) {       
    Assert.check(userContext != null, "userContext != null");
    
    try {
        
        document = new com.lowagie.text.Document(PageSize.LETTER);
            
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writer = PdfWriter.getInstance(document, out);
        Assert.check(writer != null, "writer != null");
        writer.setPageEvent(this);        
        document.open();        
        
        tableFont = FontFactory.getFont(FontFactory.HELVETICA, 10);
        normalFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        
        drawPage10();
        drawPage11();

        document.close();

        return out.toByteArray();       
            
    } catch (java.io.IOException e) {
        throw new RuntimeException("Error creating submittal", e);            
    } catch (com.lowagie.text.DocumentException e) {
        throw new RuntimeException("Error creating submittal", e);            
    } 
}


private void drawPage10() throws DocumentException, IOException {
    //drawPage10Title();

    int xpos = MARGIN_LEFT;        
    int ypos = (int)(ONEINCH * 5.25);        
    int width = MARGIN_RIGHT - MARGIN_LEFT;
    int height = (int)(ONEINCH * 3.5);

    Graphic10 graphic10 = new Graphic10();
    //addChart(writer, graphic10.createChart(statistics, benchmark), xpos, ypos, width, height);
    
    xpos = MARGIN_LEFT;        
    ypos = (int)(ONEINCH * 5.25);        
    width = MARGIN_RIGHT - MARGIN_LEFT;
    PdfPTable table = Graphic10Legend.getTable(cb);
    table.setTotalWidth(width);        
    table.writeSelectedRows(0, -1, xpos, ypos, cb);
    
        
    //drawFooter("10", SIDE_RIGHT);
    document.newPage();    
}

private void drawPage10Title() throws DocumentException, IOException {
    int TEXTCOLUMN_LEFT = 70;
    int TEXTCOLUMN_RIGHT = 550;
    int TEXTCOLUMN_TOP = 720;
    int TEXTCOLUMN_BOTTOM = 620;

    ColumnText ct = new ColumnText(cb);
    //addTitleGroup3(ct);    
    addColumnText(ct, "Level of Performance\n", FontFactory.TIMES_BOLD, 12);
    addColumnText(ct, "Graphic 10\n", FontFactory.TIMES, 12);
    addColumnText(ct, reportName + "\n", FontFactory.TIMES_BOLD, 12);
    addColumnText(ct, REPORT_TITLE + "\n\n", FontFactory.TIMES, 12);
    drawColumnText(ct, Element.ALIGN_CENTER, TEXTCOLUMN_LEFT, TEXTCOLUMN_TOP, TEXTCOLUMN_RIGHT, TEXTCOLUMN_BOTTOM);
}    
  

private void drawPage11() throws DocumentException, IOException {
    drawPage11Title();

    int xpos = MARGIN_LEFT;        
    int ypos = (int)(ONEINCH * 4.5);        
    int width = MARGIN_RIGHT - MARGIN_LEFT;
    int height = (int)(ONEINCH * 3.75);

    //Graphic11 graphic11 = new Graphic11();
    //addChart(writer, graphic11.createChart(statistics, benchmark), xpos, ypos, width, height);

    xpos = MARGIN_LEFT;        
    ypos = (int)(ONEINCH * 4.45);        
    width = MARGIN_RIGHT - MARGIN_LEFT;
    PdfPTable table = Graphic11Legend.getTable(cb);
    table.setTotalWidth(width);        
    table.writeSelectedRows(0, -1, xpos, ypos, cb);
        
    //drawFooter("11", SIDE_RIGHT);
    document.newPage();    
}

private void drawPage11Title() throws DocumentException, IOException {
    int TEXTCOLUMN_LEFT = 70;
    int TEXTCOLUMN_RIGHT = 550;
    int TEXTCOLUMN_TOP = (int)(ONEINCH * 10);
    int TEXTCOLUMN_BOTTOM = (int)(ONEINCH * 8.25);

    ColumnText ct = new ColumnText(cb);
    //addTitleGroup3(ct);    
    addColumnText(ct, "Job Attributes\n", FontFactory.TIMES_BOLD, 12);
    addColumnText(ct, "Graphic 11\n", FontFactory.TIMES, 12);
    addColumnText(ct, "The Graphic Shows the Attributes\n", FontFactory.TIMES, 12);
    addColumnText(ct, "That the Respondents Ranked \"10,\" or Most Important\n", FontFactory.TIMES, 12);
    addColumnText(ct, reportName + "\n", FontFactory.TIMES_BOLD, 12);
    addColumnText(ct, REPORT_TITLE + "\n\n", FontFactory.TIMES, 12);
    drawColumnText(ct, Element.ALIGN_CENTER, TEXTCOLUMN_LEFT, TEXTCOLUMN_TOP, TEXTCOLUMN_RIGHT, TEXTCOLUMN_BOTTOM);
}    







  


protected PdfContentByte getPdfContentByte() {
    return cb;  
}
protected String getClientName() {
    return clientName;
}

protected void addColumnTextByKey(ColumnText ct, String key, String fontName, int fontSize ) {
    addColumnText(ct, (String)rb.getObject(key), fontName, fontSize);
}
protected void addColumnText(ColumnText ct, String text, String fontName, int fontSize ) {
    ct.addText(new Phrase(text, FontFactory.getFont(fontName, fontSize)));
}

protected void drawColumnText(ColumnText ct, int align, float left, float top, float right, float bottom) throws DocumentException, IOException {
    float[] left1  = {left,top, left,bottom};
    float[] right1 = {right,top, right,bottom};
    ct.setColumns(left1, right1);    
    int status = ct.go();
    ct.setYLine(top);
    ct.setAlignment(align);
    status = ct.go();
}

private void addChart(PdfWriter writer, JFreeChart chart, int x, int y, int width, int height) {
    FontMapper fontMapper = new DefaultFontMapper();
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(width, height);
    Graphics2D g2D = tp.createGraphics(width, height, fontMapper);
    Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
    chart.draw(g2D, r2D);
    g2D.dispose();
    cb.addTemplate(tp, x, y);
}
    
    
    
// PdfPageEvent
    
/** Called when a Chapter is written.
 * <P>
 * <CODE>position</CODE> will hold the height at which the
 * chapter will be written to.
 *
 * @param writer            the <CODE>PdfWriter</CODE> for this document
 * @param document          the document
 * @param paragraphPosition the position the chapter will be written to
 * @param title             the title of the Chapter
 *
 */
public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
}    

/** Called when the end of a Chapter is reached.
 * <P>
 * <CODE>position</CODE> will hold the height of the end of the chapter.
 *
 * @param writer            the <CODE>PdfWriter</CODE> for this document
 * @param document          the document
 * @param paragraphPosition the position the chapter will be written to
 *
 */
public void onChapterEnd(PdfWriter writer, Document document, float paragraphPosition) {
}
    
/** Called when the document is closed.
 * <P>
 * Note that this method is called with the page number equal
 * to the last page plus one.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 *
 */
public void onCloseDocument(PdfWriter writer, Document document) {
    pageNumberTemplate.beginText();
    pageNumberTemplate.setFontAndSize(pageNumberFont, 8);
    pageNumberTemplate.showText(String.valueOf(writer.getPageNumber() - 1));
    pageNumberTemplate.endText();
}
    
/** Called when a page is finished, just before being written to the document.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 *
 */
public void onEndPage(PdfWriter writer, Document document) {
}
    
/** Called when a <CODE>Chunk</CODE> with a generic tag is written.
 * <P>
 * It is usefull to pinpoint the <CODE>Chunk</CODE> location to generate
 * bookmarks, for example.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 * @param rect the <CODE>Rectangle</CODE> containing the <CODE>Chunk</CODE>
 * @param text the text of the tag
 *
 */
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
}
    
/** Called when the document is opened.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 *
 */
public void onOpenDocument(PdfWriter writer, Document document) {
    try {
        pageNumberFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb = writer.getDirectContent();
        pageNumberTemplate = cb.createTemplate(50, 50);
    }
    catch(DocumentException de) {
    }
    catch(IOException ioe) {
    }
}
    
/** Called when a Paragraph is written.
 * <P>
 * <CODE>paragraphPosition</CODE> will hold the height at which the
 * paragraph will be written to. This is useful to insert bookmarks with
 * more control.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 * @param paragraphPosition the position the paragraph will be written to
 *
 */
public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
}
    
/** Called when a Paragraph is written.
 * <P>
 * <CODE>paragraphPosition</CODE> will hold the height of the end of the paragraph.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 * @param paragraphPosition the position of the end of the paragraph
 *
 */
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
}
    
/** Called when a Section is written.
 * <P>
 * <CODE>position</CODE> will hold the height at which the
 * section will be written to.
 *
 * @param writer            the <CODE>PdfWriter</CODE> for this document
 * @param document          the document
 * @param paragraphPosition the position the chapter will be written to
 * @param title             the title of the Chapter
 *
 */
public void onSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) {
}
    
/** Called when the end of a Section is reached.
 * <P>
 * <CODE>position</CODE> will hold the height of the section end.
 *
 * @param writer            the <CODE>PdfWriter</CODE> for this document
 * @param document          the document
 * @param paragraphPosition the position the chapter will be written to
 *
 */
public void onSectionEnd(PdfWriter writer, Document document, float paragraphPosition) {
    // this.paragraphPosition = paragraphPosition; 
}
    
/** Called when a page is initialized.
 * <P>
 * Note that if even if a page is not written this method is still
 * called. It is preferable to use <CODE>onEndPage</CODE> to avoid
 * infinite loops.
 *
 * @param writer the <CODE>PdfWriter</CODE> for this document
 * @param document the document
 *
 */
public void onStartPage(PdfWriter writer, Document document) {
}

}
