/*
 * 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)
 */


/*
 * AppletPanel.java
 *
 * Created on Jan 21, 2010, 3:46:07 PM
 */
package hu.astrid.debgen.print;

import java.net.URI;
import java.text.DecimalFormat;
import java.util.Locale;

/**
 *
 * @author inagy
 */
public class AppletPanel extends javax.swing.JPanel {

    //private final PrintController printController;
    private URI pdf = null;
    private URI xml = null;
    private PrintApplet applet;
    public final PrintController printController = new PrintController(this);
    private int copies;

    /** Creates new form AppletPanel */
    public AppletPanel() {
        initComponents();
        //this.printController = new PrintController(this);
    }

    AppletPanel(Locale locale) {
        this.setLocale(locale);
        Locale.setDefault(locale);
        initComponents();
    }

    public void print(final URI xml, final URI pdf, final int copies) {
        this.xml = xml;
        this.pdf = pdf;
        this.copies = copies;
        if (pdf == null) {
            printXml(xml);
        } else if (xml == null) {
            printPdf(pdf, copies);
        } else {
            java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
            phaseOneLabel.setText(bundle.getString("print_datasheet"));
            phaseTwoLabel.setText(bundle.getString("print_label"));
            this.progressBar.setVisible(true);
            this.phaseOneIcon.setEnabled(true);
            this.labelProgressLabel.setText(""+copies);
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        printController.print(xml, pdf, copies);
                    } catch (Exception ex) {
                        error(ex);
                    }
                }
            };
            thread.run();
        }
    }

    public void printXml(final URI xml) {
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        phaseTwoLabel.setText(bundle.getString("print_label"));
        this.phaseTwoIcon.setEnabled(true);
        this.phaseTwoIcon.paintImmediately(0, 0, 30, 30);
        this.paintImmediately(0, 0, 400, 300);
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    printController.printXml(xml);
                    phaseTwoIcon.setEnabled(true);
                } catch (Exception ex) {
                    error(ex);
                }
            }
        };
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.run();

    }

    public void printPdf(final URI pdf, final int copies) {
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        phaseOneLabel.setText(bundle.getString("print_datasheet"));
        phaseOneIcon.setEnabled(true);
        this.phaseOneIcon.paintImmediately(0, 0, 30, 30);
        this.paintImmediately(0, 0, 400, 300);
        this.phaseTwoLabel.setText("");
        this.phaseTwoIcon.setVisible(false);
        Thread thread = new Thread() {

            @Override
            public void run() {
                try {
                    printController.printPdf(pdf, copies);
                    Thread.sleep(5000);
                    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
                    phaseOneLabel.setText(bundle.getString("print_datasheet_done"));
                    phaseOneIcon.setEnabled(false);
                } catch (Exception ex) {
                    error(ex);
                }

            }
        };
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.run();

    }

    void error(Exception ex) {
        this.applet.redirect("reprint"+(xml == null ? "module" : "")+".html?error=true");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        todoPanel = new javax.swing.JPanel();
        phaseOneLabel = new javax.swing.JLabel();
        phaseOneIcon = new javax.swing.JLabel();
        phaseTwoLabel = new javax.swing.JLabel();
        phaseTwoIcon = new javax.swing.JLabel();
        bottomPanel = new javax.swing.JPanel();
        labelPrintLabel = new javax.swing.JLabel();
        printerLabel = new javax.swing.JLabel();
        label = new javax.swing.JLabel();
        labelName = new javax.swing.JLabel();
        labelProgressLabel = new javax.swing.JLabel();
        totalProgressLabel = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        ramainingTime = new javax.swing.JLabel();
        remainingTimeLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        setBackground(new java.awt.Color(255, 255, 255));
        setMaximumSize(new java.awt.Dimension(390, 300));
        setMinimumSize(new java.awt.Dimension(390, 300));
        setPreferredSize(new java.awt.Dimension(390, 300));

        todoPanel.setBackground(new java.awt.Color(255, 255, 255));
        todoPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(63,161,203)));
        todoPanel.setForeground(new java.awt.Color(63,161,203));
        todoPanel.setMaximumSize(new java.awt.Dimension(366, 84));
        todoPanel.setMinimumSize(new java.awt.Dimension(366, 84));

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        phaseOneLabel.setText(bundle.getString("print_datasheet")); // NOI18N

        phaseOneIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hu/astrid/debgen/print/images/left.png"))); // NOI18N

        phaseTwoLabel.setText(bundle.getString("print_label")); // NOI18N

        phaseTwoIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/hu/astrid/debgen/print/images/left.png"))); // NOI18N
        phaseTwoIcon.setEnabled(false);

        javax.swing.GroupLayout todoPanelLayout = new javax.swing.GroupLayout(todoPanel);
        todoPanel.setLayout(todoPanelLayout);
        todoPanelLayout.setHorizontalGroup(
            todoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(todoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(todoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(phaseOneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phaseTwoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(todoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(phaseOneIcon, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(phaseTwoIcon, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        todoPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {phaseOneIcon, phaseTwoIcon});

        todoPanelLayout.setVerticalGroup(
            todoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(todoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(todoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(todoPanelLayout.createSequentialGroup()
                        .addComponent(phaseOneIcon, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(phaseTwoIcon, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE))
                    .addGroup(todoPanelLayout.createSequentialGroup()
                        .addComponent(phaseOneLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(phaseTwoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        bottomPanel.setBackground(new java.awt.Color(255, 255, 255));
        bottomPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(63,161,203)));
        bottomPanel.setForeground(new java.awt.Color(63,161,203));
        bottomPanel.setMaximumSize(new java.awt.Dimension(366, 186));
        bottomPanel.setMinimumSize(new java.awt.Dimension(366, 186));

        labelPrintLabel.setText(bundle.getString("pdfprint_with_printer")); // NOI18N

        printerLabel.setText(bundle.getString("printer")); // NOI18N

        label.setText(bundle.getString("pdf")); // NOI18N

        labelName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelName.setText("          ");

        labelProgressLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        labelProgressLabel.setText("-");

        totalProgressLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        totalProgressLabel.setText("  ");

        total.setText(bundle.getString("empty")); // NOI18N

        ramainingTime.setText(bundle.getString("empty")); // NOI18N

        remainingTimeLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        remainingTimeLabel.setText("     ");

        progressBar.setBackground(new java.awt.Color(255, 255, 255));
        progressBar.setForeground(new java.awt.Color(63, 161, 203));
        progressBar.setIndeterminate(true);

        javax.swing.GroupLayout bottomPanelLayout = new javax.swing.GroupLayout(bottomPanel);
        bottomPanel.setLayout(bottomPanelLayout);
        bottomPanelLayout.setHorizontalGroup(
            bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bottomPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(progressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                    .addComponent(printerLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPrintLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 338, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, bottomPanelLayout.createSequentialGroup()
                        .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(total, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(ramainingTime, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, bottomPanelLayout.createSequentialGroup()
                                .addComponent(label, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelName, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(remainingTimeLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)
                            .addGroup(bottomPanelLayout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelProgressLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                                    .addComponent(totalProgressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        bottomPanelLayout.setVerticalGroup(
            bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(bottomPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelPrintLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(printerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label)
                    .addComponent(labelProgressLabel)
                    .addComponent(labelName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(total)
                    .addComponent(totalProgressLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ramainingTime)
                    .addComponent(remainingTimeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bottomPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(todoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(todoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bottomPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPanel;
    private javax.swing.JLabel label;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelPrintLabel;
    private javax.swing.JLabel labelProgressLabel;
    private javax.swing.JLabel phaseOneIcon;
    private javax.swing.JLabel phaseOneLabel;
    private javax.swing.JLabel phaseTwoIcon;
    private javax.swing.JLabel phaseTwoLabel;
    private javax.swing.JLabel printerLabel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel ramainingTime;
    private javax.swing.JLabel remainingTimeLabel;
    private javax.swing.JPanel todoPanel;
    private javax.swing.JLabel total;
    private javax.swing.JLabel totalProgressLabel;
    // End of variables declaration//GEN-END:variables

    public void setCurrentLabelName(String codeValue) {
        this.labelName.setText(codeValue);
//        this.paintImmediately(0, 0, 400, 300);
    }

    public void setCurrentLabelProcess(int current, int max) {
        this.labelProgressLabel.setText("(" + current + "/" + max + ")");
//        this.paintImmediately(0, 0, 400, 300);
    }

    public void setTotalLabelProcess(int current, int total) {
        this.totalProgressLabel.setText("(" + current + "/" + total + ")");
        this.progressBar.setMaximum(total);
        this.progressBar.setValue(current);
        this.remainingTimeLabel.setText(new DecimalFormat("00").format((total - current) / 60) + ":" + new DecimalFormat("00").format((total - current) % 60));
//        this.paintImmediately(0, 0, 400, 300);
    }

    public void cancelLabels() {
        this.labelProgressLabel.setText("-");
        this.labelName.setText("----------");
        this.totalProgressLabel.setText("-");
        this.progressBar.setValue(progressBar.getMaximum());
        this.remainingTimeLabel.setText("--:--");
        this.phaseOneIcon.setVisible(false);
        this.phaseTwoIcon.setVisible(false);
    }

    public void doneLabels() {
//        this.phaseOneIcon.setVisible(false);
//        this.phaseTwoIcon.setVisible(false);
        this.phaseTwoIcon.setEnabled(false);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        phaseTwoLabel.setText(bundle.getString("print_label_done"));
        
    }

    public void setApplet(PrintApplet a) {
        this.applet = a;
    }

    public void setPrinterName(String pdfPrinter) {
        this.printController.setPrinterName(pdfPrinter);
    }

    public void pdfDone() {
        this.phaseOneIcon.setEnabled(false);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        phaseOneLabel.setText(bundle.getString("print_datasheet_done"));
        progressBar.setValue(progressBar.getMaximum());
        progressBar.setIndeterminate(false);
        progressBar.setStringPainted(true);
    }

    public void startlabels() {
        phaseTwoIcon.setEnabled(true);
        progressBar.setIndeterminate(false);
        progressBar.setStringPainted(true);
        progressBar.setValue(0);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        labelPrintLabel.setText(bundle.getString("labelprint_with_printer"));
        printerLabel.setText("Zebra Printer");
        label.setText(bundle.getString("label")); // NOI18N
        labelName.setText("----------");
        labelProgressLabel.setText("-");
        totalProgressLabel.setText("-");
        total.setText(bundle.getString("total")); // NOI18N
        ramainingTime.setText(bundle.getString("remainingTime")); // NOI18N
        remainingTimeLabel.setText("--:--");
    }

    public void startPdf() {
        phaseOneIcon.setEnabled(true);
        phaseTwoIcon.setEnabled(false);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("hu/astrid/debgen/localization"); // NOI18N
        labelPrintLabel.setText(bundle.getString("pdfprint_with_printer")); // NOI18N
        printerLabel.setText(this.printController.getPdfPrinter()); // NOI18N
        label.setText(bundle.getString("pdf")); // NOI18N
        labelName.setText("          ");
        labelProgressLabel.setText(""+this.copies);
        totalProgressLabel.setText("  ");
        total.setText("          "); // NOI18N
        ramainingTime.setText("          "); // NOI18N
        remainingTimeLabel.setText("     ");
        progressBar.setIndeterminate(true);
        progressBar.setStringPainted(false);
    }
//    public void setZebraPrinterName(String zebraName) {
//        this.printerLabel.setText(zebraName);
//    }
}
