/*
 * 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.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

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.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import rbsla.environment.ECAConstraints;
import rbsla.environment.view.SpringUtilities;

public class StatsActual extends JFrame implements ActionListener
{
	private final int srvRow = 3;
	private Color[] colors;
	private Vector checked;
	private Connection con;

	//center of the Panel
	private ChartPanel chartPanel;
	private JFreeChart chart;
	private Hashtable chartProperties;
	private TimeSeries series1;
	private TimeSeries series2;
	private TimeSeries series3;
	private TimeSeries series4;
	private TimeSeries series5;
	private TimeSeriesCollection dataset;
	
	//south
	private JPanel southPanel;
	private JPanel cSouthPanel;
	private JPanel sSouthPanel;
	
//	buttons
	private JButton close;
	private JButton refresh;
	
	public StatsActual()
    {
    	super("Acutal Values");
		this.getContentPane().setLayout(new BorderLayout());
		
		try
		{
			
			//db conn
			Class.forName(ECAConstraints._DB_DRIVER);
			con = DriverManager.getConnection(ECAConstraints._DB_NAME, ECAConstraints._DB_USER, ECAConstraints._DB_PWD);
			
			checked = new Vector();
			
			//init colors
			initColors();
			
			southPanel = new JPanel(new BorderLayout());
			
			
			//configuring center panel
			
			setChartProperties();
			
			dataset = new TimeSeriesCollection();
			series1 = new TimeSeries("", Millisecond.class);
			series2 = new TimeSeries("", Millisecond.class);
			series3 = new TimeSeries("", Millisecond.class);
			series4 = new TimeSeries("", Millisecond.class);
			series5 = new TimeSeries("", Millisecond.class);
			
			dataset.addSeries(series1);
			dataset.addSeries(series2);
			dataset.addSeries(series3);
			dataset.addSeries(series4);
			dataset.addSeries(series5);
			
			chart = ChartFactory.createTimeSeriesChart(null,
	                (String) chartProperties.get("X"),
	                (String) chartProperties.get("Y"), dataset, true, true, false);
			 XYPlot plot = chart.getXYPlot();

			 		 
			 ValueAxis axis = plot.getDomainAxis();
			 axis.setAutoRange(true);
			 axis.setFixedAutoRange(new Double((String) chartProperties.get("FixedAutoRange")).doubleValue());
			 axis = plot.getRangeAxis();
			 axis.setRange(new Double((String) chartProperties.get("From")).doubleValue(), new Double((String) chartProperties.get("To")).doubleValue());
			
			 
			 
			 chartPanel = new ChartPanel(chart);
			
			 //configuring southpanel
			 sSouthPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			 close = new JButton(ECAConstraints._EXIT);
			 //initialise buttons and add them to panel
			 refresh = new JButton(ECAConstraints._REFRESH);
			 close.addActionListener(this);
			 refresh.addActionListener(this);
			 sSouthPanel.add(refresh);
			 sSouthPanel.add(close);
			 //server list
			 cSouthPanel = new JPanel(new SpringLayout());
			 cSouthPanel.setBackground(Color.LIGHT_GRAY);
			 //get the host list of db
			 String getServers = "SELECT DISTINCT host FROM availability";
			 Statement st = con.createStatement();
			 ResultSet rs = st.executeQuery(getServers);
			 rs.last();
			 int records = rs.getRow();
			 rs.beforeFirst();
			 while(rs.next())
			 {
				 //cSouthPanel.add(new JLabel(rs.getString("host")));
				 JCheckBox check = new JCheckBox(rs.getString("host"));
				 cSouthPanel.add(check);
				 check.setBackground(Color.LIGHT_GRAY);
				 check.addActionListener(this);
			 }
			 
			 int tmp = srvRow - (records % srvRow);
			 for(int i=0;i<tmp;i++)
			 {
				 records++;
				 cSouthPanel.add(new JLabel(""));
			 }
			 
			 SpringUtilities.makeCompactGrid(cSouthPanel,records/srvRow,srvRow,8,8,8,8);
			 
			 
			 southPanel.add(cSouthPanel, BorderLayout.CENTER);
			 southPanel.add(sSouthPanel, BorderLayout.SOUTH);
			 
			 this.add(southPanel, BorderLayout.SOUTH);
			 this.add(chartPanel, BorderLayout.CENTER);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		
		this.setSize(new Dimension(600,300));
		this.setResizable(false);
		this.setVisible(true);
    }
	
	public JFreeChart getChart()
	{
		return chart;
	}
	
	public TimeSeriesCollection getDataset()
	{
		return dataset;
	}
	
	public Vector getChecked()
	{
		return checked;
	}
	
	private void setChartProperties()
	{
		chartProperties = new Hashtable();
		chartProperties.put("X", "Time");
		chartProperties.put("Y", "Avalability");
		chartProperties.put("From", "0.0");
		chartProperties.put("To", "100.0");
		chartProperties.put("FixedAutoRange", "60000.0");
	}
	
	private void initColors()
	{
		colors = new Color[5];
		colors[4] = Color.MAGENTA;
		colors[2] = Color.GREEN;
		colors[1] = Color.BLUE;
		colors[3] = Color.YELLOW;
		colors[0] = Color.RED;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		Object src = e.getSource();
		if(cmd.equals(ECAConstraints._EXIT))
		{
			this.dispose();
		}
		else if(cmd.equals(ECAConstraints._REFRESH))
		{
			
		}
		else if(src instanceof JCheckBox)
		{
			int selected = checked.size();
			JCheckBox source = (JCheckBox)src;
			System.out.println(selected);
			if(checked.contains(cmd))
			{
				checked.remove(cmd);
				source.setBackground(Color.LIGHT_GRAY);
			}
			else if(selected <= 4)
			{
				checked.add(cmd);
				selected++;
				source.setBackground(colors[selected-1]);
			}
			else if(selected > 4)
			{
				source.setSelected(false);
			}
		}
	}

      
}