package OOXMLconvert;

import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.Element;

/**
 *
 * @author ferou
 */
public class OOXMLnotes {
	
	/** id a výstupný HTML element footnote */
	private LinkedHashMap<String,Element> footnotes = new LinkedHashMap<String, Element>();
	/** id a výstupný HTML element endnote */
	private LinkedHashMap<String,Element> endnotes = new LinkedHashMap<String, Element>();

	/**
	 * Konštruktor
	 */
	public OOXMLnotes() {
		// noop
	}
	
	/**
	 * Funkcia prečíta footnotes
	 * @param footnotesEl element container s footnotes
	 * @return <i>true</i> ak nenastane chyba, <i>false</i> inak
	 */
	private boolean readFootnotes(Element footnotesEl) {
		try {
			List footnoteEls = footnotesEl.getChildren("footnote", Global.wns);
			Iterator iteratorFootnoteEls = footnoteEls.iterator();
			while (iteratorFootnoteEls.hasNext()) {
				Element footnoteEl = (Element) iteratorFootnoteEls.next();
				String id = footnoteEl.getAttributeValue("id", Global.wns);
				OOXMLparser ooxmlParser = new OOXMLparser();
				Element XHTML = ooxmlParser.parse(footnoteEl);
				this.footnotes.put(id.trim(), XHTML);
			}
			return true;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (readFootnotes): " + e.toString());
			return false;
		}
	}
	
	/**
	 * Funkcia prečíta endnotes
	 * @param endnotesEl element container s endnotes
	 * @return <i>true</i> ak nenastane chyba, <i>false</i> inak
	 */
	private boolean readEndnotes(Element endnotesEl) {
		try {
			List endnoteEls = endnotesEl.getChildren("endnote", Global.wns);
			Iterator iteratorEndnoteEls = endnoteEls.iterator();
			while (iteratorEndnoteEls.hasNext()) {
				Element endnoteEl = (Element) iteratorEndnoteEls.next();
				String id = endnoteEl.getAttributeValue("id", Global.wns);
				OOXMLparser ooxmlParser = new OOXMLparser();
				Element XHTML = ooxmlParser.parse(endnoteEl);
				this.endnotes.put(id.trim(), XHTML);
			}
			return true;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (readEndnotes): " + e.toString());
			return false;
		}
	}
	
	/**
	 * Funkcia na základe daného id vráti zodpovedajúcu footnote
	 * @param id id footnote
	 * @return HTML element priradený k danej footnote
	 */
	public Element getFootnote(String id) {
		try {
			Element footnote = null;
			footnote = footnotes.get(id);
			return footnote;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getFootnote): " + e.toString());
			return null;
		}
	}
	
	/**
	 * Funkcia na základe daného id vráti zodpovedajúcu endnote
	 * @param id id endnote
	 * @return HTML element priradený k danej endnote
	 */
	public Element getEndnote(String id) {
		try {
			Element endnote = null;
			endnote = endnotes.get(id);
			return endnote;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getEndnote): " + e.toString());
			return null;
		}
	}
	
	/** 
	 * Funkcia prečíta footnotes a endnotes
	 * @return <i>true</i> ak nenastane chyba, <i>false</i> inak
	 */
	public boolean readNotes() {
		try {
			File footnotesFile = Global.ooxmlRels.footnotesFile;
			if ((footnotesFile != null) && (footnotesFile.exists())) {
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(footnotesFile);
				Element root = doc.getRootElement();
				readFootnotes(root);
			}
			File endnotesFile = Global.ooxmlRels.endnotesFile;
			if ((endnotesFile != null) && (endnotesFile.exists())) {
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(endnotesFile);
				Element root = doc.getRootElement();
				readEndnotes(root);
			}
			return true;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (readNotes): " + e.toString());
			return false;
		}
	}
	
}
