/*
 *  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.general.ui;

import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;


import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.application.ApplicationManager;

public class AboutDialog extends ChildFrame {

	private static final String DIALOG_NAME = "About "+ApplicationManager.APP_NAME;
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "welcome";
	
	private static final String COPYRIGHT_KEY = "copyright";
	private static final String LICENSE_KEY = "license";
	
	private static final int ABOUT_WIDTH = 425;
	private static final int ABOUT_HEIGHT = 450;
	
	private static final String PATH_TO_ABOUT_PICTURE = "/com/approximatrix/draco/main/ui/images/about/draco.svg";
	private static final String PATH_TO_TECHS = "/com/approximatrix/draco/main/ui/images/about/";
	
	private JPanel contentPane = null;
	private JTabbedPane mainPane = null;
	
	// The about panel
	private JPanel aboutPanel = null;
	private VectorGraphicsPanel picPanel = null;
	private JPanel copyrightPanel = null;
	private JPanel aboutButtonPanel = null;
	
	// Technology panel
	private JPanel techPanel = null;
	private VectorGraphicsPanel[] logoPanels = null;
	private JLabel[] infoLabels = null;
	
	public AboutDialog() {
		super(DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		initialize();

	}

	private void initialize() {
		this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		this.setSize(ABOUT_WIDTH,ABOUT_HEIGHT);
		this.setContentPane(this.getMainContentPane());
		this.setResizable(true);
		//this.setLocationRelativeTo(null);
		this.setTitle(DIALOG_NAME);
	}

	private JPanel getMainContentPane() {
		if(contentPane == null) {
			contentPane = new JPanel();
			contentPane.setLayout(new BorderLayout());
			contentPane.add(getMainPane(),BorderLayout.CENTER);
		}
		return contentPane;
	}
	
	private JTabbedPane getMainPane() {
		if(mainPane == null) {
			mainPane = new JTabbedPane();
			mainPane.setTabPlacement(JTabbedPane.TOP);
			//mainPane.add(title, component);
			mainPane.add("Draco Econometrics", getAboutPanel());
			//mainPane.add("Technologies",getTechPanel());
		}
		return mainPane;
	}
	
	private JPanel getAboutPanel() {
		if(aboutPanel == null) {
			aboutPanel = new JPanel();
			//aboutPanel.setLayout(new BoxLayout(aboutPanel,BoxLayout.Y_AXIS));
			//aboutPanel.add(getPicPanel());
			//aboutPanel.add(getCopyrightPanel());
			aboutPanel.setLayout(new BorderLayout());
			aboutPanel.add(getPicPanel(),BorderLayout.CENTER);
			
			JPanel south = new JPanel();
			south.setLayout(new BorderLayout());
			south.add(getCopyrightPanel(),BorderLayout.CENTER);
			south.add(getAboutButtonsPanel(),BorderLayout.SOUTH);
			
			aboutPanel.add(south,BorderLayout.SOUTH);
			aboutPanel.doLayout();
		}
		return aboutPanel;
	}
	
	private VectorGraphicsPanel getPicPanel() {
		if(picPanel == null) {
			picPanel = new VectorGraphicsPanel(PATH_TO_ABOUT_PICTURE);
			picPanel.setBackground(Color.WHITE);
		}
		return picPanel;
	}
	
	private JPanel getCopyrightPanel() {
		if(copyrightPanel == null) {
			copyrightPanel = new JPanel();
			copyrightPanel.setBackground(Color.WHITE);
			copyrightPanel.setLayout(new BoxLayout(copyrightPanel,BoxLayout.Y_AXIS));
			copyrightPanel.add(new JLabel("<html><b>"+ApplicationManager.APP_NAME+"    "+ApplicationManager.APP_VERSION+"</b></html>"));
			copyrightPanel.add(new JLabel("Copyright 2006-2009 Approximatrix, LLC"));
			copyrightPanel.add(new JLabel("http://www.approximatrix.com/"));
			copyrightPanel.add(new JLabel("Portions Copyrighted by Various Contributors"));
			copyrightPanel.add(new JLabel("Licensed under the GNU General Public License"));
		}
		return copyrightPanel;
	}
	
	private JPanel getAboutButtonsPanel() {
		if(aboutButtonPanel == null) {
			aboutButtonPanel = new JPanel();
			aboutButtonPanel.setBackground(Color.WHITE);
			FlowLayout layout = new FlowLayout();
			layout.setAlignment(FlowLayout.CENTER);
			aboutButtonPanel.setLayout(layout);
			
			JButton button = new JButton("Copyright Info");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ApplicationManager.RequestHelp(COPYRIGHT_KEY);
				}
			});
			aboutButtonPanel.add(button);
			
			button = new JButton("Draco License");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ApplicationManager.RequestHelp(LICENSE_KEY);
				}
			});
			aboutButtonPanel.add(button);
		}
		return aboutButtonPanel;
	}
	
	private JPanel getTechPanel() {
		if(techPanel == null) {
			techPanel = new JPanel();
			GridLayout layout = new GridLayout(5,2);
			layout.setVgap(8);
			layout.setHgap(8);
			techPanel.setLayout(layout);
			for(int i=0;i<5;i++) {
				techPanel.add(getLogoPanel()[i]);
				techPanel.add(getInfoLabels()[i]);
			}
		}
		return techPanel;
	}
	
	private VectorGraphicsPanel[] getLogoPanel() {
		if(logoPanels == null) {
			logoPanels = new VectorGraphicsPanel[5];
			
			// Openchart2
			logoPanels[0] = new VectorGraphicsPanel(PATH_TO_TECHS+"openchart2.svg");
			
			// LAPACK
			logoPanels[1] = new VectorGraphicsPanel(PATH_TO_TECHS+"lapack.svg");
			
			// MESP
			logoPanels[2] = new VectorGraphicsPanel(PATH_TO_TECHS+"MESP.svg");
			
			// HelpGUI
			logoPanels[3] = new VectorGraphicsPanel(PATH_TO_TECHS+"helpgui.svg");
			
			// SVGSalamander
			logoPanels[4] = new VectorGraphicsPanel(PATH_TO_TECHS+"svgsalamander.svg");
			
			for(int i=0;i<5;i++) {
				logoPanels[i].setCenterY(true);
				logoPanels[i].setCenterX(false);
			}
			
		}
		return logoPanels;
	}
	
	private JLabel[] getInfoLabels() {
		if(infoLabels == null) {
			infoLabels = new JLabel[5];
			
			// Openchart2
			infoLabels[0] = new JLabel("<html>Openchart2 Library<br>"+
					                   "Copyright 2006-2007 Approximatrix, LLC<br>"+
					                   "Copyright 2001 Sebastian Müller<br>"+
					                   "Licensed under the GNU LGPL</html>");
			
			// LAPACK
			infoLabels[1] = new JLabel("<html>jLAPACK 0.6 - Pure Java LAPACK<br>A product of the F2J team at UTK</html>");
			
			// MESP
			infoLabels[2] = new JLabel("<html>MESP Library<br>"+
					                   "Copyright 2004 Graph Builder<br>"+
	                                   "Licensed under a modern BSD License</html>");
			
			// HelpGUI
			infoLabels[3] = new JLabel("<html>HelpGUI 1.1 (Modified)<br>"+
					                   "Copyright 2003-2004 Alexandre THOMAS<br>"+
            						   "Licensed under the GNU GPL</html>");

			// SVGSalamander
			infoLabels[4] = new JLabel("<html>svgSalamander Library<br>"+
					                   "Copyright 2004 Mark McKay<br>"+
									   "Licensed under the GNU LGPL</html>");

		}
		return infoLabels;
	}
	
	/* (non-Javadoc)
	 * @see com.approximatrix.draco.application.ChildFrame#getMenuItems()
	 */
	@Override
	public JMenu[] getMenuItems() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.approximatrix.draco.application.ChildFrame#getToolbarButtons()
	 */
	@Override
	public Component[] getToolbarButtons() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.approximatrix.draco.application.ChildFrame#isRenameable()
	 */
	@Override
	public boolean isRenameable() {
		// TODO Auto-generated method stub
		return false;
	}

	/** Returns the help key for the About window
	 * 
	 * @return the proper help key, defined by HELP_KEY
	 * @see #HELP_KEY
	 * @see com.approximatrix.draco.application.ChildFrame#getHelpKey()
	 */
	public String getHelpKey() {
		return HELP_KEY;
	}
}
