package digital.laboratory.platform.entrustment.misc;

import digital.laboratory.platform.entrustment.QRCodeUtils;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.ASTIntegerLiteral;
import org.apache.velocity.runtime.parser.node.ASTReference;
import org.apache.velocity.runtime.parser.node.ASTStringLiteral;
import org.apache.velocity.runtime.parser.node.Node;

import java.io.IOException;
import java.io.Writer;

public class VelocityBarCodeImage extends Directive {
	@Override
	public String getName() {
		return "barcodeUrlData";
	}

	@Override
	public int getType() {
		return LINE;
	}

	/**
	 * 渲染指令
	 *
	 * 模板中调用时, 格式化为: #qrcode(code [, width, height])
	 * 参数 code 是必须的, 是字符串, 或字符串变量
	 * 参数 width 和 height 是可选的, 是整数, 应该大于 0
	 *
	 * @param context
	 * @param writer
	 * @param node
	 * @return
	 * @throws IOException
	 * @throws ResourceNotFoundException
	 * @throws ParseErrorException
	 * @throws MethodInvocationException
	 */
	@Override
	public boolean render(InternalContextAdapter context, Writer writer,
						  Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
// System.out.println(String.format("node.jjtGetNumChildren()=%d", node.jjtGetNumChildren()));
// for (int i=0; i<node.jjtGetNumChildren(); i++) {
// 	Node sn = node.jjtGetChild(i);
// 	System.out.println(String.format("node[%d] value=%s type=%d info=%d line=%d", i, sn.value(context), sn.getType(), sn.getInfo(), sn.getLine() ));
// }
		if (node.jjtGetNumChildren()<1) {
			throw new IllegalArgumentException("必须要提供一个参数");
			//return false;
		}

		String code = "nocode";
		int width = 400;
		int height = 30;

		if (node.jjtGetNumChildren()>0) {
			Node sn = node.jjtGetChild(0);
			if ((sn instanceof ASTStringLiteral) || (sn instanceof ASTReference)) {
				code = (String)sn.value(context);
			}
			else {
				throw new IllegalArgumentException("未知的参数类型: " + sn.getClass().getName());
			}
		}

		if (node.jjtGetNumChildren()>1) {
			Node sn = node.jjtGetChild(1);
			if ((sn instanceof ASTIntegerLiteral) /*|| (sn instanceof ASTReference)*/) {
				Integer w =(Integer)sn.value(context);
				if ((w != null) && (w > 0)) {
					width = w;
				}
			}
			else {
				throw new IllegalArgumentException("未知的参数类型: " + sn.getClass().getName());
			}
		}

		if (node.jjtGetNumChildren()>2) {
			Node sn = node.jjtGetChild(2);
			if ((sn instanceof ASTIntegerLiteral) /*|| (sn instanceof ASTReference)*/) {
				Integer h =(Integer)sn.value(context);
				if ((h != null) && (h > 0)) {
					height = h;
				}
			}
			else {
				throw new IllegalArgumentException("未知的参数类型: " + sn.getClass().getName());
			}
		}

		String r = QRCodeUtils.getBarCode128ImageBase64(code, width, height);

		writer.write(r);
		return true;
	}
}
