/**
 * @(#)HistoryParser.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL.  All unmodified code is copyright of its respective owner.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.io.*;
import java.util.List;
import java.util.Vector;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;

/**
 * This class contains methods to parse a game's XML History.
 * Unused in International Remote Scrabble, included for completeness. 
 * 
 * @version 1.0
 * @see LocalScrabble
 */

public class HistoryParser 
{
	private DocumentBuilder builder;

	public HistoryParser() throws ParserConfigurationException 
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		builder = factory.newDocumentBuilder();
	}

	public List parse(// String file)
			File ff) throws SAXException, IOException 
			{ // File ff = new File(file);
		Document doc = builder.parse(ff);
		Element root = doc.getDocumentElement();
		// System.out.println("Root is: " + root); 
		return getHistory(root);
	}

	public List parse(String file) throws SAXException, IOException 
	{
		File ff = new File(file);
		Document doc = builder.parse(ff);
		Element root = doc.getDocumentElement();
		// System.out.println("Root is: " + root); 
		return getHistory(root);
	}

	private static List getHistory(Element e) 
	{
		Vector moves = new Vector();
		NodeList children = e.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node cn = children.item(i);
			System.out.println("Node name: " + cn.getNodeName());
			if (cn instanceof Element) // skip white space
			{
				Element ce = (Element) cn;
				System.out.println("Tag: " + ce.getTagName());
				if (ce.getTagName().equals("move")) {
					System.out.println("Move found.");
					Move mv = getMove(ce);
					System.out.println(mv);
					moves.add(mv);
				}
			}
		}
		return moves;
	}

	private static Move getMove(Element e) 
	{
		NodeList children = e.getChildNodes();
		Player player = null;
		String score = null;
		Vector letterMoves = new Vector();
		for (int i = 0; i < children.getLength(); i++) {
			Node cn = children.item(i);
			if (cn instanceof Element) {
				Element ce = (Element) cn;
				String tag = ce.getTagName();
				System.out.println("Found element: " + tag);
				if (tag.equals("player")) {
					player = getPlayer(ce);
				} else if (tag.equals("score")) {
					Text tn = (Text) ce.getFirstChild();
					score = tn.getData();
					System.out.println("Player score: " + score);
				} else if (tag.equals("lettermove")) {
					letterMoves.add(getLetterMove(ce));
				}
			}
		}
		return new Move(player, letterMoves);
	}

	private static Player getPlayer(Element e) 
	{
		NodeList children = e.getChildNodes();
		String att = e.getAttribute("playerFlag");
		System.out.println("player flag = " + att);

		String name = "";
		int score = 0;
		for (int i = 0; i < children.getLength(); i++) {
			Node cn = children.item(i);
			if (cn instanceof Element) {
				Element ce = (Element) cn;
				String tag = ce.getTagName();
				Text tn = (Text) ce.getFirstChild();
				String data = tn.getData();
				if (tag.equals("score")) {
					score = Integer.parseInt(data);
				} else if (tag.equals("name")) {
					name = data;
				}
			}
		}
		if (att != null && att.equals("HumanPlayer")) {
			return new HumanPlayer(name);
		} else {
			return new ComputerPlayer(name);
		}
	}

	private static LetterMove getLetterMove(Element e) 
	{
		NodeList children = e.getChildNodes();
		int x = 0;
		int y = 0;
		Letter letter = null;
		for (int i = 0; i < children.getLength(); i++) {
			Node cn = children.item(i);
			if (cn instanceof Element) {
				Element ce = (Element) cn;
				String tag = ce.getTagName();
				Text tn = (Text) ce.getFirstChild();
				String data = tn.getData();
				if (tag.equals("x")) {
					x = Integer.parseInt(data);
				} else if (tag.equals("y")) {
					y = Integer.parseInt(data);
				} else if (tag.equals("letter")) {
					letter = getLetter(ce);
				}
			}
		}
		return new LetterMove(x, y, letter);
	}

	private static Letter getLetter(Element e) 
	{
		NodeList children = e.getChildNodes();
		String symbol = " ";
		int score = 0;
		for (int i = 0; i < children.getLength(); i++) {
			Node cn = children.item(i);
			if (cn instanceof Element) {
				Element ce = (Element) cn;
				String tag = ce.getTagName();
				Text tn = (Text) ce.getFirstChild();
				String data = tn.getData();
				if (tag.equals("symbol")) {
					symbol = data;
				} else if (tag.equals("score")) {
					score = Integer.parseInt(data);
				}
			}
		}
		return new Letter(symbol.charAt(0), score);
	}

	public static void main(String[] args) 
	{
		File file;
		// = new File("out.dat");  /* default */ 
		try {
			HistoryParser hp = new HistoryParser();
			System.out.println(hp.parse("output/out.dat"));
		} catch (Exception e) {
			System.err.println("Load aborted");
		}
	}
}