package jt.core;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import jt.core.beans.ObjectBean;
import jt.core.beans.PdfBean;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

/*
 * This class is part of the draft "iText framework used in JavaTango"
 * written by Julio M. Oliveira - Buenos Aires 2010-08-27
 */
public class itextFramework {

 
	
	public static String RESULT= "c:/pdf/test";
	
	/**
     * Main method.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
	// ---------------------------------------------------------------------------
    public static void main(String[] args)
        throws IOException, DocumentException {

    	int xdebug = 999 ; 
    	
    	int cantElements_det_1 = 1 ; 
    	
    	
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM.dd_HH_mm_ss");
		String fecha = formatter.format(new Date());
		
    	 RESULT+= "_" +  fecha + ".pdf";
    	
    	ObjectBean ob = new ObjectBean();
    	
    	PdfBean pb = new PdfBean();
    	
    	ob.setPb(pb);
    	
    	pb.setPdfFileOutputName(RESULT);
    	
    	// create a new document 
    	pb.newDocument(ob, RESULT);
    	

    	ArrayList <ArrayList<String>> aList = new ArrayList<ArrayList<String>>();
    	ArrayList <String> aListElements = new ArrayList<String> ();
    	
    	
		// this model is for two columns output
		
        String date1 = "01/08/2010" ; 
        String date2 = "08/08/2010" ; 
        
        String city = "Buenos Aires  Argentina";
        String name = "Julio M. Oliveira";
        String totV1 = "35";
        String totImpV2 = "1.452.00";
        String totC3 = 	"   300.00";
        String totNet = 	" 1.152.00";
        
        aList = new ArrayList<ArrayList<String>>();
        
        aListElements = new ArrayList<String>();

    	
    	// ------------------------------------------------

        

        aListElements.add("City: ");
        aListElements.add(city);
        
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
        
        aListElements.add("Name: ");
        aListElements.add(name);
        
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
        
        aListElements.add("TotV1: ");
        aListElements.add(totV1);
        
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
    
        aListElements.add("TotImp_V2: ");
        aListElements.add(totImpV2);
     
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
        
        aListElements.add("TotImp C3: ");
        aListElements.add(totC3);
        
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
        
        aListElements.add("Tot Net: ");
        aListElements.add(totNet);
        
        aList.add(aListElements);
        aListElements = new ArrayList<String>();
  

        
    	// ---------------------------------------------
        
    	pb.setaHead_0(aList);
 
    	
    	// -------------------------------------------

            	
    	aList = new ArrayList<ArrayList<String>>();
    	aListElements = new ArrayList<String> ();
    	
    	aListElements.add("Tit Col 1");
    	aListElements.add("Tit Col 2");
    	aListElements.add("Tit Col 3");
    	aListElements.add("Tit Col 4");
    	aListElements.add("Tit Col 5");
    	aListElements.add("Tit Col 6");

    	aList.add(aListElements);
    	aListElements = new ArrayList<String> ();
    	
    	aListElements.add("A - 111");
    	aListElements.add("A - 222");
    	aListElements.add("A - 333");
    	aListElements.add("A - 444");
    	aListElements.add("A - 555");
    	aListElements.add("A - 666");

    	aList.add(aListElements);
    	aListElements = new ArrayList<String> ();
    	
    	aListElements.add("B - 111");
    	aListElements.add("B - 222");
    	aListElements.add("B - 333");
    	aListElements.add("B - 444");
    	aListElements.add("B - 555");
    	aListElements.add("B - 666");

    	aList.add(aListElements);
    	aListElements = new ArrayList<String> ();
    	
    	aListElements.add("C - 111");
    	aListElements.add("C - 222");
    	aListElements.add("C - 333");
    	aListElements.add("C - 444");
    	aListElements.add("C - 555");
    	aListElements.add("C - 666");

    	aList.add(aListElements);
    	
    	cantElements_det_1  = aListElements.size();
    	
    	aListElements = new ArrayList<String> ();
    	


        pb.setaDet_1(aList);

    	// ------------------------------------------------------------------------------

        
        
        
        // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        // Excute the createPdf
        
        
       	// ----------------------------------------
    	// execute print title at head 
    	pb.setModel(5);
       	
    	pb.setPdfFileOutputName(RESULT);
    	// pb.setActionToTake_DOCUMENT_OPEN();
    	pb.setCantElement(3);
		String dateTitle = formatter.format(new Date()).trim();
    	pb.setTitleText("This is the head title date: " + dateTitle);
    	pb.setTitleBackgroundColor(BaseColor.GRAY);
    	pb.setTitleAlign(Element.ALIGN_CENTER);
    	
    	
    	
    	//--------------------------------------
    	createPdf(ob);  // execute model 5 (titles)
    	// -------------------------------------
    	
    	
    	
    	// ----------------------------------------
    	// execute a demo for head  ( two column elements)
    	pb.setModel(10);
       	
    	pb.setPdfFileOutputName(RESULT);
    	pb.setCantElement(3);
    	
    	//     	pb.setaHead_0(aList);
    	
    	//--------------------------------------
    	createPdf(ob);  // execute model 10 ( head)
    	// -------------------------------------
    	
    	
    	
    	pb.setModel(11);
    	pb.setCantElement(cantElements_det_1);

    	//  pb.setaDet_1(aList);
    	
    	// -------------------------------------------
    	createPdf(ob); // execute model 1  detail
    	// -------------------------------------------
    	
    	// se cierra el documento
    	pb.closeDocument();
    	
    	

    	
        // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    	
    	System.err.println("Arch OK");
 
    	
    }// end method
	
    

    /**
     * Creates a PDF with information about the movies
     * @param    String filename the name of the PDF file that will be created.
     * @param    ArrayList aList collection of aListElements
     * 			  aList is a collection of aListElements the firs the tiles others the items 	
     * @throws    DocumentException 
     * @throws    IOException
     */
    // --------------------------------------------------------------------------------

