/*
 * Copyright 2020 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.watch.registry.impl;

import com.google.common.collect.ImmutableList;
import org.gradle.internal.file.FileType;
import org.gradle.internal.snapshot.CompleteFileSystemLocationSnapshot;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

public class WatchRootUtil {
    /**
     * Filters out directories whose ancestor is also among the watched directories.
     */
    public static Set<Path> resolveRootsToWatch(Set<Path> directories) {
        Set<Path> roots = new HashSet<>();
        directories.stream()
            .sorted(Comparator.comparingInt(Path::getNameCount))
            .filter(path -> {
                Path parent = path;
                while (true) {
                    parent = parent.getParent();
                    if (parent == null) {
                        break;
                    }
                    if (roots.contains(parent)) {
                        return false;
                    }
                }
                return true;
            })
            .forEach(roots::add);
        return roots;
    }

    /**
     * Resolves the directories to watch for a snapshot.
     *
     * The directories to watch are
     * - root for a complete directory snapshot
     * - parent dir for complete snapshots
     * - the first existing parent directory for a missing file snapshot
     */
    public static ImmutableList<Path> getDirectoriesToWatch(CompleteFileSystemLocationSnapshot snapshot) {
        Path path = Paths.get(snapshot.getAbsolutePath());

        // For existing files and directories we watch the parent directory,
        // so we learn if the entry itself disappears or gets modified.
        // In case of a missing file we need to find the closest existing
        // ancestor to watch so we can learn if the missing file respawns.
        Path ancestorToWatch;
        switch (snapshot.getType()) {
            case RegularFile:
            case Directory:
                ancestorToWatch = path.getParent();
                break;
            case Missing:
                ancestorToWatch = findFirstExistingAncestor(path);
                break;
            default:
                throw new AssertionError();
        }
        if (ancestorToWatch == null) {
            return ImmutableList.of(path);
        }
        return snapshot.getType() == FileType.Directory
            ? ImmutableList.of(ancestorToWatch, path)
            : ImmutableList.of(ancestorToWatch);

    }

    private static Path findFirstExistingAncestor(Path path) {
        Path candidate = path;
        while (true) {
            candidate = candidate.getParent();
            if (candidate == null) {
                // TODO Can this happen on Windows when a SUBST'd drive is unregistered?
                throw new IllegalStateException("Couldn't find existing ancestor for " + path);
            }
            // TODO Use the VFS to find the ancestor instead
            if (Files.isDirectory(candidate)) {
                return candidate;
            }
        }
    }
}

@@ -712,50 +712,8 @@
 st;%0A
-import org.gradle.internal.file.FileType;%0A
 impo
@@ -2617,97 +2617,53 @@
     
-case Directory:%0A                ancestorToWatch = path.getParent();%0A                break
+    return ImmutableList.of(path.getParent())
 ;%0A  
@@ -2681,15 +2681,17 @@
 ase 
-Missing
+Directory
 :%0A  
@@ -2726,128 +2726,150 @@
 h = 
-findFirstExistingAncestor(path);%0A                break;%0A            default:%0A                throw new AssertionError();
+path.getParent();%0A                // If the path already is the root (e.g. C:%5C on Windows),%0A                // then we can't watch its parent.
 %0A   
@@ -2873,26 +2873,24 @@
         
-%7D%0A
         
 if (ance
@@ -2881,20 +2881,23 @@
         
-if (
+return 
 ancestor
@@ -2903,36 +2903,35 @@
 rToWatch == null
-) %7B%0A
+%0A  
             retu
@@ -2918,38 +2918,39 @@
 l%0A              
-return
+      ?
  ImmutableList.o
@@ -2952,25 +2952,24 @@
 ist.of(path)
-;
 %0A        %7D%0A 
@@ -2965,26 +2965,24 @@
         
-%7D%0A
         
 return s
@@ -2977,69 +2977,13 @@
     
-return snapshot.getType() == FileType.Directory%0A            ?
+    :
  Imm
@@ -3014,24 +3014,25 @@
 Watch, path)
+;
 %0A           
@@ -3036,45 +3036,170 @@
     
-: ImmutableList.of(ancestorToWatch);%0A
+case Missing:%0A                return ImmutableList.of(findFirstExistingAncestor(path));%0A            default:%0A                throw new AssertionError();%0A        %7D
 %0A   

Simplify code in `WatchRootUtil`