/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.general.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.application.preferences.PreferencesManager;
import com.approximatrix.draco.general.ui.preferences.EditorPreferencesPanel;
import com.approximatrix.draco.general.ui.preferences.PluginPreferencesPanel;
import com.approximatrix.draco.general.ui.preferences.PreferencesPanel;
import com.approximatrix.draco.general.ui.preferences.UIPreferencesPanel;
import com.approximatrix.draco.main.ui.DataDialog;
import com.approximatrix.draco.main.ui.MainWindow;

public class PreferencesDialog extends ChildFrame implements ActionListener {
	
	/** The title string */
	private static final String TITLE = "Preferences";
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "preferences";
	
	private JPanel buttonPanel = null;
	private JButton acceptButton = null;
	private JButton cancelButton = null;
	private JTabbedPane tabs = null;
	
	private PreferencesManager manager = null;
	
	/** The user interface preferences panel */
	private UIPreferencesPanel uiprefs = null;
	
	/** The plugin preferences panel */
	private PluginPreferencesPanel pluginprefs = null;
	
	/** The editor preferences panel */
	private EditorPreferencesPanel editorprefs = null;
	
	public PreferencesDialog() {
		super(TITLE,
		      true,
		      true,
		      true,
		      true);
		
		manager = new PreferencesManager();
		
		initialize();
	}

	private void initialize() {
		this.setSize(400,300);
		
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(getButtonPanel(),BorderLayout.SOUTH);
		this.getContentPane().add(getTabPane(),BorderLayout.CENTER);
		
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	}
	
	private JPanel getButtonPanel() {
		if(buttonPanel == null) {
			buttonPanel = new JPanel();
			FlowLayout layout = new FlowLayout();
			layout.setAlignment(FlowLayout.RIGHT);
			buttonPanel.setLayout(layout);
			
			acceptButton = new JButton("Ok");
			cancelButton = new JButton("Cancel");
			acceptButton.addActionListener(this);
			cancelButton.addActionListener(this);
			buttonPanel.add(acceptButton);
			buttonPanel.add(cancelButton);
		}
		return buttonPanel;
	}
	
	private JTabbedPane getTabPane() {
		if(tabs == null) {
			tabs = new JTabbedPane();
			
			tabs.add(UIPreferencesPanel.TAB_TITLE, new UIPreferencesPanel(manager.getUIPrefs()));
			tabs.add(PluginPreferencesPanel.TAB_TITLE, new PluginPreferencesPanel(manager.getPluginPrefs()));
			tabs.add(EditorPreferencesPanel.TAB_TITLE, new EditorPreferencesPanel(manager.getEditorPrefs()));
		}
		return tabs;
	}
	
	@Override
	public String getHelpKey() {
		Component topMost = tabs.getSelectedComponent();
		if(topMost instanceof PreferencesPanel)
			return ((PreferencesPanel)topMost).getHelpKey();
		else
			return HELP_KEY;
	}

	@Override
	public JMenu[] getMenuItems() {
		return null;
	}

	@Override
	public Component[] getToolbarButtons() {
		return null;
	}

	@Override
	public boolean isRenameable() {
		return false;
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == acceptButton) {
			manager.apply();
			manager.save();
			
			performGuiUpdates();
		}
		dispose();
	}
	
	private void performGuiUpdates() {
		Object[] values = this.getMainWindow().getWindowsByClass(DataDialog.class);
		if(values.length > 0) {
			try {
				final DataDialog dd = ((DataDialog)values[0]);
				final MainWindow mw = this.getMainWindow();
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						try {
							dd.setSelected(true);
							mw.getDesktop().getDesktopManager().activateFrame(dd);
						} catch (Exception e) {
							
						}
					}
				});
			} catch(ClassCastException cce) {
				cce.printStackTrace();
			}
		}
	}

}
