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

import com.atlassian.soy.springmvc.InjectedDataFactory;
import com.atlassian.stash.internal.ApplicationConstants;
import com.atlassian.stash.internal.web.util.xsrf.XsrfTokenInterceptor;
import com.google.common.collect.Maps;
import org.springframework.context.i18n.LocaleContextHolder;

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

/**
 * An implementation of injectedDataFactory which will never access the database
 *
 * @since 1.2
 */
public class MaintenanceInjectedDataFactory implements InjectedDataFactory {

    @Override
    public Map<String, Object> createInjectedData(HttpServletRequest request) {
        final Map<String, Object> injectedData = Maps.newHashMap();

        // Use the product name in place of the instance name
        injectedData.put("instanceName", ApplicationConstants.PRODUCT_NAME);

        // used in the user level lozenge
        injectedData.put("productName", ApplicationConstants.PRODUCT_NAME);

        // this needs to seeded from somewhere for i18n
        injectedData.put("language", LocaleContextHolder.getLocale().getLanguage());

        injectedData.put(XsrfTokenInterceptor.XSRF_TOKEN_NAME, request.getAttribute(XsrfTokenInterceptor.XSRF_TOKEN_NAME));
        injectedData.put(XsrfTokenInterceptor.XSRF_TOKEN_VALUE, request.getAttribute(XsrfTokenInterceptor.XSRF_TOKEN_VALUE));

        return injectedData;
    }
}