/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package org.apache.hc.client5.http.impl.cache;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.StreamSupport;

import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.utils.Hex;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.Internal;
import org.apache.hc.core5.annotation.ThreadingBehavior;
import org.apache.hc.core5.function.Resolver;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HeaderElement;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.MessageHeaders;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicHeaderElementIterator;
import org.apache.hc.core5.http.message.BasicHeaderValueFormatter;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.net.PercentCodec;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
import org.apache.hc.core5.util.TextUtils;

/**
 * @since 4.1
 */
@Contract(threading = ThreadingBehavior.STATELESS)
public class CacheKeyGenerator implements Resolver<URI, String> {

    /**
     * Default instance of {@link CacheKeyGenerator}.
     */
    public static final CacheKeyGenerator INSTANCE = new CacheKeyGenerator();

    /**
     * @deprecated Do not use.
     */
    @Deprecated
    @Override
    public String resolve(final URI uri) {
        return generateKey(uri);
    }

    /**
     * @deprecated Use {@link CacheSupport#requestUriRaw(HttpHost, HttpRequest)}
     */
    @Deprecated
    @Internal
    public static String getRequestUri(final HttpHost target, final HttpRequest request) {
        return CacheSupport.requestUriRaw(target, request);
    }

    /**
     * @deprecated Use {@link CacheSupport#normalize(URI)}
     */
    @Deprecated
    @Internal
    public static URI normalize(final URI requestUri) throws URISyntaxException {
        return CacheSupport.normalize(requestUri);
    }

    /**
     * @deprecated Use {@link CacheSupport#normalize(URI)}
     */
    @Deprecated
    @Internal
    public static URI normalize(final String requestUri) {
        if (requestUri == null) {
            return null;
        }
        try {
            return CacheSupport.normalize(new URI(requestUri));
        } catch (final URISyntaxException ex) {
            return null;
        }
    }

    /**
     * Computes a key for the given request {@link URI} that can be used as
     * a unique identifier for cached resources. The URI is expected to
     * in an absolute form.
     *
     * @param requestUri request URI
     * @return cache key
     */
    public String generateKey(final URI requestUri) {
        try {
            final URI normalizeRequestUri = CacheSupport.normalize(requestUri);
            return normalizeRequestUri.toASCIIString();
        } catch (final URISyntaxException ex) {
            return requestUri.toASCIIString();
        }
    }

    /**
     * Computes a root key for the given {@link HttpHost} and {@link HttpRequest}
     * that can be used as a unique identifier for cached resources.
     *
     * @param host The host for this request
     * @param request the {@link HttpRequest}
     * @return cache key
     *
     * @deprecated Use {@link #generateKey(HttpHost, HttpRequest, Function)}
     */
    @Deprecated
    public String generateKey(final HttpHost host, final HttpRequest request) {
        return generateKey(host, request, r -> null);
    }

    /**
     * Computes a root key for the given {@link HttpHost}, {@link HttpRequest} and
     * request body that can be used as a unique identifier for cached resources.
     * <p>
     * For requests with an enclosed body, such as {@literal QUERY}, the key also
     * incorporates a digest of the request content and the value of the request
     * {@literal Content-Type} header, as required by the HTTP QUERY method
     * specification (RFC 10008).
     *
     * @param host The host for this request
     * @param request the {@link HttpRequest}
     * @param bodyExtractor function to extract request body. May be {@code null}
     * @return cache key
     *
     * @since 5.7
     */
    public <T extends HttpRequest> String generateKey(final HttpHost host,
                                                      final T request,
                                                      final Function<T, byte[]> bodyExtractor) {
        final String s = CacheSupport.requestUriRaw(host, request);
        String rootKey;
        try {
            rootKey = generateKey(new URI(s));
        } catch (final URISyntaxException ex) {
            rootKey = s;
        }
        final byte[] body = bodyExtractor != null ? bodyExtractor.apply(request) : null;
        if (body != null) {
            final Header contentType = request.getFirstHeader(HttpHeaders.CONTENT_TYPE);
            return "{" + request.getMethod() + ":" + generateContentDigest(contentType, body) + "}" + rootKey;
        }
        return rootKey;
    }

    /**
     * Computes a digest of request content and its content type suitable
     * for cache key generation.
     */
    static String generateContentDigest(final Header contentType, final byte[] content) {
        final MessageDigest digester;
        try {
            digester = MessageDigest.getInstance("SHA-256");
        } catch (final NoSuchAlgorithmException ex) {
            throw new IllegalStateException("SHA-256 message digest is not supported", ex);
        }
        if (contentType != null && contentType.getValue() != null) {
            digester.update(contentType.getValue().getBytes(StandardCharsets.UTF_8));
        }
        digester.update((byte) 0);
        digester.update(content);
        return Hex.encodeHexString(digester.digest());
    }

