package edu.calpoly.csc480.Corral.Main;

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

import com.bcurry.www.swing.*;
import com.bcurry.www.swing.dialog.*;

import edu.calpoly.csc480.Corral.Agent.*;
import edu.calpoly.csc480.Corral.Seer.*;

public class ToolsMenu extends JMenu {
	public ToolsMenu() {
		super("Tools");
		setMnemonic('t');
		getPopupMenu().setLightWeightPopupEnabled(false);
	}
	
	public void setOwner(BaseMain main) {
		this.main = main;
		this.seer = main.getSeer();

		selectAgentItem = new JMenuItem("Select Agent...", 'a');
		defineRulesItem = new JMenuItem("Define Rules...", 'r');
		optionsItem = new JMenuItem("Options...", 'o');
		
		selectAgentItem.addActionListener(new SelectAgentItemListener());
		defineRulesItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				seer.getRules().getView().setVisible(true);
			}
		});
		optionsItem.addActionListener(new OptionsItemListener());
 
		selectAgentItem.setEnabled(false);
		
		add(selectAgentItem);
		add(defineRulesItem);
		addSeparator();
		add(optionsItem);
	}
	
	public void fileOpened() {
		selectAgentItem.setEnabled(true);
	}

	public void fileClosed() {
		selectAgentItem.setEnabled(false);
	}

	protected BaseMain main;
	protected BaseSeer seer;
	
	protected Options options;

	protected JMenuItem defineRulesItem, selectAgentItem, optionsItem;

	protected class SelectAgentItemListener implements ActionListener {
		protected class SelectAgentDialog extends ModalConfirmDialog {
			public SelectAgentDialog(Frame owner) {
				super(owner, "Select Agent...");
			}
			
			protected JComboBox agentComboBox;
			
			protected JPanel makePanel() {
				GridBagConstraints curb;
				JPanel panel;
				
				curb = new GridBagConstraints();

				panel = new JPanel(new GridBagLayout());
				panel.setName("The Agent");
				
				agentComboBox = new JComboBox(Agents.getNames());
				agentComboBox.setEditable(false);
				
				curb.gridx = 0;
				curb.anchor = GridBagConstraints.WEST;
				curb.fill = GridBagConstraints.NONE;
				panel.add(new JLabel("Agent:  ", JLabel.LEFT), curb);
				
				curb.gridx = 1;
				curb.anchor = GridBagConstraints.WEST;
				curb.fill = GridBagConstraints.HORIZONTAL;
				panel.add(agentComboBox, curb);
				
				return panel;
			}
			
			protected void fillPanel() {
				if (seer.isAgentSelected()) {
					agentComboBox.setSelectedItem(agentName);
				}
			}
			
			protected void doOK() {
				agentName = (String)agentComboBox.getSelectedItem();
			}
		}

		public SelectAgentItemListener() {
			dialog = new SelectAgentDialog(main);
		}
			
		public void actionPerformed(ActionEvent event) {
			agentName = seer.getAgentName();
			dialog.setVisible(true);

			if (dialog.getResult() == dialog.OK_OPTION) {
				seer.agentSelected(agentName);
			}
		}
		
		protected String agentName;
		protected SelectAgentDialog dialog;
	}

	protected class OptionsItemListener implements ActionListener {
		protected class OptionsDialog extends ModalConfirmDialog {
			public OptionsDialog(Frame owner, String logFileName) {
				super(owner, "Options...");
				this.logFileName = logFileName;
			}
			
			protected JTextField logFileField;
			protected String logFileName;
			
			protected JPanel makePanel() {
				final int numLogFileFieldColumns = 32;

				GridBagConstraints curb;
				JPanel panel;
				
				curb = new GridBagConstraints();
				
				panel = new JPanel(new GridBagLayout());
				panel.setName("The Options");
				
				logFileField = new JTextField(
				 logFileName, numLogFileFieldColumns);
				
				curb.gridx = 0;
				curb.anchor = GridBagConstraints.WEST;
				curb.fill = GridBagConstraints.NONE;
				panel.add(new JLabel("Logfile:  "), curb);
				
				curb.gridx = 1;
				curb.anchor = GridBagConstraints.WEST;
				curb.fill = GridBagConstraints.HORIZONTAL;
				panel.add(logFileField, curb);
				
				return panel;
			}
			
			protected void doOK() {
				options.logFileName = logFileField.getText();
			}
		}
		
		public OptionsItemListener() {
			options = new Options();
		}
			
		public void actionPerformed(ActionEvent event) {
			options.logFileName = seer.getLogFileName();

			dialog = new OptionsDialog(main, options.logFileName);
			dialog.setVisible(true);

			if (dialog.getResult() == dialog.OK_OPTION) {
				try {
					seer.setLogFileName(options.logFileName);
				}
				catch (IOException err) {
					BasicOptionPane.showError("Log Error",
					 "Error writing " + options.logFileName + ": " +
					 err.getMessage());
				}
			}
		}
		
		protected OptionsDialog dialog;
	}
}