/*
 * Pdf.java
 *
 * Created on November 13, 2007, 11:33 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pdf;

import analiseacidentestrabalho.Acidentado;
import analiseacidentestrabalho.AnaliseAcidente;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import db.AnalisesDataProvider;
import java.awt.Color;
import java.io.FileOutputStream;
import javax.faces.context.FacesContext;

/**
 *
 * @author lluis
 */
public class Pdf {

    PdfPCell cell = null;
    Paragraph p = null;
    private final static int FONT_SIZE = 5;      
    /** Creates a new instance of Pdf */
    public Pdf() {
    }
    
    public void generatePdf(String folder, AnaliseAcidente a)
    {
        PdfPTable table = null;
        FacesContext ctx = FacesContext.getCurrentInstance();
        ////HttpServletResponse response = (HttpServletResponse)ctx.getExternalContext().getResponse();
        Document document = new Document();           
        ////response.setContentType("application/pdf"); 

        try
        {
            ////PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            PdfWriter.getInstance(document, new FileOutputStream(folder + "/" + a.getId().toString() + ".pdf"));                    
            document.open();
            //document.add(new Paragraph("Hello World"));
            //document.add(new Paragraph(new Date().toString()));
            
            table = createTableEmpresa(a);
            document.add(table);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        document.close();                            
    }
    
    private PdfPTable createTableHeader(AnaliseAcidente a) 
    {
        PdfPTable table = new PdfPTable(3);
        return table;
    }

    private PdfPTable createTableEmpresa(AnaliseAcidente a) 
    {
        String empresa_str = "";
        String estabelecimento_str = "";
        String trabalhador_str = "";
        AnalisesDataProvider adp = new AnalisesDataProvider();
        try
        {
            String nome_empresa = adp.getEmpresaNome(a.getEmpresa_id());
            empresa_str = utils.Utils.unicodeToHTML(nome_empresa);
        }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }                 
        try
        {
            estabelecimento_str = adp.getEstabelecimentoNome(a.getEstabelecimento_id());
        }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }         
        
        Acidentado ac = null;
        try
        {
            ac = adp.getAcidentado(a.getAcidentado_id());
            trabalhador_str = ac.getNome();
        }
        catch(Exception ex)
        {
           ex.printStackTrace();
        }    
        
        //TableEvents event = new TableEvents();
        
        float[] widths = { 1f, 4f };                
        PdfPTable table = new PdfPTable(widths);
        table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.TOP | Rectangle.BOTTOM);
        //PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));                                                
        //cell.setColspan(3);
        //table.addCell(cell); 
        

        
         
        cell = new PdfPCell(new Paragraph("Empresa:", FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE)));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));           
        table.addCell(cell);
        
        cell = new PdfPCell(new Paragraph(empresa_str, FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE, Font.NORMAL, new Color(255, 255, 255))    ));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));                   
        table.addCell(cell);        
        
        cell = new PdfPCell(new Paragraph("Estabelecimento:", FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE)));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));           
        table.addCell(cell);                

        cell = new PdfPCell(new Paragraph(estabelecimento_str, FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE, Font.NORMAL, new Color(255, 255, 255))));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));           
        table.addCell(cell);   
        
        cell = new PdfPCell(new Paragraph("Trabalhador:", FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE)));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));    
        
        table.addCell(cell);  
        
        cell = new PdfPCell(new Paragraph(trabalhador_str, FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, FONT_SIZE, Font.NORMAL, new Color(255, 255, 255))));
        cell.setBorder(Rectangle.NO_BORDER); 
        cell.setBackgroundColor(new Color(102, 133, 151));           
        table.addCell(cell);   
        table.getDefaultCell().setBackgroundColor(new Color(102, 133, 151));   
        table.getDefaultCell().setPadding(0f);
        table.setSpacingAfter(0f);
        table.setSpacingBefore(0f);
        return table;        
    }    
    
    private PdfPTable createTableEmpresaHs(AnaliseAcidente a) 
    {
        PdfPTable table = new PdfPTable(3);
        return table;        
    }  
    
    
    

}
