/*
 * Copyright 2012-2015 the original author or authors.
 *
 * 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.
 */

package org.springframework.boot.autoconfigure.mustache;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.bind.PropertySourcesPropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

import com.samskivert.mustache.DefaultCollector;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Mustache.Collector;
import com.samskivert.mustache.Mustache.VariableFetcher;

/**
 * Mustache {@link Collector} to expose properties from the Spring {@link Environment}.
 *
 * @author Dave Syer
 * @since 1.2.2
 */
public class MustacheEnvironmentCollector extends DefaultCollector implements
		EnvironmentAware {

	private ConfigurableEnvironment environment;

	private Map<String, Object> target;

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = (ConfigurableEnvironment) environment;
		this.target = new HashMap<String, Object>();
		new RelaxedDataBinder(this.target).bind(new PropertySourcesPropertyValues(
				this.environment.getPropertySources()));
	}

	@Override
	public Mustache.VariableFetcher createFetcher(Object ctx, String name) {
		VariableFetcher fetcher = super.createFetcher(ctx, name);
		if (fetcher != null) {
			return fetcher;
		}
		if (this.environment.containsProperty(name)) {
			return new VariableFetcher() {

				@Override
				public Object get(Object ctx, String name) throws Exception {
					return MustacheEnvironmentCollector.this.environment
							.getProperty(name);
				}

			};
		}
		if (this.target.containsKey(name)) {
			return new VariableFetcher() {

				@Override
				public Object get(Object ctx, String name) throws Exception {
					return MustacheEnvironmentCollector.this.target.get(name);
				}

			};
		}
		return null;
	}

}

@@ -846,16 +846,78 @@
 Binder;%0A
+import org.springframework.boot.bind.RelaxedPropertyResolver;%0A
 import o
@@ -1125,49 +1125,8 @@
 or;%0A
-import com.samskivert.mustache.Mustache;%0A
 impo
@@ -1551,16 +1551,149 @@
 arget;%0A%0A
+%09private RelaxedPropertyResolver propertyResolver;%0A%0A%09private final VariableFetcher propertyFetcher = new PropertyVariableFetcher();%0A%0A
 %09@Overri
@@ -1979,16 +1979,84 @@
 es()));%0A
+%09%09this.propertyResolver = new RelaxedPropertyResolver(environment);%0A
 %09%7D%0A%0A%09@Ov
@@ -2070,25 +2070,16 @@
 %09public 
-Mustache.
 Variable
@@ -2246,27 +2246,32 @@
 f (this.
-environment
+propertyResolver
 .contain
@@ -2303,116 +2303,34 @@
 urn 
-new VariableFetcher() %7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public Object get(Object ctx, String name) throws Exception %7B%0A%09%09%09
+this.propertyFetcher;%0A%09%09%7D%0A
 %09%09re
@@ -2338,150 +2338,67 @@
 urn 
-MustacheEnvironmentCollector.this.environment%0A%09%09%09%09%09%09%09.getProperty(name);%0A%09%09%09%09%7D%0A%0A%09%09%09%7D;%0A%09%09%7D%0A%09%09if (this.target.containsKey(name)) %7B%0A%09%09%09return new
+null;%0A%09%7D%0A%0A%09private class PropertyVariableFetcher implements
  Var
@@ -2413,16 +2413,12 @@
 cher
-()
  %7B%0A%0A
-%09%09
 %09%09@O
@@ -2425,18 +2425,16 @@
 verride%0A
-%09%09
 %09%09public
@@ -2489,18 +2489,16 @@
 ption %7B%0A
-%09%09
 %09%09%09retur
@@ -2537,26 +2537,44 @@
 his.
-target.get
+propertyResolver.getProperty
 (name);%0A
 %09%09%09%09
@@ -2573,40 +2573,13 @@
 e);%0A
-%09%09
 %09%09%7D%0A%0A
-%09%09%09%7D;%0A%09%09%7D%0A%09%09return null;%0A
 %09%7D%0A%0A

Simplify MustacheEnvironmentCollector