/*
 * Copyright 2012-present 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
 *
 *      https://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.mustache.reactive.view;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import com.samskivert.mustache.Mustache.Compiler;
import com.samskivert.mustache.Template;
import org.jspecify.annotations.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.server.ServerWebExchange;

/**
 * Spring WebFlux {@link View} using the Mustache template engine.
 *
 * @author Brian Clozel
 * @since 4.0.0
 */
public class MustacheView extends AbstractUrlBasedView {

	private @Nullable Compiler compiler;

	private @Nullable Charset charset;

	/**
	 * Set the JMustache compiler to be used by this view. Typically this property is not
	 * set directly. Instead a single {@link Compiler} is expected in the Spring
	 * application context which is used to compile Mustache templates.
	 * @param compiler the Mustache compiler
	 */
	public void setCompiler(Compiler compiler) {
		this.compiler = compiler;
	}

	/**
	 * Set the {@link Charset} used for reading Mustache template files.
	 * @param charset the charset
	 * @since 4.1.0
	 */
	public void setCharset(@Nullable Charset charset) {
		this.charset = charset;
	}

	/**
	 * Set the name of the charset used for reading Mustache template files.
	 * @param charset the charset
	 * @deprecated since 4.1.0 for removal in 4.3.0 in favor of
	 * {@link #setCharset(Charset)}
	 */
	@Deprecated(since = "4.1.0", forRemoval = true)
	public void setCharset(@Nullable String charset) {
		setCharset((charset != null) ? Charset.forName(charset) : null);
	}

	@Override
	public boolean checkResourceExists(Locale locale) throws Exception {
		return resolveResource() != null;
	}

	@Override
	protected Mono<Void> renderInternal(Map<String, Object> model, @Nullable MediaType contentType,
			ServerWebExchange exchange) {
		Resource resource = resolveResource();
		if (resource == null) {
			return Mono
				.error(new IllegalStateException("Could not find Mustache template with URL [" + getUrl() + "]"));
		}
		DataBuffer dataBuffer = exchange.getResponse()
			.bufferFactory()
			.allocateBuffer(DefaultDataBufferFactory.DEFAULT_INITIAL_CAPACITY);
		try (Reader reader = getReader(resource)) {
			Assert.state(this.compiler != null, "'compiler' must not be null");
			Template template = this.compiler.compile(reader);
			Charset charset = getCharset(contentType).orElseGet(this::getDefaultCharset);
			try (Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset)) {
				template.execute(model, writer);
				writer.flush();
			}
		}
		catch (Exception ex) {
			DataBufferUtils.release(dataBuffer);
			return Mono.error(ex);
		}
		return exchange.getResponse().writeWith(Flux.just(dataBuffer));
	}

	private @Nullable Resource resolveResource() {
		ApplicationContext applicationContext = getApplicationContext();
		String url = getUrl();
		if (applicationContext == null || url == null) {
			return null;
		}
		Resource resource = applicationContext.getResource(url);
		if (resource == null || !resource.exists()) {
			return null;
		}
		return resource;
	}

	private Reader getReader(Resource resource) throws IOException {
		if (this.charset != null) {
			return new InputStreamReader(resource.getInputStream(), this.charset);
		}
		return new InputStreamReader(resource.getInputStream());
	}

	private Optional<Charset> getCharset(@Nullable MediaType mediaType) {
		return Optional.ofNullable((mediaType != null) ? mediaType.getCharset() : null);
	}

}
