 /*
 *
 * Autopsy Forensic Browser
 * 
 * Copyright 2012 Basis Technology Corp.
 * 
 * Copyright 2012 42six Solutions.
 * Contact: aebadirad <at> 42six <dot> com
 * Project Contact/Architect: carrier <at> sleuthkit <dot> org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.report;

import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import org.sleuthkit.autopsy.coreutils.Logger;
import javax.swing.*;
import javax.swing.border.Border;
import org.openide.util.Lookup;
import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.datamodel.BlackboardArtifact;
import org.sleuthkit.datamodel.SleuthkitCase;

/**
 *
 * @author Alex
 */
public class ReportFilter extends javax.swing.JPanel {

    public static ArrayList<Integer> filters = new ArrayList<Integer>();
    public static ReportConfiguration config = new ReportConfiguration();
    private final Logger logger = Logger.getLogger(this.getClass().getName());
    public final ReportFilter panel = this;
    ReportPanelAction rpa = new ReportPanelAction();
    public static boolean cancel = false;
    Case currentCase = Case.getCurrentCase(); // get the most updated case
    SleuthkitCase skCase = currentCase.getSleuthkitCase();

    /**
     * Creates new form ReportFilter
     */
    public ReportFilter() {
        this.setLayout(new GridLayout(0, 1));
        initComponents();
        cancel = false;
        try {
            config.getAllTypes();

        } catch (ReportModuleException ex) {
            Logger.getLogger(Report.class.getName()).log(Level.WARNING, "Exception occurred", ex);
        }
    }

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

        jButton2 = new javax.swing.JButton();
        progBar = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        updateLabel = new javax.swing.JLabel();

        jButton2.setText(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.jButton2.text")); // NOI18N
        jButton2.setActionCommand(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.jButton2.actionCommand")); // NOI18N
        jButton2.setLabel(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.jButton2.label")); // NOI18N

        setPreferredSize(new java.awt.Dimension(500, 75));

        progBar.setDoubleBuffered(true);
        progBar.setEnabled(false);
        progBar.setName(""); // NOI18N
        progBar.setPreferredSize(new java.awt.Dimension(146, 15));
        progBar.setString(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.progBar.string")); // NOI18N
        progBar.setStringPainted(true);

        jButton1.setText(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.jButton1.text")); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButton1MouseReleased(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        cancelButton.setText(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.cancelButton.text")); // NOI18N
        cancelButton.setActionCommand(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.cancelButton.actionCommand")); // NOI18N
        cancelButton.setEnabled(false);
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        updateLabel.setText(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.updateLabel.text")); // NOI18N
        updateLabel.setToolTipText(org.openide.util.NbBundle.getMessage(ReportFilter.class, "ReportFilter.updateLabel.toolTipText")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(updateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(progBar, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cancelButton)))
                        .addGap(24, 24, 24))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(progBar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 11, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void getfilters(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
    }

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    jButton1.setEnabled(false);
    progBar.setEnabled(true);
    cancelButton.setEnabled(true);
    progBar.setStringPainted(true);
    progBar.setValue(0);
    ReportConfiguration newConfig = ReportAction.config;
    ArrayList<String> preview = ReportAction.preview;
    ArrayList<JCheckBox> reportList = ReportAction.reportList;
    ArrayList<String> classList = new ArrayList<String>();
    for (JCheckBox box : reportList) {
        if (box.isSelected()) {
            classList.add(box.getName());

        }
    }
    config.resetGenArtifactTypes();
    getReports(newConfig, classList, preview);
}//GEN-LAST:event_jButton1ActionPerformed

    public void getReports(final ReportConfiguration reportConfig, final ArrayList<String> classList, final ArrayList<String> preview) {
        new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() throws Exception {
                rpa.reportGenerate(reportConfig, classList, preview, panel);
                return null;
            }

            ;

     // this is called when the SwingWorker's doInBackground finishes
            @Override
            protected void done() {
                progBar.setVisible(false); // hide my progress bar JFrame
            }
        ;
        }.execute();
  progBar.setVisible(true);
    }

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    cancelButton.setText("Cancelled!");
    cancel = true;
}//GEN-LAST:event_cancelButtonActionPerformed

private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseReleased
}//GEN-LAST:event_jButton1MouseReleased

    public void progBarSet(int cc) {
        final int count = cc;
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                int start = progBar.getValue();
                int end = start + count;
                progBar.setValue(end);
                progBar.setString(null);
                progBar.setString(progBar.getString());
                progBar.setStringPainted(true);
                if (progBar.getPercentComplete() == 1.0) {
                    progBar.setString("Populating Report - Please wait...");
                    progBar.setStringPainted(true);
                    progBar.setIndeterminate(true);
                }
            }
        });
    }

    public void progBarDone() {
        int max = progBar.getMaximum();
        progBar.setValue(max);
        jButton2.doClick();
    }

    public void progBarStartText() {
        progBar.setIndeterminate(true);
        progBar.setString("Querying Database for Report Results...");
    }

    public void setUpdateLabel(final String text) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                updateLabel.setText(text);
                updateLabel.repaint();
            }
        });

    }

    public void progBarText() {

        progBar.setString("Populating Report - Please wait...");
        progBar.setStringPainted(true);
        progBar.repaint();
        progBar.setIndeterminate(true);

    }

    public void progBarCount(int count) {
        progBar.setIndeterminate(false);
        progBar.setString(null);
        progBar.setMinimum(0);
        progBar.setMaximum(count);
        progBar.setValue(0);
        //Double bper = progBar.getPercentComplete();
        progBar.setString(progBar.getString());

    }

    public void setjButton1ActionListener(ActionListener e) {
        jButton1.addActionListener(e);

    }

    public void setjButton2ActionListener(ActionListener e) {
        jButton2.addActionListener(e);
        cancelButton.addActionListener(e);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JProgressBar progBar;
    private javax.swing.JLabel updateLabel;
    // End of variables declaration//GEN-END:variables
}
