// Yura Mamyrin

package risk.engine;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import javax.swing.JEditorPane;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.File;

/**
 * <p> About Dialog </p>
 * @author Yura Mamyrin
 */


public class AboutDialog extends JDialog {

	private String title;
	private String product;
	private String version;
	private String author = " Yura Mamyrin (yura@yura.net)";
	private String copyright;
	private String comments;
	private JPanel contentPane = new JPanel();
	private JLabel prodLabel = new JLabel();
	private JLabel verLabel = new JLabel();
	private JLabel authLabel = new JLabel();
	private JLabel copLabel = new JLabel();
	private JTextArea commentField = new JTextArea();
	private JPanel btnPanel = new JPanel();
	private JButton okButton = new JButton();
	private JLabel image = new JLabel();
	// private BorderLayout formLayout = new BorderLayout();
	private GridBagLayout contentPaneLayout = new GridBagLayout();
	private FlowLayout btnPaneLayout = new FlowLayout();

	private JPanel infoPanel = new JPanel();
	private JTextArea info1 = new JTextArea();
	private JTextArea info2 = new JTextArea();

	/**
	 * Creates a new AboutDialog
	 * @param parent decides the parent of the frame
	 * @param modal
	 * @param p contains the product
	 * @param v contains the GUI version
	 */

	public AboutDialog(Frame parent, boolean modal, String p, String v) {
		super(parent, modal);
		product=p;
		initGUI(v);

	}

	/** This method is called from within the constructor to initialize the dialog. */
	private void initGUI(String v) {
		java.util.ResourceBundle resb = risk.engine.translation.TranslationBundle.getBundle();

		version=resb.getString("about.version")+" " + v;

		this.title		= resb.getString("about.title");
		this.copyright	= resb.getString("about.copyright");
		this.comments	= " " + resb.getString("about.comments");

		// setSize(280,250); // (214,180)

		contentPane.setLayout(contentPaneLayout);
		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
		prodLabel.setText(product);
		contentPane.add(prodLabel,
				new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1,
						0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));

		verLabel.setText(version);
		contentPane.add(verLabel,
				new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1,
						0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));

		authLabel.setText(resb.getString("about.author") + author);
		contentPane.add(authLabel,
				new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1,
						0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));

		copLabel.setText(copyright);
		contentPane.add(copLabel,
				new GridBagConstraints(GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.REMAINDER, 1,
						0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 0, 0, 0), 0, 0));

		// commentField.setWrapStyleWord(true);
		// commentField.setLineWrap(true);

		commentField.setBackground(copLabel.getBackground());
		commentField.setForeground(copLabel.getForeground());
		commentField.setFont(copLabel.getFont());
		commentField.setText(comments);
		commentField.setEditable(false);
		commentField.setOpaque(false);

		// image.setText("[Your image]");
		image.setIcon(new javax.swing.ImageIcon( Risk.class.getResource("logo.gif") ));
		image.setPreferredSize(new java.awt.Dimension(80,70));
		image.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

		btnPanel.setLayout(btnPaneLayout);
		okButton.setText(" " + resb.getString("about.okbutton") + " ");
		okButton.addActionListener(
				new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
						dispose();
					}
				});
		btnPanel.add(okButton);
		okButton.setDefaultCapable( true);

		infoPanel.setLayout(new javax.swing.BoxLayout(infoPanel, javax.swing.BoxLayout.X_AXIS));
		info1.setText(" " + resb.getString("about.infopanel"));

		info2.setText(

			Risk.getSystemInfoText() + " " + resb.getString("about.compiledfor")

		);

		info1.setBackground(copLabel.getBackground());
		info1.setForeground(copLabel.getForeground());
		info1.setFont(copLabel.getFont());
		info1.setEditable(false);
		info1.setOpaque(false);

		info2.setBackground(copLabel.getBackground());
		info2.setForeground(copLabel.getForeground());
		info2.setFont(copLabel.getFont());
		info2.setEditable(false);
		info2.setOpaque(false);

		infoPanel.add(info1);
		infoPanel.add(info2);
		infoPanel.setOpaque(false);

		//info1.setFocusable( false);
		//info2.setFocusable( false);
		//commentField.setFocusable( false);




		JEditorPane editorPane1 = new JEditorPane();
		JEditorPane editorPane2 = new JEditorPane();
		JEditorPane editorPane3 = new JEditorPane();

		editorPane1.setEditable(false);
		editorPane2.setEditable(false);
		editorPane3.setEditable(false);

		JScrollPane scrollPane1 = new JScrollPane(editorPane1);
		JScrollPane scrollPane2 = new JScrollPane(editorPane2);
		JScrollPane scrollPane3 = new JScrollPane(editorPane3);

		JTabbedPane tabbedpane = new JTabbedPane();
		tabbedpane.addTab( resb.getString("about.tab.sysinfo") , infoPanel);
		tabbedpane.addTab( resb.getString("about.tab.credits"), scrollPane1);
		tabbedpane.addTab( resb.getString("about.tab.license"), scrollPane2);
		tabbedpane.addTab( resb.getString("about.tab.changelog"), scrollPane3);

		JPanel allcontent = new JPanel();

		// allcontent.setLayout(formLayout);
		allcontent.setLayout(new java.awt.GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new java.awt.Insets(3, 3, 3, 3);


		c.gridx = 0; // col
		c.gridy = 0; // row
		c.gridwidth = 1; // width
		c.gridheight = 1; // height
		allcontent.add(image, c);


		c.gridx = 1; // col
		c.gridy = 0; // row
		c.gridwidth = 1; // width
		c.gridheight = 1; // height
		allcontent.add(contentPane, c);


		c.gridx = 0; // col
		c.gridy = 3; // row
		c.gridwidth = 2; // width
		c.gridheight = 1; // height
		allcontent.add(btnPanel, c);


		c.fill = GridBagConstraints.BOTH;


		c.gridx = 0; // col
		c.gridy = 2; // row
		c.gridwidth = 2; // width
		c.gridheight = 1; // height
		allcontent.add(tabbedpane, c);


		c.gridx = 0; // col
		c.gridy = 1; // row
		c.gridwidth = 2; // width
		c.gridheight = 1; // height
		allcontent.add(commentField, c);


		getContentPane().add(allcontent);

		setTitle(title);
		setResizable(false);
		okButton.requestFocus();


		addWindowListener(
				new java.awt.event.WindowAdapter() {
					public void windowClosing(WindowEvent evt) {
						closeDialog(evt);
					}
				});


		pack();
		Dimension size = getPreferredSize();


		try {

		    URL url;

		    if (Risk.applet==null) {

			//url = new URL( "file:///"+System.getProperty("user.dir") );
			url = new File(".").toURI().toURL();
		    }
		    else {

			url = Risk.applet.getCodeBase();
		    }

		    editorPane1.setPage( url+"/help/risk_credits.htm" );

		    editorPane2.setPage( url+"/gpl.txt" );

		    editorPane3.setPage( url+"/ChangeLog.txt" );


		}
		catch(Exception e) {
		    editorPane1.setText( e.getMessage() );
		    editorPane2.setText( e.getMessage() );
		    editorPane3.setText( e.getMessage() );
		}

		setSize(size);

	}


	/** Closes the dialog */
	private void closeDialog(WindowEvent evt) {
		setVisible(false);
		dispose();
	}
}
