/*
 * Copyright 2018 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
 *
 *      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.gradle.internal.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class FilePathUtil {
    private static final String[] EMPTY_STRING_ARRAY = {};
    // On Windows, / and \ are separators, on Unix only / is a separator.
    private static final String FILE_PATH_SEPARATORS = File.separatorChar != '/' ? ("/" + File.separator) : File.separator;

    private FilePathUtil() {
    }

    public static String[] getPathSegments(String path) {
        StringTokenizer tokenizer = new StringTokenizer(path, FILE_PATH_SEPARATORS);
        List<String> segments = new ArrayList<String>();
        while (tokenizer.hasMoreElements()) {
            segments.add(tokenizer.nextToken());
        }
        return segments.toArray(EMPTY_STRING_ARRAY);
    }

    /**
     * Does not include the file separator.
     */
    public static int sizeOfCommonPrefix(String path1, String path2, int offset) {
        return sizeOfCommonPrefix(path1, path2, offset, File.separatorChar);
    }

    /**
     * Does not include the separator char.
     */
    public static int sizeOfCommonPrefix(String path1, String path2, int offset, char separatorChar) {
        int pos = 0;
        int lastSeparator = 0;
        int maxPos = Math.min(path1.length(), path2.length() - offset);
        for (; pos < maxPos; pos++) {
            if (path1.charAt(pos) != path2.charAt(pos + offset)) {
                break;
            }
            if (path1.charAt(pos) == separatorChar) {
                lastSeparator = pos;
            }
        }
        if (pos == maxPos) {
            if (path1.length() == path2.length() - offset) {
                return pos;
            }
            if (pos < path1.length() && path1.charAt(pos) == separatorChar) {
                return pos;
            }
            if (pos < path2.length() - offset && path2.charAt(pos + offset) == separatorChar) {
                return pos;
            }
        }
        return lastSeparator;
    }

    /**
     * Removes the trailing segments from the given path if it ends with the specified path to remove.
     */
    public static String maybeRemoveTrailingSegments(String path, String pathToRemove) {
        String[] removalSegments = getPathSegments(pathToRemove);
        String[] pathSegments = getPathSegments(path);
        int potentialRemovalIndex = pathSegments.length - removalSegments.length;
        // Check if the path ends with the removal path.
        if (potentialRemovalIndex > 0) {
            String[] lastSegments = Arrays.stream(pathSegments)
                .skip(potentialRemovalIndex)
                .toArray(String[]::new);

            if (Arrays.equals(lastSegments, removalSegments)) {
                // If it does, we remove the segments from the path
                // i.e: <rootPath>/<removalPath> becomes just <rootPath>
                String maybeLeadingSeparator = FILE_PATH_SEPARATORS.contains(path.substring(0, 1)) ? "/" : "";
                return maybeLeadingSeparator + String.join("/", Arrays.copyOf(pathSegments, potentialRemovalIndex));
            }
        }
        // Otherwise, we return the original path.
        return path;
    }
}