    public static void createPdf(ObjectBean ob ) {
    		      	
	 	Document document = ob.getPb().getDocument(); 
      	
    	ArrayList <ArrayList<String>> aList = new ArrayList<ArrayList<String>>();
    	ArrayList<String> aListElements = new ArrayList<String>();
    
    
    	
    	int modelNumber = ob.getPb().getModel();
    	
    	PdfPTable table ;

    	PdfPCell cell;
        	

        try {
        	// -------------------------		head 
        	if (modelNumber==10){
        		aList= ob.getPb().getaHead_0();
        		
          	}
        	
        	
        	
        	// --------------------------       detail
        	if (modelNumber==11){
        		aList= ob.getPb().getaDet_1();
        	}
            
        	
        	// Font font = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD, BaseColor.BLACK);
           	
   		 	table = new PdfPTable(ob.getPb().getCantElement());
           	
            table.setTotalWidth(500);
            table.setLockedWidth(true); 
            
        	// Title
            if (modelNumber==5){
            	
		    	  addCellTitle(ob, table);
		    	  document.add(table);
	    		
        	}// end if 	
            
        	// head
            if (modelNumber==10){
	            try {
	    			table.setWidths(new float[]{2, 1, 1});
	    		} catch (DocumentException e) {
	    			// TODO Auto-generated catch block
	    			e.printStackTrace();
	    		}
	    		
		    	  addCellTwoCol(ob, table, aList);
		    	  document.add(table);
	    		
        	}// end if 	
           	
            
            
        	// Detail 
            if (modelNumber==11){
            	
		    	  addCell(ob, table,aList);
		    	  document.add(table);
	    		
        	}// end if 
            
  	    	
	        
	        System.err.println(" Model  OK " + modelNumber);
	        
		
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.err.println("Error Document " + e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.err.println("Error Exception" + e.getMessage());
			e.printStackTrace();
		}
        
    }// end method 
    
    
     
     

     
     
 	// --------------------------------------------------------------------------------------------------
 	// @@ addCellTitle
 	// --------------------------------------------------------------------------------------------------
 	/**
 	 * To add cell to a table, for n elements
 	 * 
 	 * @param PdfPTable table - table to update  
 	 * @return PdfPTable 
 	 */
 	// -----------------------------------------------------------------
     private static PdfPTable   addCellTitle(ObjectBean ob, PdfPTable table ){
     	
     	PdfPCell cell;
     	
     	
       	try {
 		 
 			  cell = new PdfPCell(new Phrase(ob.getPb().getTitleText() ));
 			  cell.setColspan(ob.getPb().getCantElement());
 			  cell.setBackgroundColor(ob.getPb().getTitleBackgroundColor());
 			  cell.setHorizontalAlignment(ob.getPb().getTitleAlign());
 			  table.addCell(cell);
 	    	 
 		} catch (Exception e) {
 			System.err.println("(E-780555) Error  PDF - " + e.getMessage());
 		}
     	 
  
         
     	return table;
     }// end method
     
   
     

     
     
     
     
	// --------------------------------------------------------------------------------------------------
	// @@ addCellTwoCol
	// --------------------------------------------------------------------------------------------------
	/**
	 * To add cell to a table, for two elements in each array 
	 * 
	 * @param PdfPTable table - table to update  
	 * @return PdfPTable 
	 */
	// -----------------------------------------------------------------
    private static PdfPTable   addCellTwoCol(ObjectBean ob, PdfPTable table, ArrayList <ArrayList<String>> aList){
    	
    	PdfPCell cell;
    	
    	 String text;
    	 String value; 
    	 
    	try {
			// ----------------------------------  
			//  loop over the aList elements 
			 ArrayList <String> aListElements = new ArrayList<String>();
			 
			 Iterator it = aList.iterator();
			 
			 while (it.hasNext()) {
				 
				 // elements to list 
				 aListElements = (ArrayList<String>) it.next();
				 
				 Iterator itElements = aListElements.iterator();

				String val = "" ;  
				while (itElements.hasNext()) {
					// ---------------- cell line text 
					val = (String) itElements.next() ;
				    cell = new PdfPCell(new Phrase(val + " : "));
				    cell.setColspan(2);
				    table.addCell(cell);
				    
				    
				    // ----------------  cell value data alignment to the right  
					val = (String) itElements.next() ;
				    cell = new PdfPCell(new Phrase( val));
				    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				    table.addCell(cell);
				}
			
			}// end while 
		} catch (Exception e) {
			System.err.println("(E-780123) Error  PDF - " + e.getMessage());
		}
    	 
        
    	return table;
    	
    }// end method
    
        
    
    
 	// --------------------------------------------------------------------------------------------------
 	// @@ addCell
 	// --------------------------------------------------------------------------------------------------
 	/**
 	 * To add cell to a table, for n elements
 	 * 
 	 * @param PdfPTable table - table to update  
 	 * @return PdfPTable 
 	 */
 	// -----------------------------------------------------------------
     private static PdfPTable   addCell(ObjectBean ob, PdfPTable table, ArrayList <ArrayList<String>> aList ){
     	
     	PdfPCell cell;
     	
     	
       	try {
 			// ----------------------------------  
 			//  loop over the aList elements 
 			 ArrayList <String> aListElements = new ArrayList<String>();
 			 
 			 Iterator it = aList.iterator();
 			 
	    	 table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);

	    	 int rows = 0 ; 
	    	 while (it.hasNext()) {
	    		 rows ++ ; 
		    	 // elements to list 
 				 aListElements = (ArrayList<String>) it.next();
 		    	 int cantElement = aListElements.size();
   		    	 for (int x = 0; x <=cantElement-1  ; x++) {
 			        cell = new PdfPCell(new Phrase(aListElements.get(x) ));
 			        cell.setColspan(1);
 			        if (rows < 2){
 			        	cell.setBackgroundColor(BaseColor.LIGHT_GRAY );
 			        }else {
 			        	cell.setBackgroundColor(null);
 			        }
 			        // table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
 			        table.addCell(cell);
   		    	 }// end for
	    		 if (rows < 2){
	    			table.setHeaderRows(3);
   		   	 		table.setFooterRows(1);
		    		table.getDefaultCell().setBackgroundColor(null);
		    	 }
 	    	
 			}// end while
 			 
 		} catch (Exception e) {
 			System.err.println("(E-780555) Error PDF - " + e.getMessage());
 		}
     	 
    	table.setSpacingAfter(15);
    	table.setSpacingBefore(15);
 
         
     	return table;
     }// end method  
     
     
}