/*
 * Copyright 2009 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.gui;

import net.aavalla.utils.gui.twovenn.VennDiagram;
import net.aavalla.utils.jobpool.parameters.gui.ParameterDialog;
import compid.application.CompidApp;
import compid.report.ReportModel;
import compid.report.ReportPageModel;
import compid.report.exporters.ExcelExporter;
import net.aavalla.utils.jobpool.parameters.FieldInstance;
import net.aavalla.utils.jobpool.parameters.ParameterCollection;
import compid.report.VennReportPageModel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;

/**
 *
 * @author Lari Natri
 */
public class ReportViewPanel extends javax.swing.JPanel {

    private JTabbedPane fullRepTabbedPane;
    private final ReportModel model;

    /** Creates new form FullReportPanel */
    public ReportViewPanel(ReportModel model) {
        initComponents();

        this.model = model;
        fullRepSubtitle.setText(model.getSubtitle());


        if (model.getPageCount()==1) {

            if (model.getPage(0) instanceof VennReportPageModel) {
                VennReportPageModel vPage = (VennReportPageModel)model.getPage(0);
                vPage.getVennParameters().hideFieldByName("A: total size");
                vPage.getVennParameters().hideFieldByName("B: total size");
                vPage.getVennParameters().hideFieldByName("A\u2229B: size");
                VennDiagram d = new VennDiagram(vPage.getVennParameters(),CompidApp.getApplication().getJobPool());
                fullRepContainer.add(d);
            } else {
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.setBorder(null);
                CompidTable table = new CompidTable(model.getPage(0));
                table.setTooltipBreakSymbol(";; ");
                table.setAutoCreateColumnsFromModel(true);
                table.setAutoCreateRowSorter(true);
                scrollPane.setViewportView(table);
                fullRepContainer.add(scrollPane);
            }
        } else if (model.getPageCount()>1){
            fullRepTabbedPane = new JTabbedPane();
            for (ReportPageModel page : model) {
                if (page instanceof VennReportPageModel) {
                    VennReportPageModel vPage = (VennReportPageModel)page;
                    vPage.getVennParameters().hideFieldByName("A: total size");
                    vPage.getVennParameters().hideFieldByName("B: total size");
                    vPage.getVennParameters().hideFieldByName("A\u2229B: size");
                    VennDiagram d = new VennDiagram(vPage.getVennParameters(),CompidApp.getApplication().getJobPool());
                    fullRepTabbedPane.addTab(vPage.getTitle(),d);
                } else {                
                    JScrollPane scrollPane = new JScrollPane();
                    CompidTable table = new CompidTable(page);
                    table.setTooltipBreakSymbol(";; ");
                    table.setAutoCreateColumnsFromModel(true);
                    table.setAutoCreateRowSorter(true); 
                    scrollPane.setViewportView(table);
                    fullRepTabbedPane.addTab(page.getTitle(), scrollPane);
                }
                fullRepTabbedPane.setTabComponentAt(fullRepTabbedPane.getTabCount()-1, new ClosableTabComponent(fullRepTabbedPane));
            }
            fullRepContainer.add(fullRepTabbedPane);

        }

        if (model.getParameters()==null) {
            parametersButton.setEnabled(false);
        }
        
        fullRepContainer.validate();

    }

    public void exportToExcel() {
        ExcelExporter exporter = new ExcelExporter();
        ParameterCollection params = exporter.getParametersForModification();
        for (FieldInstance f : params) {
            if (f.getType().equals(ReportModel.class)) {
                f.setValue(this.model);
            }
        }
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, exporter, CompidApp.getApplication().getJobPool(),null);
        dialog.setVisible(true);
    }

    public void showParameters() {
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, model.getParameters(),null);
        dialog.setVisible(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() {

        fullRepToolBar = new javax.swing.JToolBar();
        fullRepSubtitle = new javax.swing.JTextField();
        parametersButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        exportButton = new javax.swing.JButton();
        fullRepContainer = new javax.swing.JPanel();

        setName("Form"); // NOI18N

        fullRepToolBar.setFloatable(false);
        fullRepToolBar.setRollover(true);
        fullRepToolBar.setName("fullRepToolBar"); // NOI18N

        fullRepSubtitle.setEditable(false);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(compid.application.CompidApp.class).getContext().getResourceMap(ReportViewPanel.class);
        fullRepSubtitle.setText(resourceMap.getString("fullRepSubtitle.text")); // NOI18N
        fullRepSubtitle.setName("fullRepSubtitle"); // NOI18N
        fullRepToolBar.add(fullRepSubtitle);

        parametersButton.setIcon(resourceMap.getIcon("parametersButton.icon")); // NOI18N
        parametersButton.setText(resourceMap.getString("parametersButton.text")); // NOI18N
        parametersButton.setToolTipText(resourceMap.getString("parametersButton.toolTipText")); // NOI18N
        parametersButton.setFocusable(false);
        parametersButton.setName("parametersButton"); // NOI18N
        parametersButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        parametersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parametersButtonActionPerformed(evt);
            }
        });
        fullRepToolBar.add(parametersButton);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fullRepToolBar.add(jSeparator1);

        exportButton.setIcon(resourceMap.getIcon("exportButton.icon")); // NOI18N
        exportButton.setText(resourceMap.getString("exportButton.text")); // NOI18N
        exportButton.setToolTipText(resourceMap.getString("exportButton.toolTipText")); // NOI18N
        exportButton.setFocusable(false);
        exportButton.setName("exportButton"); // NOI18N
        exportButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        exportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportButtonActionPerformed(evt);
            }
        });
        fullRepToolBar.add(exportButton);

        fullRepContainer.setName("fullRepContainer"); // NOI18N
        fullRepContainer.setLayout(new java.awt.GridLayout(1, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fullRepToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
            .addComponent(fullRepContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fullRepToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fullRepContainer, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void parametersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parametersButtonActionPerformed
        this.showParameters();
    }//GEN-LAST:event_parametersButtonActionPerformed

    private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
        this.exportToExcel();
    }//GEN-LAST:event_exportButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton exportButton;
    private javax.swing.JPanel fullRepContainer;
    private javax.swing.JTextField fullRepSubtitle;
    private javax.swing.JToolBar fullRepToolBar;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JButton parametersButton;
    // End of variables declaration//GEN-END:variables

}
