package com.quantum.actions;


import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import com.quantum.Messages;
import com.quantum.model.xml.ModelToXMLConverter;
import com.quantum.sql.metadata.MetaDataXMLInterface;
import com.quantum.util.xml.XMLHelper;
import com.quantum.view.ViewHelper;
import com.quantum.view.bookmark.BookmarkView;
import com.quantum.view.bookmark.EntityNode;
import com.quantum.view.bookmark.TreeNode;
import com.quantum.view.subset.SubsetView;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author root
 */
public class ExportXMLAction extends Action implements IViewActionDelegate {
    
	IViewPart view;
	/**
	 * @see org.eclipse.ui.IViewActionDelegate#init(IViewPart)
	 */
	public void init(IViewPart view) {
		this.view = view;
	}

	/**
	 * @see org.eclipse.ui.IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		run();
	}
		
	public void run() {
		
		FileOutputStream out = ViewHelper.askSaveFile("exportxml", view.getSite().getShell());
		if (out == null)
			return;
		StructuredSelection selection = null;
		if (view instanceof BookmarkView){
			BookmarkView bookmarkView = (BookmarkView) view;
			selection = bookmarkView.getSelection(); 
		} else if (view instanceof SubsetView){
			SubsetView subsetView = (SubsetView) view;
			selection = subsetView.getSelection();
		}

		try {
            Document doc = XMLHelper.createEmptyDocument();
            exportXMLSelection(doc, selection);
            try {
                XMLHelper.write(out, doc);
            } finally {
    			out.close();
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		
	}

	/**
	 * Exports to an XmlDocument the items selected in a StructuredSelection.
	 * @param doc
	 * @param selection
	 */
	public void exportXMLSelection(Document doc, StructuredSelection selection) {
		Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.Metadata"))); //$NON-NLS-1$
		MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Author"), //$NON-NLS-1$
									Messages.getString("ExportXMLAction.Quantum")); //$NON-NLS-1$
		MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Version"), //$NON-NLS-1$
									Messages.getString("ExportXMLAction.XMLVersionNumber")); //$NON-NLS-1$
		Iterator iter = selection.iterator();
		while (iter.hasNext()) {
			TreeNode current = (TreeNode) iter.next();
// TODO: reinstate this
//			if (current instanceof SubsetNode){
//				SubsetNode subset = (SubsetNode) current;
//				MetaDataXMLInterface.createElementText(root, Messages.getString("ExportXMLAction.Subset"), //$NON-NLS-1$
//												subset.getName()); //$NON-NLS-1$
//				Object[] children = subset.getChildren();
//				for (int i = 0; i < children.length; i++) {
//					TreeNode objectNode = (TreeNode) children[i];
//					if (objectNode instanceof SelectableNode)
//						ExportXMLAction.exportObject(root, (SelectableNode)objectNode);					
//				}
//			} else {
                exportObject(root, current);                    
//			}
		}
		
	}

	/**
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * Exports a TreeNode metadata representation to an XmlDocument, based on a an already-created root Element
	 * @param doc The XmlDocument to receive the metadata representation
	 * @param node The node with the metadata to export
	 * @param root The root element (already present in the XmlDocument) that will hold the metadata
	 */
	public void exportObject(Element root, TreeNode node) {
		if (node instanceof EntityNode) { 
			EntityNode entityNode = (EntityNode) node;
	        ModelToXMLConverter.getInstance().convert(root, entityNode.getEntity());
		}
	}
}