    /**
     * Returns all variant names contained in {@literal VARY} headers of the given message.
     *
     * @since 5.4
     */
    public static List<String> variantNames(final MessageHeaders message) {
        if (message == null) {
            return null;
        }
        final List<String> names = new ArrayList<>();
        for (final Iterator<Header> it = message.headerIterator(HttpHeaders.VARY); it.hasNext(); ) {
            final Header header = it.next();
            MessageSupport.parseTokens(header, names::add);
        }
        return names;
    }

    @Internal
    public static void normalizeElements(final MessageHeaders message, final String headerName, final Consumer<String> consumer) {
        // User-Agent as a special case due to its grammar
        if (headerName.equalsIgnoreCase(HttpHeaders.USER_AGENT)) {
            final Header header = message.getFirstHeader(headerName);
            if (header != null) {
                consumer.accept(header.getValue().toLowerCase(Locale.ROOT));
            }
        } else {
            normalizeElements(message.headerIterator(headerName), consumer);
        }
    }

    @Internal
    public static void normalizeElements(final Iterator<Header> iterator, final Consumer<String> consumer) {
        final Iterator<HeaderElement> it = new BasicHeaderElementIterator(iterator);
        StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.NONNULL), false)
                .filter(e -> !TextUtils.isBlank(e.getName()))
                .map(e -> {
                    if (e.getValue() == null && e.getParameterCount() == 0) {
                        return e.getName().toLowerCase(Locale.ROOT);
                    }
                    final CharArrayBuffer buf = new CharArrayBuffer(1024);
                    BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
                            buf,
                            new BasicNameValuePair(
                                e.getName().toLowerCase(Locale.ROOT),
                                !TextUtils.isBlank(e.getValue()) ? e.getValue() : null),
                            false);
                    if (e.getParameterCount() > 0) {
                        for (final NameValuePair nvp : e.getParameters()) {
                            if (!TextUtils.isBlank(nvp.getName())) {
                                buf.append(';');
                                BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(
                                        buf,
                                        new BasicNameValuePair(
                                                nvp.getName().toLowerCase(Locale.ROOT),
                                                !TextUtils.isBlank(nvp.getValue()) ? nvp.getValue() : null),
                                        false);
                            }
                        }
                    }
                    return buf.toString();
                })
                .sorted()
                .distinct()
                .forEach(consumer);
    }

    /**
     * Computes a "variant key" for the given request and the given variants.
     * @param request originating request
     * @param variantNames variant names
     * @return variant key
     *
     * @since 5.4
     */
    public String generateVariantKey(final HttpRequest request, final Collection<String> variantNames) {
        Args.notNull(variantNames, "Variant names");
        final StringBuilder buf = new StringBuilder("{");
        final AtomicBoolean firstHeader = new AtomicBoolean();
        variantNames.stream()
                .map(h -> h.toLowerCase(Locale.ROOT))
                .sorted()
                .distinct()
                .forEach(h -> {
                    if (!firstHeader.compareAndSet(false, true)) {
                        buf.append("&");
                    }
                    buf.append(PercentCodec.encode(h, StandardCharsets.UTF_8)).append("=");
                    final AtomicBoolean firstToken = new AtomicBoolean();
                    normalizeElements(request, h, t -> {
                        if (!firstToken.compareAndSet(false, true)) {
                            buf.append(",");
                        }
                        buf.append(PercentCodec.encode(t, StandardCharsets.UTF_8));
                    });
                });
        buf.append("}");
        return buf.toString();
    }

    /**
     * Computes a "variant key" from the headers of the given request if the given
     * cache entry can have variants ({@code Vary} header is present).
     *
     * @param request originating request
     * @param entry cache entry
     * @return variant key if the given cache entry can have variants, {@code null} otherwise.
     */
    public String generateVariantKey(final HttpRequest request, final HttpCacheEntry entry) {
        if (entry.containsHeader(HttpHeaders.VARY)) {
            final List<String> variantNames = variantNames(entry);
            return generateVariantKey(request, variantNames);
        }
        return null;
    }

    /**
     * Computes a key for the given {@link HttpHost} and {@link HttpRequest}
     * that can be used as a unique identifier for cached resources. if the request has a
     * {@literal VARY} header the identifier will also include variant key.
     *
     * @param host The host for this request
     * @param request the {@link HttpRequest}
     * @param entry the parent entry used to track the variants
     * @return cache key
     *
     * @deprecated Use {@link #generateKey(HttpHost, HttpRequest)} or {@link #generateVariantKey(HttpRequest, Collection)}
     */
    @Deprecated
    public String generateKey(final HttpHost host, final HttpRequest request, final HttpCacheEntry entry) {
        final String rootKey = generateKey(host, request);
        final List<String> variantNames = variantNames(entry);
        if (variantNames.isEmpty()) {
            return rootKey;
        }
        return generateVariantKey(request, variantNames) + rootKey;
    }

}
