/*
 * Copyright 2004-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.security.saml2.provider.service.web;

import java.util.Map;
import java.util.Objects;

import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jspecify.annotations.Nullable;

import org.springframework.core.convert.converter.Converter;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.RequestPath;
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationPlaceholderResolvers.UriResolver;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.util.Assert;

/**
 * A {@link Converter} that resolves a {@link RelyingPartyRegistration} by extracting the
 * registration id from the request, querying a
 * {@link RelyingPartyRegistrationRepository}, and resolving any template values.
 *
 * @author Josh Cummings
 * @since 5.4
 */
public final class DefaultRelyingPartyRegistrationResolver
		implements Converter<HttpServletRequest, @Nullable RelyingPartyRegistration>, RelyingPartyRegistrationResolver {

	private Log logger = LogFactory.getLog(getClass());

	private final RelyingPartyRegistrationRepository relyingPartyRegistrationRepository;

	private final RequestMatcher registrationRequestMatcher = new RequestMatcher() {
		@Override
		public boolean matches(HttpServletRequest request) {
			return matcher(request).isMatch();
		}

		@Override
		public MatchResult matcher(HttpServletRequest request) {
			RequestPath path = RequestPath.parse(request.getRequestURI(), request.getContextPath());
			PathContainer contextPath = path.contextPath();
			PathContainer relativePath = path.subPath(contextPath.elements().size());
			int size = relativePath.elements().size();
			if (size > 0) {
				return RequestMatcher.MatchResult
					.match(Map.of("registrationId", relativePath.elements().get(size - 1).value()));
			}
			return RequestMatcher.MatchResult.notMatch();
		}
	};

	public DefaultRelyingPartyRegistrationResolver(
			RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) {
		Assert.notNull(relyingPartyRegistrationRepository, "relyingPartyRegistrationRepository cannot be null");
		this.relyingPartyRegistrationRepository = relyingPartyRegistrationRepository;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public @Nullable RelyingPartyRegistration convert(HttpServletRequest request) {
		return resolve(request, null);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public @Nullable RelyingPartyRegistration resolve(HttpServletRequest request,
			@Nullable String relyingPartyRegistrationId) {
		if (relyingPartyRegistrationId == null) {
			if (this.logger.isTraceEnabled()) {
				this.logger.trace("Attempting to resolve from " + this.registrationRequestMatcher
						+ " since registrationId is null");
			}
			relyingPartyRegistrationId = this.registrationRequestMatcher.matcher(request)
				.getVariables()
				.get("registrationId");
		}
		if (relyingPartyRegistrationId == null) {
			if (this.logger.isTraceEnabled()) {
				this.logger.trace("Returning null registration since registrationId is null");
			}
			return null;
		}
		RelyingPartyRegistration registration = this.relyingPartyRegistrationRepository
			.findByRegistrationId(relyingPartyRegistrationId);
		if (registration == null) {
			return null;
		}
		UriResolver uriResolver = RelyingPartyRegistrationPlaceholderResolvers.uriResolver(request, registration);
		String entityId = uriResolver.resolve(registration.getEntityId());
		entityId = Objects.requireNonNull(entityId);
		String assertionConsumerServiceLocation = uriResolver
			.resolve(registration.getAssertionConsumerServiceLocation());
		assertionConsumerServiceLocation = Objects.requireNonNull(assertionConsumerServiceLocation);
		return registration.mutate()
			.entityId(entityId)
			.assertionConsumerServiceLocation(assertionConsumerServiceLocation)
			.singleLogoutServiceLocation(uriResolver.resolve(registration.getSingleLogoutServiceLocation()))
			.singleLogoutServiceResponseLocation(
					uriResolver.resolve(registration.getSingleLogoutServiceResponseLocation()))
			.build();
	}

}
