// import java libraries
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class VarmintQuery implements ActionListener{

    JFrame frame = null;

    VarmintDB database = null;

    String userName = null;

    JTextField titleBox;
    JEditorPane queryEntry;
    JScrollPane mountingPane;
    JComboBox sharingBox;

    VarmintPanel varmintPanel;

    public VarmintQuery(VarmintDB db, String name) {

	database = db;

	userName = name;
    }

    public void SaveQuery(String base_query, VarmintPanel varmintPanel_) {
	
	varmintPanel = varmintPanel_;

	frame = new JFrame();

	JPanel savePanel = createSavePanel(base_query);

	frame.getContentPane().add(savePanel);

	// listen for window closing events
	//
	frame.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {frame.dispose();}
	    });

	// resize and set the frame dimensions
	//
	frame.pack();

	// center the frame to the screen
	//
	Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); 
	Rectangle frameDim = frame.getBounds(); 
	frame.setLocation((screenDim.width - frameDim.width) / 3,
			      (screenDim.height - frameDim.height) / 3); 
	
	// display the frame
	//
	frame.setVisible(true);
    }

    public void LoadQuery() {}

    
    private JPanel createSavePanel(String base_query) {
	
	JPanel savePanel = new JPanel();
	savePanel.setPreferredSize(new Dimension(400,300));

	GridBagLayout gridbag = new GridBagLayout();
	savePanel.setLayout(gridbag);
	
	GridBagConstraints c = new GridBagConstraints();
	
	// create and place the title label
	//
	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.weightx = 0;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.NONE;
	c.insets = new Insets(30,30,5,5);
	    
	
	JLabel title = new JLabel("Title:");
	gridbag.setConstraints(title, c);
	savePanel.add(title);

	// create and place the Query lable
	//
	c.gridy = 1;
	c.insets = new Insets(5,30,5,5);

	JLabel query = new JLabel("Query:");
	gridbag.setConstraints(query, c);
	savePanel.add(query);

	// create and place the sharing label
	//
	c.gridy = 2;
	c.insets = new Insets(5,30,5,5);

	JLabel sharing = new JLabel("Sharing:");
	gridbag.setConstraints(sharing, c);
	savePanel.add(sharing);

	// create and place the title text field
	//
	c.gridx = 1;
	c.gridy = 0;
	c.gridwidth = GridBagConstraints.REMAINDER;
	c.gridheight = 1;
	c.weightx = 10;
	c.weighty = 0;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(30,5,5,30);

	titleBox = new JTextField("");
	gridbag.setConstraints(titleBox, c);
	savePanel.add(titleBox);

	// create and place the query text area
	//
	c.gridx = 1;
	c.gridy = 1;
	c.gridwidth = 3;
	c.gridheight = 1;
	c.weightx = 10;
	c.weighty = 10;
	c.anchor = GridBagConstraints.NORTHWEST;
	c.fill = GridBagConstraints.BOTH;
	c.insets = new Insets(5,5,5,30);

	queryEntry = new JEditorPane("text/plain",base_query);
	mountingPane = new JScrollPane(queryEntry);
	gridbag.setConstraints(mountingPane, c);
	savePanel.add(mountingPane);

	// create and place the sharing combo box
	//
	c.gridx = 1;
	c.gridy = 2;
	c.gridwidth = 2;
	c.gridheight = 1;
	c.weightx = 10;
	c.weighty = 0;
	c.anchor = GridBagConstraints.CENTER;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(5,5,5,30);

	sharingBox = new JComboBox(getShareGroups());
	gridbag.setConstraints(sharingBox, c);
	savePanel.add(sharingBox);
	

	// create and place the save and cancel buttons
	//
	c.gridx = 1;
	c.gridy = 3;
	c.gridwidth = 1;
	c.gridheight = 1;
	c.weightx = 10;
	c.weighty = 0;
	c.anchor = GridBagConstraints.CENTER;
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets = new Insets(5,30,30,5);

	JButton saveButton = new JButton("Save");
	saveButton.addActionListener(this);

	gridbag.setConstraints(saveButton, c);
	savePanel.add(saveButton);
	
	c.gridx = 2;
	c.insets = new Insets(5,5,30,30);
	JButton cancelButton = new JButton("Cancel");
	cancelButton.addActionListener(this);
    

	gridbag.setConstraints(cancelButton, c);
	savePanel.add(cancelButton);

	

	return savePanel;

    }

    public void actionPerformed(ActionEvent e) {

	if (e.getActionCommand().equals("Save")) {
	    
	    String desc = titleBox.getText();
	    String query = queryEntry.getText();
	    
	    String share = (String)(sharingBox.getSelectedItem());
	    String owner = new String("");

	    BugQuery.setQueryString(desc);

	    if (share.equals("varmint")) {
		share = "all";
		owner = "VARMINT";
	    }
	    else
		owner = userName;

	    database.saveQuery(query, desc, owner, share);
	    frame.dispose();

	    varmintPanel.searchMenu.updateMenus();
	}

	else if (e.getActionCommand().equals("Cancel")) {
	    
	    frame.dispose();
	}
	
	
    }

    private Vector getShareGroups() {

	// get all of the groups for sharing from the database and put
	// them into a Vector
	//
	Vector  shareGroups = database.getGroups();

	Vector groups = new Vector(5,5);
	groups.add("public");
	
	
	for (int i =0 ; i < shareGroups.size(); i++) {

	    // create the vector to hold each of the enum values
	    //
	    Vector groupUsers = (Vector)shareGroups.get(i);
	    
	    		
	    String members = (String)groupUsers.get(1);
	    System.out.println(members);
	    // tokenize the string containing the values with comma as the
	    // delimiter
	    //
	    StringTokenizer tokenizer = new StringTokenizer(members, ",");
	    
	    // run through and add each token to the shareGroups vector
	    //
	    while (tokenizer.hasMoreTokens()) {
		
		String line = (String)tokenizer.nextToken();
		line = line.trim();
		
		if (line.equals(userName)) {
		    
		    String group = (String)groupUsers.get(0); 
    
		    groups.add(group);
		    if (group.equals("admin")) {
			groups.add("varmint");
		    }
		}
	    }
	}
	groups.add("private");
	return groups;
    }
}










