/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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.application;

import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.Icon;

import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.main.ui.MainWindow;
import com.approximatrix.draco.general.ui.SplashFrame;
import com.approximatrix.draco.application.icons.IconLibrary;
import com.approximatrix.draco.application.icons.VectorIcon;

import java.util.ArrayList;

import net.sourceforge.helpgui.gui.MainFrame;
import gnu.getopt.Getopt;

/*
 * Created on Apr 4, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author armstrongj
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ApplicationManager {

	private static ArrayList<MainWindow> main_window_list = null;
	private static boolean started_from_main = false;
	
	/** The name of the Application */
	public static final String APP_NAME = "Draco";
	
	/** The current version information */
	public static final String APP_VERSION = "Release 1.0 Candidate 2";
	
	/** Attempt to use system L&F */
	private static final boolean USE_NATIVE_LF = true;
	
	/** Enable the background image */
	public static final boolean ENABLE_BKGD = true;
	
	/** The icon to be displayed by default for the help window */
	private static final String PATH_TO_HELP_WINDOW_ICON = "/com/approximatrix/draco/application/images/help.png";
	
	/** The key for the directory containing help */
	private static String helpDirectory="doc";
	
	// The mainframe for help
	private static MainFrame HelpWindow = null;
	private static final String HELPGUI_STYLE = "crystal";
	private static final String HELP_TITLE = APP_NAME+" Help";
	
	// Splash handling
	private static SplashFrame Splash = null;
	private static final int MS_TO_SPLASH_CLOSE = 3000;
	//private static final boolean SHOW_SPLASH = false; 

	// Icon library
	private static IconLibrary icons = null;
	
	public static void main(String[] args) {
		if(USE_NATIVE_LF) {
			try {
				UIManager.setLookAndFeel(
						UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e) { 
				System.out.println("Change of LNF failed...");
			}
		}

        ProcessCommandLine(args);
	
   	    // Show the splash screen if necessary
		StartApplication();
		//StartWithSplash();
   	    //if(SHOW_SPLASH) 
   	    //	StartWithSplash();
   	    //else
   	    //	StartApplication();
	}
	
	private static void StartWithSplash() {
		Splash = new SplashFrame();
   	    Splash.setLocationRelativeTo(null);
   	    Splash.setVisible(true);
   	    Splash.startup();
	}
	
	
	
	public static void RequestNewWindow() {
		main_window_list.add(new MainWindow());
	}
	
	public static void RequestNewWindow(double[][] data,
			                            String[] column_names) {
		main_window_list.add(new MainWindow(data,column_names));
	}
	
	public static void RequestNewWindow(StatTableModel data) {
		main_window_list.add(new MainWindow(data));
	}
	
	public static MainWindow RequestAndReturnNewWindow() {
		MainWindow new_window = new MainWindow();
		main_window_list.add(new_window);
		return new_window;
	}
	
	public static MainWindow RequestAndReturnNewWindow(StatTableModel data) {
		MainWindow new_window = new MainWindow(data);
		main_window_list.add(new_window);
		return new_window;
	}
	
	public static void CloseWindow(Object this_window) {
		for(int i=0;i<main_window_list.size();i++) {
			if(main_window_list.get(i)==this_window) {
				main_window_list.remove(i);
				break;
			}
		}
		
		if(main_window_list.size() == 0) {
			main_window_list = null;
			if(started_from_main) {
				System.exit(0);
			}
		}		
	}
	
	public static void RequestExit() {
		
		// Might want to ask first
		
		while(main_window_list.size()>0) {
			((MainWindow)main_window_list.get(0)).closeWindow();
		}
	}
	
	public static void RequestHelp(String topic) {
		if(HelpWindow == null) {
			//HelpWindow = new MainFrame("docs",HELPGUI_STYLE);
			HelpWindow = new MainFrame(helpDirectory,MainFrame.PATH_TYPE_FILESYSTEM,HELPGUI_STYLE);
			HelpWindow.setTitle(HELP_TITLE);
			HelpWindow.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
			HelpWindow.setIconImage(new ImageIcon(ApplicationManager.class.getResource(PATH_TO_HELP_WINDOW_ICON)).getImage());
		}
		if(!HelpWindow.isVisible()) {
			HelpWindow.setVisible(true);
		}
		HelpWindow.gotoTopic(topic);
		HelpWindow.requestFocus();
	}
	
	private static void StartApplication() {
		
		main_window_list = new ArrayList<MainWindow>();
    	//main_window_list.add(new MainWindow());
    	started_from_main = true;
    	
    	StartWithSplash();
	}

	/** Returns an icon from the icon library as defined by the 
	 * passed key
	 *
	 * @param key a String used as the key for accessing the icon
	 * @return an Icon object for display
	 */
	public static VectorIcon GetIcon(String key) {
		return icons.getIcon(key);
	}
	
	/** Loads the icons from the specified default icon properties file
	 */
	public static void LoadIcons() {
		icons = new IconLibrary();
	}
	
	private static void ProcessCommandLine(String[] args) {
	    Getopt opt = new Getopt(APP_NAME,args,"hd:");
	    opt.setOpterr(false);
	    
        int c;	    
	    while((c = opt.getopt()) != -1) {
	        switch(c) {
	            case 'h':
	                OutputCommandLineOptions();
	                System.exit(0);
	                break;
	            case 'd':
	                helpDirectory = opt.getOptarg();
	                break;
	        }
	    }
	
	}
	
	private static void OutputCommandLineOptions() {
	    System.out.println(APP_NAME+" "+APP_VERSION+"\n"+
	        "Copyright 2006-2008 Approximatrix, LLC\n" +
	        "\n"+
	        "Command Line Options:\n"+
	        "\t-h      \tDisplay this help message and exit\n"+
	        "\t-d <dir>\tSpecify the path to the online help files\n"+
	        "\n");
	}
}
