/*
 * Copyright (C) 1999-2006 <a href="mailto:adrian.paschke@gmx.de">Adrian Paschke</a>
 * 
 * The RBSLM Environment is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package rbsla.environment.view;

import java.awt.BorderLayout;
import java.util.Hashtable;

import javax.swing.JComponent;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;

public class GraphicTimeSeriesViewer implements Viewer
{

    final static String defaultRangeFrom = "0.0";

    final static String defaultRangeTo = "100.0";

    final static String defaultFixedAutoRange = "60000.0"; //60 second

    final static String defaultXName = "Time";

    final static String defaultYName = "Value";

    private static Logger logger = Logger
            .getLogger(GraphicTimeSeriesViewer.class);

    TimeSeriesPanel timeSeriesPanel;

    public Hashtable getViewerPropertieNames()
    {
        Hashtable timeSeriesViewerProperties;
        timeSeriesViewerProperties = new Hashtable();
        timeSeriesViewerProperties.put("Range From", defaultRangeFrom);
        timeSeriesViewerProperties.put("Range To", defaultRangeTo);
        timeSeriesViewerProperties.put("Fixed Auto Range",
                defaultFixedAutoRange);
        timeSeriesViewerProperties.put("X Name", defaultXName);
        timeSeriesViewerProperties.put("Y Name", defaultYName);

        return timeSeriesViewerProperties;

    }

    public JComponent getContentPane(Hashtable viewProperties)
    {
        timeSeriesPanel = new TimeSeriesPanel(viewProperties);
        return timeSeriesPanel;
    }

    public void dataChanged(String result, String debugText) throws Throwable
    {

        //default value
        double factor = 0.0;

        //convert result in double
        try
        {
            if (result.indexOf("true") < 0 && result.indexOf("false") < 0)
            {
                if (new Double(result) != null)
                    factor = new Double(result).doubleValue();
            }
        } catch (NumberFormatException ex)
        {
            logger.info("", ex);
        }

        this.timeSeriesPanel.addLastValue(factor);

    }
}

class TimeSeriesPanel extends JPanel
{

    /** The time series data. */
    private TimeSeries series;

    /** The most recent value added. */
    private double lastValue = 100.0;

    /** The most recent value added. */
    private double procent = 100.0;

    /** chat properties */
    private Hashtable chartProperties;

    /**
     * Creates a new instance.
     */
    public TimeSeriesPanel(Hashtable props)
    {
        super(new BorderLayout());
        this.chartProperties = props;
        this.series = new TimeSeries("", Millisecond.class);
        TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
        ChartPanel chartPanel = new ChartPanel(createChart(dataset, ""));
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

        add(chartPanel);
    }
    
    public void addLastValue(double factor)
    {
        // get value
        lastValue = procent * factor;

        // get time
        Millisecond now = new Millisecond();

        // create a dateset add(time, value)
        series
                .add(new Millisecond(), lastValue);
    }

    private JFreeChart createChart(XYDataset dataset, String title)
    {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(title,
                (String) chartProperties.get("X Name"),
                (String) chartProperties.get("Y Name"), dataset, true, true,
                false);
        
        XYPlot plot = chart.getXYPlot();

        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(new Double((String) chartProperties
                .get("Fixed Auto Range")).doubleValue()); // 60 seconds
        axis = plot.getRangeAxis();
        axis.setRange(new Double((String) chartProperties
                .get("Range From")).doubleValue(), new Double(
                (String) chartProperties.get("Range To")).doubleValue());
        return chart;
    }

}


