/**
 * 
 */
package view.aide;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import language.Language;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;
import tool.observable.EasyObserver;
/**
 * @author alexis
 *
 */
public class HelpMenu extends JPanel implements EasyObserver, ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1226017388881956351L;
	private HashMap<String,JPanel> sujets;
	private JPanel panelIndex;
	private JButton buttonRetour;
	private final String index_ID = "index";
	private GridBagConstraints constraint;
	
	public HelpMenu(Language lang) {
		super(new GridBagLayout());
		sujets = new HashMap<String,JPanel>();
		
		initIndex(lang);
		loadFromXML(lang.getNomFichierAide());
	}
	
	private void initIndex(Language lang){
		panelIndex = new JPanel(new GridBagLayout());
		buttonRetour = new JButton(lang.getInformationRetour());
		buttonRetour.addActionListener(this);
		buttonRetour.setVisible(false);
		
		constraint = new GridBagConstraints();
		
		JLabel indexLabel = new JLabel("Index");
		indexLabel.setForeground(Color.BLUE);
		indexLabel.setFont(new Font("Arial", Font.ITALIC, 24));
		
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		constraint.insets  = new Insets(0,10,0,10);
		panelIndex.add(indexLabel,constraint);
		
		constraint.anchor  = GridBagConstraints.NORTH;
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		constraint.insets  = new Insets(0,10,5,10);
		panelIndex.add(new JSeparator(),constraint);
		
		constraint.anchor  = GridBagConstraints.NORTHWEST;
		constraint.fill    = GridBagConstraints.HORIZONTAL;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 0;
		constraint.insets  = new Insets(0,10,0,10);
		this.add(panelIndex,constraint);
		
		constraint.anchor  = GridBagConstraints.SOUTHEAST;
		constraint.fill    = GridBagConstraints.NONE;
		constraint.weightx = 1.0;
		constraint.weighty = 1.0;
		constraint.gridx   = 0;
		constraint.gridy   = 1;
		constraint.insets  = new Insets(0,10,10,10);
		this.add(buttonRetour,constraint);
		
		sujets.put(index_ID, panelIndex);
	}
	
	private void loadFromXML(String _xmlPath){
		/*initialised to null to remove IDE "not declared"*/
		Document doc = null;
		
		try {
		    File XML = new File(_xmlPath);
		    Builder parser = new Builder();
		    doc = parser.build(XML);
		}
		catch (ValidityException ex) {
		  System.err.println("XML format invalid");
		}
		catch (ParsingException ex) {
		    System.err.println("XML format failure");
		}
		catch (IOException ex) {
		    System.err.println("Could not reach XML file");
		}
		    
	    Element aide = (Element)doc.getChild(0);
	    Elements aideElem = aide.getChildElements();
	    
	    for (int i = 0; i < aideElem.size(); i++) 
	    {/*for each subject and separator*/
            Element sujetNode = aideElem.get(i);
	    	if(sujetNode.getLocalName().equals("separator"))
	    	{
	    		System.out.println("?");
	    		constraint.anchor  = GridBagConstraints.NORTHWEST;
	    		constraint.fill    = GridBagConstraints.NONE;
	    		constraint.weightx = 1.0;
	    		constraint.weighty = 1.0;
	    		constraint.gridx   = 0;
	    		constraint.gridy   = i+1;
	    		constraint.insets  = new Insets(10,10,0,10);
	            panelIndex.add(new JLabel("---"+sujetNode.getAttributeValue("id")+"---"),constraint);
	    	}
	    	else
	    	{
	            String id = sujetNode.getAttributeValue("id");
	            String titre = sujetNode.getChildElements("titre").get(0).getChild(0).getValue().replace('\n', ' ').trim();
	            String texte = sujetNode.getChildElements("texte").get(0).getChild(0).getValue();//.replace('\n', ' ').trim();
	            texte = texte.substring(1,texte.lastIndexOf("\n"));
	            
	            JLinkedLabel indexLink = new JLinkedLabel(id,titre);
	            indexLink.getObservable().addObserver(this);
	            
	            constraint.anchor  = GridBagConstraints.NORTHWEST;
	    		constraint.fill    = GridBagConstraints.NONE;
	    		constraint.weightx = 1.0;
	    		constraint.weighty = 1.0;
	    		constraint.gridx   = 0;
	    		constraint.gridy   = i+1;
	    		constraint.insets  = new Insets(0,10,0,10);
	            panelIndex.add(indexLink,constraint);
	            
	            /*liens*/
	            HelpSubject sujet = new HelpSubject(id,titre,texte);
	            
	            Elements liens = sujetNode.getChildElements("lien");
	            for (int j = 0; j < liens.size(); j++) 
			    {
	            	String lien_ID = liens.get(j).getAttributeValue("vers");
	            	String lien_texte = liens.get(j).getChild(0).getValue().replace('\n', ' ').trim();
	            	
	            	sujet.addLink(lien_ID,lien_texte);
			    }
	            
	            sujet.getObservable().addObserver(this);
	            this.sujets.put(id, sujet);
	            
	            constraint.anchor  = GridBagConstraints.NORTH;
	    		constraint.fill    = GridBagConstraints.HORIZONTAL;
	    		constraint.weightx = 1.0;
	    		constraint.weighty = 1.0;
	    		constraint.gridx   = 0;
	    		constraint.gridy   = 0;
	    		constraint.insets  = new Insets(0,10,0,10);
	            this.add(sujet,constraint);
	    	}
        }
	    //this.setSelected(index_ID);
		buttonRetour.doClick();
	}
	
	private void setSelected(String _ID){
		buttonRetour.setVisible(true);
		
		for(Entry<String, JPanel> entry : sujets.entrySet()) {
		    String key = entry.getKey();
		    if(!key.equals(_ID))
		    	sujets.get(key).setVisible(false);
		    else
		    	sujets.get(_ID).setVisible(true);
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		this.setSelected(index_ID);
		buttonRetour.setVisible(false);
	}
	
	@Override
	public void update(Object observable, Object message) throws SQLException,Exception {
		this.setSelected((String)message);
	}
}
