package interactivefeaturelocator.model;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.ntlab.trace.Trace;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Pattern {

	private String patternFolderPath;
	private String plusTraceFilePath;
	private String minusTraceFilePath;
	private File scenarioXml;
	private String markFilePath;

	public Pattern(String patternPath) throws ParserConfigurationException, SAXException, IOException {
		this.patternFolderPath = patternPath;
		File patternFolder = new File(patternFolderPath);
		File[] files = patternFolder.listFiles();
		
		// Featureクラスのインスタンスにpatternsとfeature.xmlを格納
		for (File f : files) {
			if (f.isFile()) {
				if (f.getName().endsWith("xml")) {
					scenarioXml = f;
				}				
			}
		}
		domRead();
	}
	
	public String getPlusTraceFilePath() {
		return plusTraceFilePath;
	}

	public String getMinusTraceFilePath() {
		return minusTraceFilePath;
	}
	
	public File getScenarioXml() {
		return scenarioXml;
	}
	
	public String getMarkFilePath() {
		return markFilePath;
	}	
	
	private void domRead() throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = factory.newDocumentBuilder();
		Document document = documentBuilder.parse(scenarioXml);

		Element root = document.getDocumentElement();

		//ルート要素のノード名を取得する
		System.out.println("ノード名：" +root.getNodeName());

		//ルート要素の属性を取得する
		System.out.println("ルート要素の属性：" + root.getAttribute("name"));

		//ルート要素の子ノードを取得する
		NodeList rootChildren = root.getChildNodes();

		System.out.println("子要素の数：" + rootChildren.getLength());
		System.out.println("------------------");

		for(int i=0; i < rootChildren.getLength(); i++) {
			Node node = rootChildren.item(i);

			if (node.getNodeType() == Node.ELEMENT_NODE) {
				Element element = (Element)node;
				if (element.getNodeName().equals("traces")) {
					NodeList tracesChildren = node.getChildNodes();

					for (int j=0; j < tracesChildren.getLength(); j++) {
						Node tracesNode = tracesChildren.item(j);
						
						if (tracesNode.getNodeType() == Node.ELEMENT_NODE) {
							Element traceElement = (Element)tracesNode;
							if (traceElement.getNodeName().equals("trace")) {
								
								if (traceElement.getAttribute("attr").equals("plus")) {
									System.out.println("--trace(plus)--");
									this.plusTraceFilePath = patternFolderPath + "\\Plus\\" + traceElement.getAttribute("traceFile");
									System.out.println("　PlusTracePath:" + plusTraceFilePath);
									this.markFilePath = patternFolderPath + "\\Plus\\" + traceElement.getAttribute("markFile");
									System.out.println("　MarkFilePath:" + markFilePath);
								} else if (traceElement.getAttribute("attr").equals("minus")) {
									System.out.println("--trace(minus)--");
									this.minusTraceFilePath = patternFolderPath + "\\Minus\\" + traceElement.getAttribute("traceFile");
									System.out.println("　MinusTracePath:" + minusTraceFilePath);
								}
							}


						}
					}
					System.out.println("------------------");
				}
			}


		}
	}
}
