/**
 * ADIImporter.java
 * Created Apr 30, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.title.conf.specs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.tandbergtv.watchpoint.pmm.entities.Title;

/**
 *
 * @author Sahil Verma
 */
public class ADIImporter {

	private static final Logger logger = Logger.getLogger(ADIImporter.class);
	
	/**
	 * Creates a ADIImporter
	 */
	public ADIImporter() {
	}
	
	/**
	 * Reads the ADI metadata file from the given path and converts to a PMM Title.
	 *   
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public Title importTitle(String path) throws Exception {
		File file = new File(path);
		Document document = load(file);
		Title title = getTitle(document.getDocumentElement());
		
		return title;
	}
	
	/*
	 * Reads an ADI metadata file from the specified path
	 */
	private Document load(File file) {
		Document document;
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			
			factory.setValidating(false);
			factory.setAttribute("http://xml.org/sax/features/validation", Boolean.FALSE);
			factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", Boolean.FALSE);			

			DocumentBuilder builder = factory.newDocumentBuilder();
			
			builder.setEntityResolver(null);
			document = builder.parse(file);
		} catch (Exception e) {
			throw new RuntimeException("Failed to read ADI file " + file.getAbsolutePath(), e);
		}
		
		return document;
	}
	
	@Deprecated
	private Title getTitle(Node asset) throws Exception {
		/*Title title = new Title("CableLabsVOD11");
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		Node metadata = (Node)xpath.evaluate("Metadata", asset, XPathConstants.NODE);
		
		for (TitleMetadata field : getMetadata(metadata))
			title.addMetadata(field);
		
		String type = title.getMetadataFieldValue("Asset_Class");
		title.setName(type);
		
		NodeList assets = (NodeList)xpath.evaluate("Asset", asset, XPathConstants.NODESET);
		
		for (int i = 0; i < assets.getLength(); i++) {
			Title child = getTitle(assets.item(i));
			
			title.addChild(child);
		}
		
		Node content = (Node)xpath.evaluate("Content", asset, XPathConstants.NODE);
		
		if (content != null)
			title.addMetadata(getContent(content));
		
		return title;*/
		
		return null;
	}
	
	/*private Collection<TitleMetadata> getMetadata(Node metadata) throws Exception {
		Collection<TitleMetadata> fields = new ArrayList<TitleMetadata>();
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		NodeList list = (NodeList)xpath.evaluate("App_Data", metadata, XPathConstants.NODESET);
		
		for (int i = 0; i < list.getLength(); i++) {
			Node node = list.item(i);
			String name = node.getAttributes().getNamedItem("Name").getTextContent();
			String value = node.getAttributes().getNamedItem("Value").getTextContent();
			TitleMetadata field = new TitleMetadata(name, value); 
			logger.debug("Metadata " + name + " " + value);
			
			fields.add(field);
		}
		
		Node ams = (Node)xpath.evaluate("AMS", metadata, XPathConstants.NODE);
		
		for (TitleMetadata field : getAMSMetadata(ams))
			fields.add(field);
		
		return fields;
	}
	
	private Collection<TitleMetadata> getAMSMetadata(Node ams) throws Exception {
		Collection<TitleMetadata> fields = new ArrayList<TitleMetadata>();
		
		for (int i = 0; i < ams.getAttributes().getLength(); i++) {
			Attr attribute = (Attr)ams.getAttributes().item(i);
			String name = attribute.getName();
			String value = attribute.getNodeValue();
			TitleMetadata field = new TitleMetadata(name, value);
			
			logger.debug("Metadata " + name + " " + value);
			fields.add(field);
		}
		
		return fields;
	}
	
	private TitleMetadata getContent(Node content) throws Exception {
		String value = content.getAttributes().getNamedItem("Value").getTextContent();
		logger.debug("Found content " + value);
		
		return new TitleMetadata("Content", value);
	}*/
}
