package entity;

import boundary.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;

/**
 * @author ofuangka
 * StockXMLToDatabase takes an XML Document from XMLBuilder and writes all of the stock information
 * to the database specified by DatabaseCommunicator
 *
 */
public class XMLReader {
	private Document xmlDocument;
	private Element root;
	
	public XMLReader(Document document) {
		xmlDocument = document;
		root = xmlDocument.getDocumentElement();
		if (!DatabaseCommunicator.connect()) {
			System.err.println("Could not connect to database.\nShutting down...");
			System.exit(1);
		}
		else {
			System.out.println("Successfully connected to database.");
		}
	}
	public boolean writeSelfToDatabase() {
		NodeList stockNodeList = root.getChildNodes();
		NodeList labelNodeList = null;
		Node stockNode = null;
		Node labelNode = null;
		String stockSymbol;
		String labelName;
		String labelValue;
		String query;
		String currentDate = (new java.sql.Timestamp((new java.util.Date()).getTime())).toString();
		boolean retval = false;
		
		// write yourself to the database
		for (int i = 1; i < stockNodeList.getLength(); i+=2) {
			stockNode = stockNodeList.item(i);
			stockSymbol = stockNode.getNodeName();
			labelNodeList = stockNode.getChildNodes();
			for (int j = 1; j < labelNodeList.getLength(); j+=2) {
				// write each label to the database
				labelNode = labelNodeList.item(j);
				labelName = labelNode.getNodeName();
				// mysql hack, since div is a keyword
				if (labelName.equals("div")) {
					labelName = "div_";
				}
				labelValue = labelNode.getTextContent();
				query = formUpdateQuery(stockSymbol, labelName, labelValue);
				DatabaseCommunicator.query(query);
				retval = true;
				// also insert necessary info into the history file
				if (labelName.equals("last")) {
					query = formHistorySQL(stockSymbol, labelValue, currentDate);
					DatabaseCommunicator.query(query);
				}
			}
		}
		return retval;
	}
	
	public boolean insertSelfAsFirstStocks() {
		NodeList stockNodeList = root.getChildNodes();
		Node stockNode = null;
		String stockSymbol;
		String query;
		
		// write everything to the database
		for (int i = 1; i < stockNodeList.getLength(); i+=2) {
			stockNode = stockNodeList.item(i);
			stockSymbol = stockNode.getNodeName();
			query = formInsertSQL(stockSymbol);
			DatabaseCommunicator.query(query);
		}
		return true;
	}
	
	// helper methods to quickly form SQL
	private String formUpdateQuery(String stockSymbol, String labelName, String labelValue) {
		String retval = "UPDATE stock SET " + labelName + " = '" + labelValue + "' WHERE symbol = '" + stockSymbol + "';";
		return retval;
	}
	private String formInsertSQL(String stockSymbol) {
		String retval = "INSERT INTO stock (symbol) VALUES ('" + stockSymbol + "');";
		return retval;
	}
	private String formHistorySQL (String stockSymbol, String currentPrice, String currentDate) {
		String retval = "INSERT INTO stock_history (symbol, price, date) VALUES ('" + stockSymbol + "', '" + currentPrice + "', '" + currentDate + "');";
		return retval;
	}
}
