package com.peterphi.std.guice.web.rest.templating.thymeleaf;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.peterphi.std.annotation.Doc;
import com.peterphi.std.threading.Timeout;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContext;
import java.util.concurrent.TimeUnit;

public class TemplateResolverProvider implements Provider<ITemplateResolver>
{
	@Inject(optional = true)
	@Named("thymeleaf.cache-ttl")
	@Doc("The maximum Time-To-Live value on the thymeleaf in-memory template cache (default 1h)")
	Timeout cacheTTL = new Timeout(1, TimeUnit.HOURS);

	@Inject
	ServletContext ctx;

	@Override
	public ITemplateResolver get()
	{
		ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(ctx);

		resolver.setTemplateMode("HTML");

		// Load templates from WEB-INF/templates/{name}.html
		resolver.setPrefix("/WEB-INF/template/");
		resolver.setSuffix(".html");

		if (cacheTTL.getMilliseconds() > 0)
		{
			// cache templates for an hour
			resolver.setCacheTTLMs(cacheTTL.getMilliseconds());
			resolver.setCacheable(true);
		}
		else
		{
			// Don't cache
			resolver.setCacheable(false);
		}

		return resolver;
	}
}

@@ -732,16 +732,159 @@
 OURS);%0A%0A
+%09@Inject(optional = true)%0A%09@Named(%22thymeleaf.mode%22)%0A%09@Doc(%22The thymeleaf template mode (default HTML)%22)%0A%09public String templateMode = %22HTML%22;%0A%0A
 %09@Inject
@@ -1065,22 +1065,28 @@
 ateMode(
-%22HTML%22
+templateMode
 );%0A%0A%09%09//

Allow Thymeleaf Template Mode to be configured. Defaults to HTML as before, but can now be set to XML if desired