/*
 * This file is part of DebGen Biobank Software developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 * Copyright ASTRID Research Inc., Hungary, 2009-2010.
 *
 * ASTRID Research
 * Headquaters: Csapo utca 42. Debrecen H-4029 HUNGARY
 * Phone: +36-52-533-173        Fax: +36-52-533-174
 * Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DebGen Biobank Software project.
 * URLS:
 * Developers: Istvan Zoltan Nagy (istvan.nagy@astrid.hu)
 */
package hu.astrid.debgen.print;

import java.applet.Applet;
import java.awt.Color;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author inagy
 */
public class PrintApplet extends Applet {

    private AppletPanel panel = null;
    private String pdfPrinter = null;
//    private String zebraName;
    private String xmlName = null;
    private String pdfName = null;
    private int pdfCopies = 1;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
        this.setVisible(true);
        this.setBounds(0, 0, 400, 300);
        this.setBackground(Color.WHITE);
        panel = new AppletPanel(new Locale(this.getParameter("language").toLowerCase()));
        panel.setApplet(this);
//        panel.setLocale(new Locale(this.getParameter("language")));
        this.add(panel);
        pdfPrinter = this.getParameter("printer");
//        zebraName = this.getParameter("zebra");
        xmlName = this.getParameter("xml");
        pdfName = this.getParameter("pdf");
        try {
            pdfCopies = Integer.parseInt(this.getParameter("copies"));
        } catch (NumberFormatException numberFormatException) {
            //IGNORED
        }

    }

    @Override
    public void start() {
        try {
            super.start();
            Thread.yield();
            this.panel.setPrinterName(pdfPrinter);
            URI pdf = new URI(this.getCodeBase().toString() + pdfName);
            if (xmlName != null) {
                URI xml = new URI(this.getCodeBase().toString() + xmlName);
                this.panel.print(xml, pdf, pdfCopies);
                Thread.sleep(2000);
                this.redirect("reprint.html?pdf=" + pdfName + "&xml=" + xmlName + "&copies=" + pdfCopies);
            } else {
                this.panel.print(null, pdf, pdfCopies);
                Thread.sleep(2000);
                this.redirect("reprintmodule.html?pdf=" + pdfName + "&copies=" + pdfCopies);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(PrintApplet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (URISyntaxException ex) {
            Logger.getLogger(PrintApplet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void redirect(String string) {
        try {
            getAppletContext().showDocument(new URL(getCodeBase().toString() + string));
        } catch (MalformedURLException ex) {
            Logger.getLogger(PrintApplet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
