// Copyright 2015 Eivind Vegsundvåg
//
// 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 ninja.eivind.hotsreplayuploader.files;

import ninja.eivind.hotsreplayuploader.utils.StormHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

@Singleton
public class AccountDirectoryWatcher {

    private static final Logger LOG = LoggerFactory.getLogger(AccountDirectoryWatcher.class);
    private final Set<File> watchDirectories;
    private StormHandler stormHandler;
    private Set<WatchHandler> watchHandlers = new HashSet<>();

    @Inject
    public AccountDirectoryWatcher(StormHandler stormHandler) {
        this.stormHandler = stormHandler;
        watchDirectories = new HashSet<>(stormHandler.getHotSAccountDirectories());
        beginWatch();
    }

    public void beginWatch() {
        LOG.info("Initiating watch against directories:");
        watchDirectories.stream().map(file -> Paths.get(file.toString())).forEach(path -> {
            try {
                LOG.info("\t" + path);
                WatchHandler watchHandler = new WatchHandler(stormHandler, path);
                watchHandlers.add(watchHandler);
                new Thread(watchHandler).start();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        LOG.info("Watcher initiated.");
    }

    public Stream<File> getAllFiles() {
        return watchDirectories.stream();
    }

    public void addFileListener(FileListener fileListener) {
        LOG.info("Adding listener " + fileListener.getClass());
        for (final WatchHandler watchHandler : watchHandlers) {
            watchHandler.addListener(fileListener);
        }
    }
}

@@ -885,16 +885,45 @@
 .Paths;%0A
+import java.util.Collection;%0A
 import j
@@ -936,24 +936,24 @@
 il.HashSet;%0A
-
 import java.
@@ -1287,16 +1287,56 @@
 Set%3C%3E();
+%0A    private Collection%3CThread%3E threads;
 %0A%0A    @I
@@ -1527,24 +1527,59 @@
 ctories());%0A
+        threads = new HashSet%3C%3E();%0A
         begi
@@ -1980,16 +1980,32 @@
         
+ Thread thread =
  new Thr
@@ -2025,15 +2025,76 @@
 ler)
-.start(
+;%0A                thread.start();%0A                threads.add(thread
 );%0A 
@@ -2576,24 +2576,24 @@
 eListener);%0A
-
         %7D%0A  
@@ -2595,11 +2595,172 @@
  %7D%0A    %7D
+%0A%0A    public void stop() %7B%0A        threads.stream()%0A                .filter(thread -%3E !thread.isInterrupted())%0A                .forEach(Thread::interrupt);%0A    %7D
 %0A%7D%0A

allow account directory watcher to cancel its running threads