// file: TextTree.java
//

// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.tree.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.text.*;
import java.sql.*;


public class VStatistics extends JFrame implements ActionListener {
    
    VarmintDB database;
    
    String query_d;
    String field_d;
    
    PieChart pie_chart;
    
    int[] nums = null;
    String[] labs = null;
    int num_elem = 0;

    public VStatistics(Font font, String field, 
		       String query, VarmintDB dbase) {
	
	// make sure the query doesn't have an order in it
	//
	if (query.indexOf("order") != -1) {
	    
	    RuntimeException e = new RuntimeException("you cannot have order specified for a database query");
	    
	    JFrame frame = new JFrame("Warning");
	    JOptionPane.showMessageDialog(frame, 
					  "statistics: you cannot obtain statistics for a query with order specified");
	    
	    this.dispose();
	    return;
	}
	    
	// add an order so we can easily group the results
	//
	query = query + " order by bug_list."+field;
	
	// get our stats from the database
	//
	int n = computeResults(dbase.getStats(query), field);
	
	int dim = 300;
	int offset = 20;

	
	JLabel lab = new JLabel("Query returns "+n+" result(s), distributed by "+field);
	lab.setAlignmentX(Component.CENTER_ALIGNMENT);
	lab.setFont(font);
	
	pie_chart = new PieChart(offset, dim+offset, dim, dim, 
				 num_elem, nums, labs);
	
	JPanel pane = new JPanel();
	
	pane.add(Box.createRigidArea(new Dimension(0, 10)));
	pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
	
	pane.add(Box.createRigidArea(new Dimension(0, 10)));
	pane.add(lab);
	pane.add(pie_chart);
	
	pane.add(Box.createRigidArea(new Dimension(0, 10)));
	JButton dismiss = new JButton("Dismiss");
	dismiss.setAlignmentX(Component.CENTER_ALIGNMENT);
	dismiss.addActionListener(this);
	
	pane.add(dismiss);
	pane.add(Box.createRigidArea(new Dimension(0, 10)));
	getContentPane().add(pane);

	pack();
	//	setSize(600, 400);
	
	//	setVisible(true);
    }

    private int computeResults(ResultSet result, String field) {

	nums = new int[200];
	labs = new String[200];

	int n = 0;

	String last_val = "___VARMINTDB_DUMMY_STATISTICS_LABEL__";
	int c = -1;
	try {
	    
	    while (result.next()) {
		
		
		
		String val = result.getString(field);
		
		// in case there are bogus entries
		//
		if (val == null)
		    continue;

		// keep the total count
		//
		n++;

		// if this is the same value, just increment count
		//
		if (val.equals(last_val)) {
		    nums[c] ++;
		} 
		
		// new value, start a new entry
		//
		else {

		    if (++c >= 200) {
			throw (new RuntimeException("too many values"));
		    }
		    
		    labs[c] = val;
		    nums[c] = 1;
		    last_val = val;
		}
	    }
	} catch (SQLException e) {

	    // todo
	    //
	    JFrame frame = new JFrame("Warning");
	    JOptionPane.showMessageDialog(frame, 
					  "computeResults: Database error");
	    e.printStackTrace();
	}
	
	num_elem = c+1;
	return n;
    }
    
    public void actionPerformed(ActionEvent e) {
	this.dispose();
    }
}	
