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 OOXMLcomments {
	
	/** id a zoznam override parametrov */
	private LinkedHashMap<String,LinkedHashMap<String,String>> comments = new LinkedHashMap<String, LinkedHashMap<String,String>>();

	/**
	 * Konštruktor
	 */
	public OOXMLcomments() {
		// noop
	}
	
	/**
	 * Funkcia z p elementu vyextrahuje čistý text
	 * @param elP p element
	 * @return String čistý text
	 */
	private String getText(Element elP) {
		try {
			String text = "";
			List elsR = elP.getChildren("r", Global.wns);
			Iterator iteratorElsR = elsR.iterator();
			while (iteratorElsR.hasNext()) {
				Element elR = (Element)iteratorElsR.next();
				List elsT = elR.getChildren("t", Global.wns);
				Iterator iteratorElsT = elsT.iterator();
				while (iteratorElsT.hasNext()) {
					Element elT = (Element)iteratorElsT.next();
					text += elT.getText();
				}
			}
			return text.trim();
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getOverrideParams): " + e.toString());
			return null;
		}
	}
	
	/**
	 * Funkcia prečíta postupne všetky "p" elementy komentára a zistia či majú požadovaný tvar a prípadne zapíšu override parametre
	 * @param commentEl element komentára
	 * @return LinkedHashMap - zoznam override parametrov
	 */
	private LinkedHashMap<String,String> getOverrideParams(Element commentEl) {
		try {
			LinkedHashMap<String,String> overrideParams = new LinkedHashMap<String, String>();
			boolean isOverrideParams = false;
			List elsP = commentEl.getChildren("p", Global.wns);
			Iterator iteratorElsP = elsP.iterator();
			while (iteratorElsP.hasNext()) {
				Element elP = (Element)iteratorElsP.next();
				String text = getText(elP);
				if (!text.trim().isEmpty()) {
					if (text.equals("overrideParams")) {
						isOverrideParams = true;
					}
					else if (isOverrideParams) {
						String[] parts = text.split(" ");
						if (parts.length == 2) {
							overrideParams.put(parts[0], parts[1]);
						}
						else {
							// ak nema komentar predpisanu formu, tak ho ignoruj
							return null;
						}
					}
					else {
						// ak nema komentar predpisanu formu, tak ho ignoruj
						return null;
					}
				}
			}
			return overrideParams;	
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getOverrideParams): " + e.toString());
			return null;
		}
	}
	
	/**
	 * Funkcia prečíta comments
	 * @param commentsEl element container s comments
	 * @return <i>true</i> ak nenastane chyba, <i>false</i> inak
	 */
	private boolean readComments(Element commentsEl) {
		try {
			List commentEls = commentsEl.getChildren("comment", Global.wns);
			Iterator iteratorCommentEls = commentEls.iterator();
			while (iteratorCommentEls.hasNext()) {
				Element commentEl = (Element) iteratorCommentEls.next();
				String id = commentEl.getAttributeValue("id", Global.wns);
				LinkedHashMap<String,String> params = new LinkedHashMap<String,String>();
				params = getOverrideParams(commentEl);
				this.comments.put(id, params);
			}
			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 footnote id
	 * @return LinkedHashMap - zoznam override parametrov
	 */
	public LinkedHashMap<String,String> getComment(String id) {
		try {
			LinkedHashMap<String,String> comment = null;
			comment = this.comments.get(id);
			return comment;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getComment): " + e.toString());
			return null;
		}
	}

	/** 
	 * Funkcia prečíta komentare
	 * @return <i>true</i> ak nenastane chyba, <i>false</i> inak
	 */
	public boolean readComments() {
		try {
			File commentsFile = Global.ooxmlRels.commentsFile;
			if ((commentsFile != null) && (commentsFile.exists())) {
				SAXBuilder builder = new SAXBuilder();
				Document doc = builder.build(commentsFile);
				Element root = doc.getRootElement();
				readComments(root);
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (readComments): " + e.toString());
			return false;
		}
	}
	
}
