package com.eactive.eai.rms.onl.common.util;

import java.io.File;
import java.util.StringJoiner;

public class FileSystemUtil {

    /**
     * Normalizes a path by joining segments and applying the correct file separator for the current operating system.
     * It handles redundant separators between segments.
     *
     * <p>Examples for Linux/macOS:</p>
     * <ul>
     *   <li>{@code pathNormalize("/asd1/asd2", "asd3")} returns {@code "/asd1/asd2/asd3"}</li>
     *   <li>{@code pathNormalize("/asd1/asd2/", "/asd3")} returns {@code "/asd1/asd2/asd3"}</li>
     * </ul>
     *
     * <p>Examples for Windows:</p>
     * <ul>
     *   <li>{@code pathNormalize("C:/Users/Test", "Documents")} returns {@code "C:\\Users\\Test\\Documents"}</li>
     *   <li>{@code pathNormalize("C:/Users/Test/", "/Documents")} returns {@code "C:\\Users\\Test\\Documents"}</li>
     * </ul>
     *
     * @param args A variable number of path segments to join.
     * @return A normalized path string for the current operating system.
     */
    public static String pathNormalize(String... args) {
        if (args == null || args.length == 0) {
            return "";
        }

        StringJoiner joiner = new StringJoiner(File.separator);
        for (String arg : args) {
            if (arg != null && !arg.isEmpty()) {
                String segment = arg;
                // Remove leading separators from the segment
                while (segment.startsWith("/") || segment.startsWith("\\")) {
                    segment = segment.substring(1);
                }
                // Remove trailing separators from the segment
                while (segment.endsWith("/") || segment.endsWith("\\")) {
                    segment = segment.substring(0, segment.length() - 1);
                }
                if (!segment.isEmpty()) {
                    joiner.add(segment);
                }
            }
        }

        String joinedPath = joiner.toString();

        // Handle the root path for the first argument
        String firstArg = args[0];
        if (firstArg != null && !firstArg.isEmpty()) {
            if (firstArg.startsWith("/")) {
                joinedPath = File.separator + joinedPath;
            } else if (firstArg.matches("^[a-zA-Z]:\\\\") || firstArg.matches("^[a-zA-Z]:/.*")) { // Handle Windows drive letters, e.g., "C:\" or "C:/"
                String drive = firstArg.substring(0, 2);
                return drive + File.separator + joinedPath.substring(joinedPath.indexOf(File.separator) + 1);
            }
        }
        
        // Final normalization for Windows if the path starts with a separator (e.g. from a UNC path)
        if (System.getProperty("os.name").toLowerCase().contains("win")) {
             if (args[0] != null && args[0].startsWith("//") || args[0].startsWith("\\\\")) {
                return "\\\\" + joinedPath;
             }
             return joinedPath.replace("/", "\\");
        }

        return joinedPath.replace("\\", "/");
    }
}
