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.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;
	
	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()));
		}
		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();
			}
		}
	}

}
