
package sims.uae.crystalreportgenerator.hardcoded;

import com.lowagie.text.Cell;


import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.rtf.RtfHeaderFooter;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;

import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;

//import com.lowagie.text.rtf.RtfDestination;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;

//import com.lowagie.text.pPCell;
//import com.lowagie.text.pdf.RtfPTable;
//IText imports
import com.lowagie.text.rtf.RtfWriter;

import java.awt.Color;
import java.awt.Point;

import java.io.File;
import java.io.FileOutputStream;

import java.util.Hashtable;
import java.util.Vector;


/**
 * This class is used to generate a concatinated RTF report.
 */
public class RTFDocGenerator{
    public final static Rectangle STANDARD_PAGE_SIZE = new Rectangle(612, 792);
    public boolean debug_flag = true;

    //Font c_timesNormal12;
    private Font c_courier8; //used for padding at top and bottom of inside the tables

    //fonts used inside the report

    private Font c_courierBold;
    private Font c_courierBoldUnderline;
    private Font c_courierNormal;
    private Font c_timesBold12;

    //Hashtable that contains the Meta Data info for CM Data area of report

    private Table c_CMData;
    private String c_footerString = "REPORT INFORMATION\n";
    private String c_headFootAdd = ""; //4 choices of heading, will be read from report server
    private String c_headerString = "COMPANY NAME\n";

    private RtfHeaderFooter c_rtfHeader;
    private RtfHeaderFooter c_rtfFooter;

    //Handle to the RTF page contents
    //Handle to the document which represents a PDF document
    private Document c_document;

    //Working directory
    private File c_workingDirectory;

    //instances to other classes in this package used in this file
    //Handle to a PDF writer which is responsible for writing data to a file.
    private RtfWriter c_writer;
    private SimpleData c_simpleData;



    //Keeps track of temp files used to generate report

    /**
     * Constructor
     *
     * @param p_MIL1553 Working directory
     * @param p_simpleData Node count associated with this report
     * @param p_metaDataHash workspace reference data
     */
    public RTFDocGenerator(){

        // setHeader
        //creating "Courier New" fonts from the base font for both bold and normal.
        try{
            c_courierBold = FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD);
            c_courierNormal = FontFactory.getFont(FontFactory.COURIER, 10, Font.NORMAL);
            c_courierBoldUnderline = FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD | Font.UNDERLINE);
            c_timesBold12 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);

            c_courier8 = FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL);
            setCMData();
            generateRTFDocument();
        }
        catch (Exception e){
            System.out.println("Error " + e.getMessage());
        }
    }

    /**
     * Used to generate a PDF document tree p_documentTreeNode - tree node to
     * generate document with
     *
     * @return l_file.getAbsolutePath(): The output file path
     *
     * @throws Exception If unable to generate rtf report.
     */
    public String generateRTFDocument() throws Exception{
        //Create a temp file to hold the c_workingDirectory document tree
       File l_workingDirectory =  new File("C:/temp");

        File l_file = File.createTempFile("myTable", ".rtf", c_workingDirectory);

        //Create a new document
        c_document = new Document(STANDARD_PAGE_SIZE);

        //Create a writer to store the contents of the document to.
        c_writer = RtfWriter.getInstance(c_document, new FileOutputStream(l_file.getAbsolutePath()));

        //Add information to be displayed in the document summary
        c_document.addTitle("");
        c_document.addSubject("");
        c_document.addKeywords("");
        c_document.addCreator("Management System)");
        c_document.addAuthor("");

        processHeaderFooter(true); //process the header part
        processHeaderFooter(false); //process the footer part

        //Open the document so we can write to it
        c_document.open();

        //get the content of the RTF file once and use it all over, since multiple calls returns the same thing.
       // processContent();

        c_document.setHeader(c_rtfHeader); //add the header part
        c_document.setFooter(c_rtfFooter);  //add the footer part
        //Close the document
        c_document.close();

        //Delete all the temp files
        //deleteTempFiles();
        //return the absolute path.
        return l_file.getAbsolutePath();
    }



    /**
     * setCMData sets the CM Data information for the header.
     *
     * @param p_simpleDataHash - HashTable for Simple Data info
     */
    private void setCMData(){
        try{
            Table l_table = new Table(4, 1);
            l_table.addCell(new Cell("String 1"));
            l_table.addCell(new Cell("String 2"));
            l_table.addCell(new Cell("String 3"));
            l_table.addCell(new Cell("String 4"));

            c_CMData = l_table;
        }
        catch (DocumentException de){
            System.err.println(de.getMessage());
        }
        catch (Exception e){
            System.err.println(e.getMessage());
        }
    }

    /**
     * diplays the section under Word Name that has ..... to connect the two
     * columns of text.
     *
     * @param p_simpleDataHash
     */
    private void displayOtherData(Hashtable p_simpleDataHash){

    }



    /**
     * ProcessHeaderFooter - sets both header and footer. p_flag = 1  sets
     * header p_flag = 0  sets footer
     *
     * @param p_flag If True add header and footer data.
     */
    private void processHeaderFooter(boolean p_flag){
        /* HeaderFooter footer = new HeaderFooter(new Phrase("This is page: "), true);
             footer.setBorder(Rectangle.NO_BORDER);
             document.setFooter();*/

        //Header / footer string setup
        try{
            //if (p_flag){   //first time this is called go ahead and calculate it
            Phrase l_phraseHF = new Phrase(12, "                                         " + c_headerString, c_timesBold12);
            String l_value = "                          Name of the Department";


            //Final phrase is for both header and footer
            l_phraseHF.add(new Phrase(12, l_value, c_timesBold12)); //padding spaces in front to center the title
            l_phraseHF.add(new Phrase(12, c_headFootAdd + "\n", c_timesBold12));

            HeaderFooter l_hf;
            RtfHeaderFooter l_rtfHF;

            if (p_flag){ //header information
                String l_stringName = "SPECIFICATION";

                String l_cmTitle1 = new String("          Data                                       Specification: " + l_stringName + "\n");
                StringBuffer l_lineStringBuff = new StringBuffer();
                String l_cmTitle = l_cmTitle1;
                Phrase l_phraseHeader = new Phrase();
                l_phraseHeader.add(l_phraseHF); //add the top header
                l_phraseHeader.add(new Phrase(10, "\n" + l_cmTitle, c_courierBold)); //add the cm data title

                l_phraseHeader.add(c_CMData); //add the CM data table*/



                //l_hf = new HeaderFooter(l_phraseHeader, false);
                c_rtfHeader = new RtfHeaderFooter (l_phraseHeader, false);
                //l_rtfHF.set(RtfHeaderFooters.ALL_PAGES, l_hf);
            }
            else{ //footer time

                Phrase l_phraseFoot = new Phrase();
                l_phraseFoot.add(l_phraseHF);
                l_phraseFoot.add(new Phrase("                                                                                  Page: ", c_courierNormal));
                //l_hf = new HeaderFooter(l_phraseFoot, true);
                c_rtfFooter = new RtfHeaderFooter(l_phraseFoot, true);
               // l_rtfHF.set(RtfHeaderFooters.ALL_PAGES, l_hf);
            }

           //l_hf.setAlignment(l_hf.ALIGN_UNDEFINED);
           //l_hf.setBorder(2);

           /*if (p_flag){ //time to set header
                c_document.setHeader(l_rtfHF);
            }
            else //time to set footer
            {
                c_document.setFooter(l_rtfHF);
            }*/
        }
        catch (Exception l_exc){
            System.out.println("Error " + l_exc.getMessage());
        }
    }


    public static void main(String [] args){
      new RTFDocGenerator();
    }


}
