package OOXMLconvert;

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

/**
 *
 * @author ferou
 */
public class OOXMLtheme {
	
	/** ku každému názvu štýlu farby RGB HEX hodnota farby */
	private HashMap<String,String> colors = new HashMap<String,String>();
	
	/**
	 * Konštruktor
	 */
	public OOXMLtheme() {
		// noop
	}
	
	/**
	 * Funkcia na základe daného názvu štýlu v téme vráti RGB HEX hodnotu farby
	 * @param name názov štýlu
	 * @return String RGB HEX hodnota farby
	 */
	public String getColor(String name) {
		try {
			if (colors.containsKey(name)) {
				String color = colors.get(name);
				return color;
			}
			return null;
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (getColor): " + e.toString());
			return null;
		}
	}
	
	/**
	 * Funkcia prečíta farby v témach.
	 * @param themeEl element obsahujúci tému
	 */
	private void readColors(Element themeEl) {
		try {
			Element clrSchemeEl = themeEl.getChild("themeElements", Global.ans).getChild("clrScheme", Global.ans);
			List clrEls = clrSchemeEl.getChildren();
			Iterator i = clrEls.iterator();
			while (i.hasNext()) {
				Element clrEl = (Element)i.next();
				String name = clrEl.getName().trim();
				Element srgbClrEl = clrEl.getChild("srgbClr", Global.ans);
				if (srgbClrEl == null) {
					continue;
				}
				String value = srgbClrEl.getAttributeValue("val").trim();
				if (!name.isEmpty() && !value.isEmpty()) {
					this.colors.put(name, value);
				}
			}
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (readColors): " + e.toString());
		}
	}

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