package voi.ui;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.sql.*;
import voi.db.*;

/**
This function of this class is allow the user to enter his own SQL query. 
It is invoked when a user makes a switch from a Form mode to a SQL query mode

It performs the exact same way as the Form mode does, differing only in the fact
the user has to enter his own query, if he wants complex queries to be used 
*/
class SqlQueryInterface extends JPanel implements ActionListener
{
	private TdfLabel select, from, where, table, dummy;
	private JTextField attributesField, constraintsField;
	private TdfButton submitButton, resetButton, queryMode, switchButton;
	private JPanel sqlPane, switchPane;
	
	private GuiController theApp;
	private int panelHeightStep, xwidth, ywidth;
	
	final Font TDF_FONT = new Font("Garamond", Font.PLAIN, 12);
	final int PANEL_HEIGHT_STEP = 21;
	private Border edge = BorderFactory.createRaisedBevelBorder ();	// Button Border
	
	private String attrList, constraints;
	private MysqlConnect mysqlConnect;
	/** constructor that takes in theApp (GuiController class), width of the
	main window and height of the main screen window
	*/
	public SqlQueryInterface (GuiController theApp, int xwidth, int ywidth)
	{			
		this.theApp = theApp;
		this.xwidth = xwidth; 					// set the maximum width for the objects
		this.ywidth = ywidth;
	
		setPanelProperties ();
		constructQueryInterface ();		
	}
	
	/** 
	This class sets the panel properties */
	public void setPanelProperties ()
	{
		panelHeightStep = (int) (ywidth/PANEL_HEIGHT_STEP);
		setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));       		
	}			
	
	/**
	This class constructs the Query Interface by calling 2 member functions
	*/
	public void constructQueryInterface ()
	{
		constructSqlInterface ();
		add (new FillerPane (1,xwidth, panelHeightStep));
		constructSwitchPane ();
	}
	
	/** 
	This class creats the SELECT, FROM, WHERE clauses and adds the panel to the SQLInterface
	*/
	public  void constructSqlInterface ()
	{
		select = new TdfLabel ("SELECT", TDF_FONT);
		from = new TdfLabel ("FROM", TDF_FONT);
		table = new TdfLabel ("2qz_test", TDF_FONT);
		where = new TdfLabel ("WHERE", TDF_FONT);
		
		attributesField = new JTextField ();
		constraintsField = new JTextField ();
		
		submitButton = new TdfButton ("Submit", "Submit the selected query ", edge);
		resetButton = new TdfButton ("Reset", "Reset the query ", edge);
		
		sqlPane = new JPanel ();
		sqlPane.setBorder (new TitledBorder (new EtchedBorder(), "Enter the SQL Query"));
		sqlPane.setLayout (new GridLayout (5,2,10,10));
		sqlPane.setMaximumSize(new Dimension(xwidth-11,6*panelHeightStep));
		
		sqlPane.add (select);
		sqlPane.add (attributesField);
		sqlPane.add (from);
		sqlPane.add (table);
		sqlPane.add (where);
		sqlPane.add (constraintsField);
		sqlPane.add (new JLabel (""));
		sqlPane.add (new JLabel (""));
		sqlPane.add (submitButton);
		submitButton.addActionListener (this);
		sqlPane.add (resetButton);
		resetButton.addActionListener (this);
		
		add (sqlPane);
	}
	
	public void constructSwitchPane ()
	{
		
		switchButton = new TdfButton ("switch to Form mode ","Switch from the SQL Query Mode to the Form Mode ", edge);
		switchButton.addActionListener (this);
		switchPane = new JPanel ();
		switchPane.setLayout (new GridLayout (2,1,10,10));
		switchPane.setMaximumSize(new Dimension(xwidth-11,3*panelHeightStep));
		
		switchPane.add (switchButton);
		add (switchPane);
	}

	public void getSqlParameters ()
	{
		attrList = new String ();
		constraints = new String ();
		attrList = attributesField.getText ();
		constraints = constraintsField.getText ();		
	}
	
	public void constructQuery ()
	{
		getSqlParameters ();
		mysqlConnect = new MysqlConnect ();
                theApp.showResults (mysqlConnect.submitQuery (attrList, constraints));
	}
	
	public void clearAll ()
	{
		attributesField.setText ("");
		constraintsField.setText ("");
	}
	
	public ResultSet getResults ()
	{
		return mysqlConnect.getResults ();
	}	
	
	public void actionPerformed (ActionEvent ae)
	{
		Object source = ae.getSource ();
		if (source == submitButton)
		{
			theApp.removeComponents ();
			constructQuery ();
			theApp.getToolBar ().enableAll (true);
			
		}
		
		if (source == resetButton)
		{
			clearAll ();
			theApp.getToolBar ().enableAll (false);
		}
		if (source == switchButton)
		{
			theApp.switchToFormMode ();
		}

	}
}
