/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nsn.commons.pdf;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfAppearance;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;
import com.nsn.swa.domain.core.checklist.CheckList;
import com.nsn.swa.domain.core.checklist.CheckListGroup;
import com.nsn.swa.domain.core.checklist.CheckListItem;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 *
 * @author uph328
 */
public class RegisterForm1 implements PdfPCellEvent {

        protected PdfFormField parent;

        protected PdfFormField kid;

        protected float padding;

        public RegisterForm1(PdfFormField parent, PdfFormField kid, float padding) {
                this.kid = kid;
                this.parent = parent;
                this.padding = padding;
        }

        /**
         * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell,
         *      com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[])
         */
        public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
                kid.setWidget(new Rectangle(rect.getLeft(padding), rect.getBottom(padding),
                                rect.getRight(padding), rect.getTop(padding)),
                                PdfAnnotation.HIGHLIGHT_INVERT);
                try {
                        parent.addKid(kid);
                } catch (Exception e) {
                        throw new ExceptionConverter(e);
                }
        }

        public static void createPdf(CheckList checkList) {
                // step 1
                Document document = new Document();
                try {
                        PdfWriter writer = PdfWriter.getInstance(document,
                                        new FileOutputStream("C:\\Temp\\register_form" + new Random().nextInt() + ".pdf"));
                        document.open();
                                                
                        for (CheckListGroup g : checkList.getGroups()){
                            PdfFormField field = PdfFormField.createEmpty(writer);
                            document.add(createTable(writer, field, g));
                            writer.addAnnotation(field);
                        }
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }
                // step 5
                document.close();
        }

        private static PdfPTable createTable(PdfWriter writer, PdfFormField parent, CheckListGroup group)
                        throws IOException, DocumentException {
                PdfContentByte cb = writer.getDirectContent();
		PdfAppearance[] buttonStates = new PdfAppearance[2];
		buttonStates[0] = cb.createAppearance(20, 20);
		buttonStates[1] = cb.createAppearance(20, 20);
		buttonStates[1].moveTo(0, 0);
		buttonStates[1].lineTo(20, 20);
		buttonStates[1].moveTo(0, 20);
		buttonStates[1].lineTo(20, 0);
		buttonStates[1].stroke();

		PdfPTable table = new PdfPTable(2);
		PdfPCell cell;
		TextField field;
		table.getDefaultCell().setPadding(5f);

		table.addCell("Your name:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "name" + group.getId());
		field.setFontSize(12);
		cell.setCellEvent(new RegisterForm1(parent, field.getTextField(), 1));
		table.addCell(cell);

		table.addCell("Your home address:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "address" + group.getId());
		field.setFontSize(12);
		cell.setCellEvent(new RegisterForm1(parent, field.getTextField(), 1));
		table.addCell(cell);

		table.addCell("Postal code:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "postal_code" + group.getId());
		field.setFontSize(12);
		cell.setCellEvent(new RegisterForm1(parent, field.getTextField(), 1));
		table.addCell(cell);

		table.addCell("Your email address:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "email" + group.getId());
		field.setFontSize(12);
		cell.setCellEvent(new RegisterForm1(parent, field.getTextField(), 1));
		table.addCell(cell);

		table.addCell("Programming skills:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "programming" + group.getId());
		field.setFontSize(9);
		String[] list_options = { "JAVA", "C", "CS", "VB" };
		field.setChoiceExports(list_options);
		String[] list_values = { "Java", "C/C++", "C#", "VB" };
		field.setChoices(list_values);
		PdfFormField f = field.getListField();
		f.setFieldFlags(PdfFormField.FF_MULTISELECT);
		cell.setCellEvent(new RegisterForm1(parent, f, 0));
		cell.setMinimumHeight(50);
		table.addCell(cell);

		table.addCell("Mother tongue:");
		cell = new PdfPCell();
		field = new TextField(writer, new Rectangle(0, 0), "language" + group.getId());
		field.setFontSize(9);
		String[] combo_options = { "EN", "FR", "NL" };
		field.setChoiceExports(combo_options);
		String[] combo_values = { "English", "French", "Dutch" };
		field.setChoices(combo_values);
		f = field.getComboField();
		cell.setCellEvent(new RegisterForm1(parent, f, 0));
		table.addCell(cell);

		PdfFormField f1 = PdfFormField.createRadioButton(writer, true);
		f1.setFieldName("preferred" + group.getId());
		parent.addKid(f1);
		table.addCell("Preferred Language:");
		float[] widths = { 1, 10 };
		PdfPTable subtable = new PdfPTable(widths);
		cell = new PdfPCell();
		PdfFormField checkbox = PdfFormField.createEmpty(writer);
		checkbox.setValueAsName("EN");
		checkbox.setAppearanceState("EN");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "EN",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f1, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("English");
		cell = new PdfPCell();
		checkbox = PdfFormField.createEmpty(writer);
		checkbox.setValueAsName("Off");
		checkbox.setAppearanceState("Off");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "FR",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f1, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("French");
		cell = new PdfPCell();
		checkbox = PdfFormField.createEmpty(writer);
		checkbox.setValueAsName("Off");
		checkbox.setAppearanceState("Off");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "NL",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f1, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("Dutch");
		table.addCell(new PdfPCell(subtable));

		PdfFormField f2 = PdfFormField.createEmpty(writer);
		f2.setFieldName("knowledge" + group.getId());
		parent.addKid(f2);
		table.addCell("Knowledge of:");
		subtable = new PdfPTable(widths);
		cell = new PdfPCell();
		checkbox = PdfFormField.createCheckBox(writer);
		checkbox.setFieldName("English");
		checkbox.setValueAsName("Off");
		checkbox.setAppearanceState("Off");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f2, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("English");
		cell = new PdfPCell();
		checkbox = PdfFormField.createCheckBox(writer);
		checkbox.setFieldName("French");
		checkbox.setValueAsName("Off");
		checkbox.setAppearanceState("Off");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f2, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("French");
		cell = new PdfPCell();
		checkbox = PdfFormField.createCheckBox(writer);
		checkbox.setFieldName("Dutch");
		checkbox.setValueAsName("Off");
		checkbox.setAppearanceState("Off");
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off",
				buttonStates[0]);
		checkbox.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On",
				buttonStates[1]);
		cell.setCellEvent(new RegisterForm1(f2, checkbox, 0));
		subtable.addCell(cell);
		subtable.addCell("Dutch");
		table.addCell(new PdfPCell(subtable));

		return table;
        }
}
