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

class TimeSets extends JFrame implements ActionListener
{
	
	JPanel minPan, maxPan;
	JTextField minArea, maxArea;
	JLabel minLab, maxLab;

	JPanel updatePan, closePan, buttPan;
	JButton updateButt, closeButt;

	JPanel pan1, pan2;

	public TimeSets()
	{
		/* MIN time input */
		minLab = new JLabel("Set MINIMUM time");
		minArea = new JTextField(10);
		minPan = new JPanel();

		minPan.setLayout(new BoxLayout(minPan, BoxLayout.Y_AXIS));
		minPan.add(minLab);
		minPan.add(minArea);

		/* MAX time input */
		maxLab = new JLabel("Set MAXIMUM time");
		maxArea = new JTextField(10);
		maxPan = new JPanel();

		maxPan.setLayout(new BoxLayout(maxPan, BoxLayout.Y_AXIS));
		maxPan.add(maxLab);
		maxPan.add(maxArea);

		/* bringing all components together */
		pan1 = new JPanel();
		pan1.setLayout(new FlowLayout(FlowLayout.CENTER));
		pan1.add(minPan);
		pan1.add(maxPan);

		/* create the buttons */
		updateButt = new JButton ("Update");
		updatePan = new JPanel ();
		updatePan.add(updateButt);

		closeButt = new JButton ("Close");
		closePan = new JPanel ();
		closePan.add(closeButt);

		updateButt.addActionListener(this);
		closeButt.addActionListener(this);

		buttPan = new JPanel ();
		buttPan.setLayout(new BoxLayout(buttPan, BoxLayout.X_AXIS));
		buttPan.add(updatePan);
		buttPan.add(closePan);

		pan2 = new JPanel ();
		pan2.setLayout(new FlowLayout(FlowLayout.CENTER));
		pan2.add(buttPan);

		getContentPane().setLayout(new BoxLayout( getContentPane(), BoxLayout.Y_AXIS));
		getContentPane().add(pan1);
		getContentPane().add(pan2);
	}

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

		if (source==updateButt)
		{
			String min = minArea.getText();
			String max = maxArea.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");
		}
		
		if (source==closeButt)
		{
			this.setVisible(false);
			UserInterface.closeTimeWindow();
		}
	}


	public static void execute() 
	{
        TimeSets frame = new TimeSets();
        frame.setSize(400, 120);
		frame.setDefaultLookAndFeelDecorated(true);
		frame.setTitle("TimeSettings");
        frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//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;
	}

}





