/**
 *
 * Process Editor - Simulation Reporting Package
 *
 * (C) 2008,2009 Frank Puhlmann
 *
 * http://frapu.net
 *
 */
package net.frapu.code.simulation.petrinets.reporting;

import java.awt.Color;
import java.util.LinkedList;
import java.util.StringTokenizer;
import net.frapu.code.simulation.petrinets.PetriNetSimulation;
import net.frapu.code.simulation.petrinets.PetriNetSimulationListener;
import net.frapu.code.visualization.ProcessEditor;
import net.frapu.code.visualization.reporting.BarChart;
import net.frapu.code.visualization.reporting.ReportingModel;

/**
 *
 * @author fpu
 */
public class FlowRateReporting extends javax.swing.JPanel implements PetriNetSimulationListener {

	private static final long serialVersionUID = 4632174431369381663L;
	private ProcessEditor editor;
    private ReportingModel model;
    private PetriNetSimulation sim;
    private BarChart chart1;

    /** Creates new form CostReporting */
    public FlowRateReporting(PetriNetSimulation sim) {
        // Initialize components
        initComponents();
        initCustomComponents();
        // Register listener
        this.sim = sim;
        sim.addListener(this);
    }

    public void initCustomComponents() {
        // Initialize model
        model = new ReportingModel("Flow Rate");

        chart1 = new BarChart();
        chart1.setSize(400, 300);
        chart1.setPos(230, 180);
        chart1.setProperty(BarChart.PROP_BARCOLOR, "" + (new Color(64, 64, 192).getRGB()));


        chart1.setText("Flow Rate");
        chart1.setXLabel("Time (hours)");
        chart1.setYLabel("Count (items)");
        chart1.setProperty(BarChart.PROP_VALUES, "");
        chart1.setProperty(BarChart.PROP_LABELS, "");

        model.addNode(chart1);

        // Create editor and add to ViewportView
        editor = new ProcessEditor(model);
        jScrollPane1.setViewportView(editor);
    }

    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();

        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()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void refreshDisplay() {
        // ignore
    }

    public void simulationFinished() {
        // Update values here
        LinkedList<Integer> data = new LinkedList<Integer>();
        // Add data for all instances
        StringTokenizer st =
                new StringTokenizer(sim.getSimulationResult(PetriNetSimulation.SIM_FLOW_RATE), ",");
        while (st.hasMoreTokens()) {
            try {
                data.add((int) Double.parseDouble(st.nextToken()));
            } catch (Exception e) {
                chart1.setErrorMessage("Error retrieving results");
            }

        }
        chart1.setData(data);
        repaint();
    }

    public void simulationStarted() {
        // Reset values
        chart1.setData(null);
        repaint();
    }
}
