package com.atlassian.stash.internal.web.soy;


import com.atlassian.soy.renderer.SoyTemplateRenderer;
import com.atlassian.soy.springmvc.InjectedDataFactory;
import com.atlassian.soy.springmvc.SoyView;
import com.atlassian.soy.springmvc.SoyViewResolver;
import com.atlassian.bitbucket.util.Timer;
import com.atlassian.bitbucket.util.TimerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Component
public class StashSoyViewResolver extends SoyViewResolver {

    public static final String SERVER_SOY_MODULE_KEY = "com.atlassian.bitbucket.server.bitbucket-web:server-soy-templates";

    @Autowired
    public StashSoyViewResolver(SoyTemplateRenderer templateRenderer, InjectedDataFactory injectedDataFactory) {
        super(templateRenderer, injectedDataFactory);
    }

    @Override
    public SoyView constructSoyView(final String viewName) {
        return new SoyView(viewName, templateRenderer, injectedDataFactory, SERVER_SOY_MODULE_KEY) {
            @Override
            public void render(Map data, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
                try (Timer timer = TimerUtils.start("Render soy: " + viewName)) {
                    super.render(data, httpServletRequest, httpServletResponse);
                }
            }
        };
    }
}
