package org.mote.wiimote.whiteboard.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.net.URL;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

import net.miginfocom.swing.MigLayout;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.mote.wiimote.whiteboard.WiimoteWhiteboard;
import org.mote.wiimote.whiteboard.util.BareBonesBrowserLaunch;
import org.mote.wiimote.whiteboard.util.Util;

@SuppressWarnings("serial")
public class AboutWindow extends JDialog implements HyperlinkListener {
	
	public AboutWindow() {
		super(Application.getInstance(WiimoteWhiteboard.class).getMainFrame());
		if (!Util.INSIDE_APP_BUNDLE) {
			setName("aboutWindow");
			setLayout(new BorderLayout());
			final JPanel appPane = Util.newComponent(JPanel.class, "appPane");
			add(appPane, BorderLayout.NORTH);
			appPane.setLayout(new MigLayout("insets i", "[center|center]"));
			
			JLabel appLabel = new JLabel(String.format("%s %s", WiimoteWhiteboard.getProperty("id"), WiimoteWhiteboard.getProperty("version")));
			appLabel.setFont(appLabel.getFont().deriveFont(Font.BOLD, 14f));		
			JLabel crLabel = new JLabel(Util.getResourceMap(AboutWindow.class).getString("copyRight", WiimoteWhiteboard.getProperty("year"), WiimoteWhiteboard.getProperty("author")));
			crLabel.setFont(appLabel.getFont().deriveFont(10f));
					
			appPane.add(appLabel, "flowy, split 2");
			appPane.add(crLabel, "");		
			appPane.add(new JLabel(Util.getResourceMap(WiimoteWhiteboard.class).getImageIcon("icon")), "flowx, wrap");
			
			try {
				URL url = AboutWindow.class.getResource("resources/Credits.html");
				JEditorPane tp = Util.newComponent(JEditorPane.class, "infoPane");
				tp.addHyperlinkListener(this);
				tp.setPage(url);
				add(new JScrollPane(tp), BorderLayout.CENTER);			
				
				Util.getResourceMap(AboutWindow.class).injectComponents(this);
				
				setResizable(false);
				Util.placeDialogWindow(this, 295, 348);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
    public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			BareBonesBrowserLaunch.openURL(e.getURL().toString());
		}
	}
	
	@Action
	public void about() {
		setVisible(true);
	}

}
