/*
 * 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.
 */
package org.apache.jackrabbit.oak.plugins.index.elastic.util;

import org.apache.jackrabbit.oak.commons.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class ElasticIndexUtils {

    private static final Logger LOG = LoggerFactory.getLogger(ElasticIndexUtils.class);

    /**
     * Convert a JCR property name to a Elasticsearch field name.
     * Notice that "|" is not allowed in JCR names.
     *
     * @param propertyName the property name
     * @return the field name
     */
    public static String fieldName(String propertyName) {
        if(propertyName.startsWith(":")) {
            // there are some hardcoded field names
            return propertyName;
        }
        String fieldName = propertyName;
        boolean blank = fieldName.isBlank();
        boolean escape = false;
        if (blank) {
            // empty field name or field names that only consist of spaces
            escape = true;
        } else {
            // 99.99% property names are OK,
            // so we loop over the characters first
            for (int i = 0; i < fieldName.length() && !escape ; i++) {
                switch (fieldName.charAt(i)) {
                case '|':
                case '.':
                case '^':
                case '_':
                    escape = true;
                }
            }
        }
        if (escape) {
            StringBuilder buff = new StringBuilder(fieldName.length());
            if (fieldName.startsWith("_") || blank) {
                // internal field start with a _
                // we also support empty or just spaces
                buff.append('|');
            }
            for (int i = 0; i < fieldName.length(); i++) {
                char c = fieldName.charAt(i);
                // For performance, the logic for the currently supported
                // characters is hardcoded.
                // In case more characters need to be escaped,
                // buff.append('|').append(Integer.toHexString(c)).append('|');
                switch (c) {
                case '|':
                    buff.append("||");
                    break;
                case '.':
                    buff.append("|2e|");
                    break;
                case '^':
                    buff.append("|5e|");
                    break;
                default:
                    buff.append(c);
                }
            }
            fieldName = buff.toString();
        }
        return fieldName;
    }

    /**
     * Transforms a path into an _id compatible with Elasticsearch specification. The path cannot be larger than 512
     * bytes. For performance reasons paths that are already compatible are returned untouched. Otherwise, SHA-256
     * algorithm is used to return a transformed path (32 bytes max).
     *
     * @param path the document path
     * @return the Elasticsearch compatible path
     * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-id-field.html">Mapping _id field</a>
     */
    public static String idFromPath(@NotNull String path) {
        byte[] pathBytes = path.getBytes(StandardCharsets.UTF_8);
        if (pathBytes.length > 512) {
            return sha256Hash(pathBytes);
        }
        return path;
    }

    /**
     * Computes the SHA-256 hash of the given byte array and returns it as a UTF-8 string.
     *
     * @param input the byte array to hash
     * @return the SHA-256 hash as a string
     */
    public static String sha256Hash(byte[] input) {
        try {
            byte[] digest = MessageDigest.getInstance("SHA-256").digest(input);
            return StringUtils.convertBytesToHex(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * Computes a fast, deterministic hash of the given string for use in non-cryptographic
     * contexts like ES shard routing preference.
     *
     * @param input the string to hash
     * @return a hex string representation of the hash
     */
    public static String fastHash(String input) {
        // Use long to reduce collision probability compared to int hashCode
        long h = 0;
        for (int i = 0; i < input.length(); i++) {
            h = 31 * h + input.charAt(i);
        }
        return Long.toHexString(h);
    }

    /**
     * Converts a given byte array (of doubles) to a list of floats
     * @param array given byte array
     * @return list of floats
     */
    public static List<Float> toFloats(byte[] array) {
        int blockSize = Float.BYTES;
        ByteBuffer wrap = ByteBuffer.wrap(array);
        if (array.length % blockSize != 0) {
            LOG.warn("Unexpected byte array length {}", array.length);
        }
        int capacity = array.length / blockSize;
        List<Float> floats = new ArrayList<>(capacity);
        for (int i = 0; i < capacity; i++) {
            float e = wrap.getFloat(i * blockSize);
            floats.add(e);
        }
        return floats;
    }

    /**
     * Converts a given list of float values into a byte array
     * @param values given list of floats
     * @return byte array
     */
    public static byte[] toByteArray(List<Float> values) {
        byte[] bytes = new byte[values.size() * Float.BYTES];
        ByteBuffer wrap = ByteBuffer.wrap(bytes);
        for (int i = 0; i < values.size(); i++) {
            wrap.putFloat(values.get(i));
        }
        return bytes;
    }

}
