/*
 * 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 net.rubygrapefruit.platform.NativeIntegrationUnavailableException;
import net.rubygrapefruit.platform.file.FileEvents;
import net.rubygrapefruit.platform.file.FileWatchEvent;
import net.rubygrapefruit.platform.internal.jni.LinuxFileEventFunctions;
import net.rubygrapefruit.platform.internal.jni.LinuxFileEventFunctions.LinuxFileWatcher;
import org.gradle.internal.snapshot.SnapshotHierarchy;
import org.gradle.internal.watch.registry.FileWatcherProbeRegistry;
import org.gradle.internal.watch.registry.FileWatcherUpdater;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.function.Predicate;

public class LinuxFileWatcherRegistryFactory extends AbstractFileWatcherRegistryFactory<LinuxFileEventFunctions, LinuxFileWatcher> {

    public LinuxFileWatcherRegistryFactory(Predicate<String> watchFilter) throws NativeIntegrationUnavailableException {
        super(FileEvents.get(LinuxFileEventFunctions.class), watchFilter);
    }

    @Override
    protected LinuxFileWatcher createFileWatcher(BlockingQueue<FileWatchEvent> fileEvents) throws InterruptedException {
        return fileEventFunctions.newWatcher(fileEvents)
            .start();
    }

    @Override
    protected FileWatcherUpdater createFileWatcherUpdater(LinuxFileWatcher watcher, FileWatcherProbeRegistry probeRegistry, WatchableHierarchies watchableHierarchies) {
        return new NonHierarchicalFileWatcherUpdater(watcher, probeRegistry, watchableHierarchies, new LinuxMovedWatchedDirectoriesSupplier(watcher, watchableHierarchies));
    }

    private static class LinuxMovedWatchedDirectoriesSupplier implements AbstractFileWatcherUpdater.MovedWatchedDirectoriesSupplier {
        private final LinuxFileWatcher watcher;
        private final WatchableHierarchies watchableHierarchies;

        public LinuxMovedWatchedDirectoriesSupplier(LinuxFileWatcher watcher, WatchableHierarchies watchableHierarchies) {
            this.watcher = watcher;
            this.watchableHierarchies = watchableHierarchies;
        }

        @Override
        public Collection<File> stopWatchingMovedPaths(SnapshotHierarchy vfsRoot) {
            List<File> pathsToCheck = new ArrayList<>();
            vfsRoot.rootSnapshots().forEach(snapshot -> {
                if (watchableHierarchies.shouldWatch(snapshot)) {
                    switch (snapshot.getType()) {
                        case RegularFile:
                            pathsToCheck.add(new File(snapshot.getAbsolutePath()).getParentFile());
                            break;
                        case Directory:
                            pathsToCheck.add(new File(snapshot.getAbsolutePath()));
                            break;
                        case Missing:
                            // Ignore
                            break;
                        default:
                            throw new IllegalArgumentException("Unexpected file type:" + snapshot.getType());
                    }
                }
            });
            return watcher.stopWatchingMovedPaths(pathsToCheck);
        }
    }
}

@@ -1005,16 +1005,58 @@
 atcher;%0A
+import org.gradle.internal.file.FileType;%0A
 import o
@@ -1275,69 +1275,18 @@
 til.
-ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List
+Collection
 ;%0Aim
@@ -1362,16 +1362,52 @@
 edicate;
+%0Aimport java.util.stream.Collectors;
 %0A%0Apublic
@@ -2915,20 +2915,26 @@
         
-List
+Collection
 %3CFile%3E p
@@ -2951,84 +2951,107 @@
 k = 
-new ArrayList%3C%3E();%0A            vfsRoot.rootSnapshots().forEach(snapshot -%3E %7B
+vfsRoot.rootSnapshots()%0A                .filter(snapshot -%3E snapshot.getType() != FileType.Missing)
 %0A   
@@ -3067,11 +3067,15 @@
     
-if 
+.filter
 (wat
@@ -3091,17 +3091,18 @@
 rarchies
-.
+::
 shouldWa
@@ -3104,16 +3104,38 @@
 uldWatch
+)%0A                .map
 (snapsho
@@ -3135,18 +3135,19 @@
 snapshot
-))
+ -%3E
  %7B%0A     
@@ -3261,33 +3261,23 @@
         
-pathsToCheck.add(
+return 
 new File
@@ -3324,44 +3324,8 @@
 le()
-);%0A                            break
 ;%0A  
@@ -3394,25 +3394,15 @@
     
-pathsToCheck.add(
+return 
 new 
@@ -3437,155 +3437,8 @@
 h())
-);%0A                            break;%0A                        case Missing:%0A                            // Ignore%0A                            break
 ;%0A  
@@ -3609,32 +3609,33 @@
                %7D
+)
 %0A            %7D);
@@ -3631,17 +3631,48 @@
         
-%7D
+    .collect(Collectors.toList()
 );%0A     

Use more streams in LinuxFileWatcherRegistryFactory