package com.html5parser.insertionModes;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;

import com.html5parser.algorithms.InsertComment;
import com.html5parser.classes.InsertionMode;
import com.html5parser.classes.ParserContext;
import com.html5parser.classes.Token;
import com.html5parser.classes.token.DocTypeToken;
import com.html5parser.factories.InsertionModeFactory;
import com.html5parser.interfaces.IInsertionMode;
import com.html5parser.parseError.ParseErrorType;

public class Initial implements IInsertionMode {

	public ParserContext process(ParserContext parserContext) {

		Token token = parserContext.getTokenizerContext().getCurrentToken();

		switch (token.getType()) {
		// A character token that is one of U+0009 CHARACTER TABULATION, "LF"
		// (U+000A), "FF" (U+000C), "CR" (U+000D), or U+0020 SPACE
		// Ignore the token.
		case character:
			if (!token.isSpaceCharacter())
				anythingElse(parserContext);
			break;
		// A comment token
		// Insert a comment as the last child of the Document object.
		case comment:
			InsertComment
					.run(parserContext, token, parserContext.getDocument());
			break;

		// TODO A DOCTYPE token
		case DOCTYPE:
			DocTypeToken thisToken = (DocTypeToken) token;
			String tagName = thisToken.getValue();
			String publicIdentifier = thisToken.getPublicIdentifier();
			String systemIdentifier = thisToken.getSystemIdentifier();
			if ((tagName.equals("html") || publicIdentifier != null || (systemIdentifier != null && !tagName
					.equals("about:legacy-compat")))
					&& !(((tagName.equals("html"))
							&& publicIdentifier != null
							&& publicIdentifier
									.equals("-//W3C//DTD HTML 4.0//EN") && (systemIdentifier == null || systemIdentifier
							.equals("http://www.w3.org/TR/REC-html40/strict.dtd")))
							|| ((tagName.equals("html"))
									&& publicIdentifier != null
									&& publicIdentifier
											.equals("-//W3C//DTD HTML 4.01//EN") && (systemIdentifier == null || systemIdentifier
									.equals("http://www.w3.org/TR/html4/strict.dtd")))
							|| ((tagName.equals("html"))
									&& publicIdentifier != null
									&& systemIdentifier != null
									&& publicIdentifier
											.equals("-//W3C//DTD XHTML 1.0 Strict//EN") && systemIdentifier
										.equals("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd")) || ((tagName
							.equals("html"))
							&& publicIdentifier != null
							&& systemIdentifier != null
							&& publicIdentifier
									.equals("-//W3C//DTD XHTML 1.1//EN") && systemIdentifier
								.equals("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"))

					)) {
				parserContext.addParseErrors(ParseErrorType.UnexpectedToken);
			}
			Document doc = parserContext.getDocument();
			DOMImplementation domImpl = doc.getImplementation();
			DocumentType doctype = domImpl.createDocumentType(token.getValue(),
					thisToken.getPublicIdentifier(),
					thisToken.getSystemIdentifier());
			doc.appendChild(doctype);
			// doc.appendChild(doc.createElement(thisToken.getValue()));
			InsertionModeFactory factory = InsertionModeFactory.getInstance();
			parserContext.setInsertionMode(factory
					.getInsertionMode(InsertionMode.before_html));
			break;
		default:
			anythingElse(parserContext);
			break;
		}
		return parserContext;
	}

	public void anythingElse(ParserContext parserContext) {
		// Anything else
		// TODO If the document is not an iframe srcdoc document, then this is a
		// parse error; set the Document to quirks mode.
		// In any case, switch the insertion mode to "before html", then
		// reprocess the token.
		parserContext.setInsertionMode(InsertionModeFactory.getInstance()
				.getInsertionMode(InsertionMode.before_html));
		parserContext.setFlagReconsumeToken(true);
	}
}

@@ -1439,16 +1439,36 @@
 );%0A%09%09%09if
+ (tagName != null &&
  ((tagNa
@@ -1587,24 +1587,25 @@
 y-compat%22)))
+)
 %0A%09%09%09%09%09&& !((
@@ -2901,82 +2901,33 @@
 pe(t
-oken.getValue(),%0A%09%09%09%09%09thisToken.getPublicIdentifier(),%0A%09%09%09%09%09thisToken.getS
+agName,publicIdentifier,s
 yste
@@ -2937,18 +2937,16 @@
 entifier
-()
 );%0A%09%09%09do

check null for tagname