/**
 *
 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
package lucee.runtime.tag;

import java.io.IOException;

import lucee.runtime.PageSource;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.exp.PageException;
import lucee.runtime.ext.tag.BodyTagImpl;

public final class Timer extends BodyTagImpl {

	private static final int TYPE_DEBUG = 0;
	private static final int TYPE_INLINE = 1;
	private static final int TYPE_OUTLINE = 2;
	private static final int TYPE_COMMENT = 3;

	private String label = "";
	private int type = TYPE_DEBUG;
	private long time;

	@Override
	public void release() {
		super.release();
		type = TYPE_DEBUG;
		label = "";
	}

	/**
	 * @param label the label to set
	 */
	public void setLabel(String label) {
		this.label = label;
	}

	/**
	 * @param type the type to set
	 * @throws ApplicationException
	 */
	public void setType(String strType) throws ApplicationException {
		strType = strType.toLowerCase().trim();
		if ("comment".equals(strType)) type = TYPE_COMMENT;
		else if ("debug".equals(strType)) type = TYPE_DEBUG;
		else if ("inline".equals(strType)) type = TYPE_INLINE;
		else if ("outline".equals(strType)) type = TYPE_OUTLINE;
		else throw new ApplicationException("invalid value [" + strType + "] for attribute [type], valid values are [comment,debug,inline,outline]");
	}

	@Override
	public int doStartTag() {
		time = System.currentTimeMillis();
		if (TYPE_OUTLINE == type) {
			try {
				pageContext.write("<fieldset class=\"cftimer\">");
			}
			catch (IOException e) {}
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws PageException {
		try {
			_doEndTag();
		}
		catch (IOException e) {}
		return EVAL_PAGE;
	}

	public void _doEndTag() throws IOException {
		long exe = (System.currentTimeMillis() - time);
		if (TYPE_INLINE == type) {
			pageContext.write("" + label + ": " + exe + "ms");
		}
		else if (TYPE_OUTLINE == type) {
			pageContext.write("<legend align=\"top\">" + label + ": " + exe + "ms</legend></fieldset>");
		}
		else if (TYPE_COMMENT == type) {
			pageContext.write("<!-- " + label + ": " + exe + "ms -->");
		}
		else if (TYPE_DEBUG == type) {

			if (pageContext.getConfig().debug()) {
				PageSource curr = pageContext.getCurrentTemplatePageSource();
				pageContext.getDebugger().addTimer(label, exe, curr == null ? "unknown template" : curr.getDisplayPath());
			}
		}

		/* <legend align='top'>aaa</legend></fieldset> */

	}

	@Override
	public void doInitBody() {

	}

	@Override
	public int doAfterBody() {
		return SKIP_BODY;
	}

}
@@ -1,12 +1,13 @@
+%EF%BB%BF
 /**%0A *%0A * Co
@@ -2200,17 +2200,16 @@
 timer%5C%22%3E
-%00
 %22);%0A%09%09%09%7D
@@ -2526,32 +2526,33 @@
 LINE == type) %7B%0A
+%0A
 %09%09%09pageContext.w
@@ -2585,17 +2585,16 @@
 xe + %22ms
-%00
 %22);%0A%09%09%7D%0A
@@ -2720,17 +2720,16 @@
 ieldset%3E
-%00
 %22);%0A%09%09%7D%0A
@@ -2826,9 +2826,8 @@
  --%3E
-%00
 %22);%0A
@@ -3096,62 +3096,8 @@
 %09%09%7D%0A
-%0A%09%09/* %3Clegend align='top'%3Eaaa%3C/legend%3E%3C/fieldset%3E */%0A%0A
 %09%7D%0A%0A
@@ -3202,10 +3202,9 @@
 ODY;%0A%09%7D%0A
-%0A
 %7D

clean up