import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * All the setting of various parameters associated with the
 * operation of the database system.
 * 
 * Written by Peter McBrien, using code of Jaymin Patels TimeSet as a 
 * basis 
 */


class ControlSettings extends JFrame implements ActionListener {
  Container window; 
  JTextField minArea,maxArea,urlArea,driverArea,usernameArea,passwordArea;

  JButton updateButt, closeButt;

  public ControlSettings() {
    setSize(600,300);
    setDefaultLookAndFeelDecorated(true);
    setTitle("Settings");
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    window=getContentPane();
    window.setLayout(new GridBagLayout());
    int line=0;

    // Database JDBC connection details
    constrain(new JLabel("Database"),0,line++,1,1);
    constrain(new JLabel("URL"),0,line,1,1);
    urlArea=new JTextField(30);
    urlArea.setText(Query.url);
    constrain(urlArea,1,line++,5,1);
    constrain(new JLabel("Driver"),0,line,1,1);
    driverArea=new JTextField(30);
    driverArea.setText(Query.driver);
    constrain(driverArea,1,line++,5,1);
    constrain(new JLabel("Username"),0,line,1,1);
    usernameArea=new JTextField(10);
    usernameArea.setText(Query.username);
    constrain(usernameArea,1,line++,1,1);
    constrain(new JLabel("Password"),0,line,1,1);
    passwordArea=new JPasswordField(10);
    passwordArea.setText(Query.password);
    constrain(passwordArea,1,line++,1,1);

    // Time settings 
    constrain(new JLabel("Time range"),0,line++,1,1);

    /* MIN time input */
    minArea = new JTextField(10);
    minArea.setText(new Integer(UserInterface.getMinTime()).toString());
    constrain(new JLabel("Minimum Time"),0,line,1,1);
    constrain(minArea,1,line++,1,1);

    /* MAX time input */
    maxArea = new JTextField(10);
    maxArea.setText(new Integer(UserInterface.getMaxTime()).toString());
    constrain(new JLabel("Maximum Time"),0,line,1,1);
    constrain(maxArea,1,line++,1,1);

    /* create the buttons */
    updateButt = new JButton ("Update");
    closeButt = new JButton ("Close");
    updateButt.addActionListener(this);
    closeButt.addActionListener(this);

    // The button line
    constrain(new JLabel(" "),4,line,1,1);
    constrain(updateButt,5,line,1,1,GridBagConstraints.NONE,
	      GridBagConstraints.SOUTHEAST,0.0,0.0,0,0,0,0);

    pack();
  }

  public void actionPerformed (ActionEvent evt) {
    Object source = evt.getSource();

    if (source==updateButt) {
      String min = minArea.getText();
      String max = maxArea.getText();
      Query.url=urlArea.getText();
      Query.driver=driverArea.getText();
      Query.password=passwordArea.getText();
      Query.username=usernameArea.getText();

      boolean ck1 = checkIntVal(min, "Minimum");
      boolean ck2 = checkIntVal(max, "Maximum");
      //System.out.println("ck1="+ck1+" -- ck2="+ck2);
      if ( ck1==true && ck2==true) {
	int minTime = Integer.parseInt(min);
	int maxTime = Integer.parseInt(max);
	/* set the new times */
	UserInterface.setMinTime(minTime);
	UserInterface.setMaxTime(maxTime);
	System.out.println("System time successfully updated");
      }
      else
	System.out.println("ERROR: Unable to update the system time values");

      return;
    }
		
    if (source==closeButt) {
      this.setVisible(false);
      UserInterface.closeTimeWindow();
      return;
    }

    System.out.println("Unknown event in ControlSettings:"+evt);
  }


	public static void execute() 
	{
        ControlSettings frame = new ControlSettings();
		//WindowQuitter wquit = new WindowQuitter(); /* construct a listener for the frame */
		//frame.addWindowListener(wquit); /* register the listener */
    }

	private static boolean checkIntVal(String time, String type)
	{
		boolean res=false;

		if (time.equals(""))
		{
			System.out.println("ERROR: A time value has not be entered for "+type+" time");
		}
		else
		{
			/* check that user has entered a integer value: the correct type */
			try
			{
				 int checkInt = Integer.parseInt(time);
				 res=true;
			}
			catch (NumberFormatException e)
			{
				System.out.println("ERROR: The data value '" + time + "' entered for "+type+" time is not a time value of Integer type");
			}
		}

		return res;
	}

  private void constrain(Component component,
			 int grid_x,int grid_y,int grid_width,int grid_height,
			 int fill,int anchor,double weight_x,double weight_y,
			 int top,int left,int bottom,int right) {
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = grid_x; c.gridy = grid_y;
    c.gridwidth = grid_width; c.gridheight = grid_height;
    c.fill = fill; c.anchor = anchor;
    c.weightx = weight_x; c.weighty = weight_y;
    if (top + bottom + left + right > 0)
      c.insets = new Insets(top, left, bottom, right);
    ((GridBagLayout)window.getLayout()).setConstraints(component, c);
    window.add(component);
  }
 
  private void constrain(Component component,
			 int grid_x,int grid_y,int grid_width,int grid_height,
			 int fill,int anchor,double weight_x,double weight_y) {
    constrain(component, grid_x, grid_y, grid_width, grid_height,
              fill, anchor, weight_x, weight_y, 0, 0, 0, 0);
  }
  
  private void constrain(Component component,
			 int grid_x,int grid_y,int grid_width,int grid_height){
    constrain(component,grid_x,grid_y,grid_width,grid_height,
              GridBagConstraints.NONE,GridBagConstraints.NORTHWEST, 
	      0.0,0.0,0,0,0,0);
  }
}





