/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2015 wcm.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package io.wcm.devops.conga.generator.handlebars;

import io.wcm.devops.conga.generator.GeneratorException;
import io.wcm.devops.conga.generator.plugins.escapingstrategy.NoneEscapingStrategy;
import io.wcm.devops.conga.generator.spi.EscapingStrategyPlugin;
import io.wcm.devops.conga.generator.util.PluginManager;
import io.wcm.devops.conga.resource.ResourceCollection;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.commons.lang3.StringUtils;

import com.github.jknack.handlebars.EscapingStrategy;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
 * Manages charset-aware handlebars instances.
 */
public class HandlebarsManager {

  private final List<ResourceCollection> templateDirs;
  private final PluginManager pluginManager;

  private final LoadingCache<HandlebarsKey, Handlebars> handlebarsCache =
      CacheBuilder.newBuilder().build(new CacheLoader<HandlebarsKey, Handlebars>() {
        @Override
        public Handlebars load(HandlebarsKey options) throws Exception {
          TemplateLoader templateLoader = new CharsetAwareTemplateLoader(templateDirs, options.getCharset());
          Handlebars handlebars = new Handlebars(templateLoader);
          handlebars.with(getEscapingStrategy(options.getFileExtension()));
          return handlebars;
        }
      });

  /**
   * @param templateDirs Template base directories
   */
  public HandlebarsManager(List<ResourceCollection> templateDirs, PluginManager pluginManager) {
    this.templateDirs = templateDirs;
    this.pluginManager = pluginManager;
  }

  /**
   * Get handelbars instance with escaping for file extension and charset.
   * @param fileExtension File extension
   * @param charset Charset
   * @return Handlebars instance
   */
  public Handlebars get(String fileExtension, String charset) {
    HandlebarsKey key = new HandlebarsKey(fileExtension, charset);
    try {
      return handlebarsCache.get(key);
    }
    catch (ExecutionException ex) {
      throw new GeneratorException("Unable to get handlebars instance for " + key.toString(), ex);
    }
  }

  /**
   * Find escaping strategy matching for the given file extension.
   * @param fileExtension File extension
   * @return Escaping strategy (never null, returns {@link NoneEscapingStrategy} if no match found.
   */
  private EscapingStrategy getEscapingStrategy(String fileExtension) {
    EscapingStrategyPlugin escapingStrategyPlugin = pluginManager.getAll(EscapingStrategyPlugin.class).stream()
        .filter(plugin -> !StringUtils.equals(plugin.getName(), NoneEscapingStrategy.NAME))
        .filter(plugin -> plugin.accepts(fileExtension))
        .findFirst().orElse(pluginManager.get(NoneEscapingStrategy.NAME, EscapingStrategyPlugin.class));
    return escapingStrategyPlugin.getEscapingStrategy();
  }

}

@@ -1966,31 +1966,14 @@
     
-Handlebars handlebars =
+return
  new
@@ -1999,17 +1999,16 @@
 eLoader)
-;
 %0A       
@@ -2010,26 +2010,20 @@
         
-handlebars
+    
 .with(ge
@@ -2074,37 +2074,8 @@
 ));%0A
-          return handlebars;%0A
     

simplify code