/**
 * @(#)HelpSystem.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL
 * 			 2004 SPE15, KCL
 * All unmodified code is copyright of its respective owner.
 * 
 * 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 2
 * 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.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.awt.*;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;

import java.io.IOException;

/**
 * This class contains the help frame and HTML loading/displaying.
 * 
 * 
 * @author SPE15, 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see LocalScrabble
 * @see RemoteScrabble
 */

class HelpSystem extends JPanel 
{
	/**
	 * Method to return the Scrabble game
	 * 
	 * @return Scrabble
	 */
	public Scrabble getScrabble() 
	{
		return Scrabble.scrabble;
	}

	/**
	 * Help System, displayed in new dialog, using HTMLEditorKit as renderer.
	 * Help files are stored in resources/help/, then by locale.
	 * Images are stored in resources/images/help 
	 */
	public HelpSystem() 
	{ //Scrabble s = new Scrabble();

		setLayout(new BorderLayout());

		//location of html file outputed to frame in helpEditorPane
		String curDir = System.getProperty("user.dir");
		String initialHelpPage = "file:///" + curDir + "/resources/help/"
							+ getScrabble().getI18n().getLanguage() + "/"
							+ getScrabble().getI18n().getCountry() + "/index.html"; 
		
		JEditorPane helpEditorPane = new JEditorPane();
		helpEditorPane.setEditable(false); //non editable pane, so that nobody
										   // messes with the .html
		helpEditorPane.setEditorKit(new HTMLEditorKit());
		helpEditorPane.addHyperlinkListener(new LinkFollower(helpEditorPane));

		//Put the editor pane in a scroll pane.
		JScrollPane helpScrollPane = new JScrollPane(helpEditorPane);
		helpScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //scrollbar

		add(helpScrollPane, BorderLayout.CENTER);

		try 
		{
			helpEditorPane.setPage(initialHelpPage);
		}
		catch (IOException e) 
		{	//Scrabble.setMessageLine("Bad URL");
		}
	}

	/**
	 * Method to create and show the Help System interface.
	 */
	public static void createAndShowHS() 
	{ 	//Create and set up the window.
		JFrame frame = new JFrame("Help");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//Create and set up the content pane.
		HelpSystem contentPane = new HelpSystem();
		contentPane.setOpaque(true);
		frame.setContentPane(contentPane);
		frame.setResizable(false);
		//Display the window.
		frame.pack();
		//changes the default icon to custom Scrabble icon
		frame.setIconImage(new ImageIcon("resources/images/icon.png").getImage());
		frame.setSize(700, 600);
		frame.setVisible(true);
	}
}