/*
 * 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.buildpack.platform.docker.transport;

import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.apache.hc.core5.http.HttpStatus;

import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * Exception thrown when a call to the Docker API fails.
 *
 * @author Phillip Webb
 * @author Scott Frederick
 * @author Siva Sai Udayagiri
 * @since 2.3.0
 */
public class DockerEngineException extends RuntimeException {

	private final int statusCode;

	private final String reasonPhrase;

	private final Errors errors;

	private final Message responseMessage;

	/**
	 * Create a new {@link DockerEngineException}.
	 * @param host the host
	 * @param uri the URI being called
	 * @param statusCode the status code
	 * @param reasonPhrase the reason phrase
	 * @param errors the errors or {@code null}
	 * @param responseMessage the response message
	 * @deprecated since 3.4.12 for removal in 4.0.0 since the exception should only be
	 * thrown by the transport.
	 */
	@Deprecated(since = "3.4.12", forRemoval = true)
	public DockerEngineException(String host, URI uri, int statusCode, String reasonPhrase, Errors errors,
			Message responseMessage) {
		this(host, uri, statusCode, reasonPhrase, errors, responseMessage, null);
	}

	DockerEngineException(String host, URI uri, int statusCode, String reasonPhrase, Errors errors,
			Message responseMessage, byte[] content) {
		super(buildMessage(host, uri, statusCode, reasonPhrase, errors, responseMessage, content));
		this.statusCode = statusCode;
		this.reasonPhrase = reasonPhrase;
		this.errors = errors;
		this.responseMessage = responseMessage;
	}

	/**
	 * Return the status code returned by the Docker API.
	 * @return the statusCode the status code
	 */
	public int getStatusCode() {
		return this.statusCode;
	}

	/**
	 * Return the reason phrase returned by the Docker API.
	 * @return the reasonPhrase
	 */
	public String getReasonPhrase() {
		return this.reasonPhrase;
	}

	/**
	 * Return the errors from the body of the Docker API response, or {@code null} if the
	 * errors JSON could not be read.
	 * @return the errors or {@code null}
	 */
	public Errors getErrors() {
		return this.errors;
	}

	/**
	 * Return the message from the body of the Docker API response, or {@code null} if the
	 * message JSON could not be read.
	 * @return the message or {@code null}
	 */
	public Message getResponseMessage() {
		return this.responseMessage;
	}

	private static String buildMessage(String host, URI uri, int statusCode, String reasonPhrase, Errors errors,
			Message responseMessage, byte[] content) {
		Assert.notNull(host, "Host must not be null");
		Assert.notNull(uri, "URI must not be null");
		StringBuilder message = new StringBuilder(
				"Docker API call to '" + host + uri + "' failed with status code " + statusCode);
		if (StringUtils.hasLength(reasonPhrase)) {
			message.append(" \"").append(reasonPhrase).append("\"");
		}
		if (responseMessage != null && StringUtils.hasLength(responseMessage.getMessage())) {
			message.append(" and message \"").append(responseMessage.getMessage()).append("\"");
		}
		else if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED && !ObjectUtils.isEmpty(content)) {
			String contentString = new String(content, StandardCharsets.UTF_8);
			message.append(" and content \"").append(contentString.trim()).append("\"");
		}
		if (errors != null && !errors.isEmpty()) {
			message.append(" ").append(errors);
		}
		return message.toString();
	}

}
