package ifs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class IterationCountDialog extends JDialog implements ActionListener {
	
	private IFSCanvas owner;
	private JTextField input1, input2;
	
	public IterationCountDialog(IFSCanvas owner) {
		super(getFrameParent(owner), "Set Iteration Counts", true);
		this.owner = owner;
		input1 = new JTextField("" + owner.getIterationCount(), 5);
		input2 = new JTextField("" + owner.getIterationsForPreview(), 5);
		JButton okButton = new JButton(" OK ");
		JButton cancelButton = new JButton("Cancel");
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		JPanel content = new JPanel();
		setContentPane(content);
		content.setLayout(new BorderLayout(10,10));
		JLabel info = new JLabel("<html>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set the number of iterations that are applied<br>" +
				"to a point to determine its location in the image.<br>" +
				"Each iteration constists of applying a randomly selected<br>" +
				"map to the point.  Increasing the interation count might<br>" +
				"give a more accurate image, but it will take longer to<br>" +
				"compute.  There are separate iteration counts for the actual<br>" +
				"image and for the magenta-colored preview that is shown<br>" +
				"when one of the maps is selected.  Minimum value is 10.");
		content.add(info, BorderLayout.NORTH);
		JPanel bottom = new JPanel();
		bottom.setLayout(new GridLayout(0,1,5,5));
		JPanel p = new JPanel();
		p.setAlignmentY(0);
		p.add(new JLabel("Iteration count for the image  =  "));
		p.add(input1);
		bottom.add(p);
		p = new JPanel();
		p.setAlignmentY(0);
		p.add(new JLabel("Iteration count for the preview = "));
		p.add(input2);
		bottom.add(p);
		p = new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.RIGHT));
		p.setAlignmentY(0);
		p.add(cancelButton);
		p.add(okButton);
		bottom.add(p);
		content.add(bottom,BorderLayout.CENTER);
		content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		pack();
		Frame f = getFrameParent(owner);
		if (f != null) {
			setLocation(f.getX() + 20, f.getY() + 50);
		}
	}

	private static Frame getFrameParent(JComponent comp) {
		Container c =  comp.getParent();
		while (c != null && !(c instanceof Frame))
			c = c.getParent();
		return c == null? null : (Frame)c;
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getActionCommand().equals("Cancel"))
			dispose();
		else if (evt.getActionCommand().trim().equals("OK")) {
			String text1 = input1.getText();
			String text2 = input2.getText();
			try {
				int v1, v2;
				try {
					v1 = Integer.parseInt(text1);
				}
				catch (NumberFormatException e) {
					throw new Exception("The value entered for the number of iterations\nin the image must be an integer.");
				}
				try {
					v2 = Integer.parseInt(text2);
				}
				catch (NumberFormatException e) {
					throw new Exception("The value entered for the number of iterations\nin the preview must be an integer.");
				}
				if (v1 < 10)
					throw new Exception("The value entered for the number of iterations\nin the image must be 10 or greater.");
				if (v2 < 10)
					throw new Exception("The value entered for the number of iterations\nin the preview must be 10 or greater.");
				dispose();
				owner.setIterationCount(v1);
				owner.setIterationsForPreview(v2);
			}
			catch (Exception e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "Input Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}

