package de.idrinth.waraddonclient.service;

import de.idrinth.waraddonclient.service.logger.BaseLogger;
import de.idrinth.waraddonclient.model.GuiAddonList;
import java.io.File;
import java.io.IOException;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Arrays;

public class FileWatcher implements java.lang.Runnable {

    private final GuiAddonList addonList;

    private final WatchService watcher;

    private final BaseLogger logger;
    
    private final Config config;

    public FileWatcher(GuiAddonList addonList, BaseLogger logger, Config config) throws IOException {
        this.addonList = addonList;
        this.logger = logger;
        this.config = config;
        File path = new File(config.getLogsFolder());
        if (!path.exists()) {
            path.mkdirs();
        }
        watcher = path.toPath().getFileSystem().newWatchService();
        path.toPath()
        .register(
            watcher,
            Arrays.asList(
                    StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY)
                .toArray(new WatchEvent.Kind<?>[] {}));
    }

    @Override
    public void run() {
        try {
            WatchKey key = watcher.take();
            key.pollEvents().stream().filter(this::isValidEvent).map(event -> new File(config.getLogsFolder() + event.context().toString())).filter(this::isValidFile).forEach(file -> addonList.getWatchedFiles().get(file.getName().toLowerCase()).setFileToProcess(file));
            key.reset();
        } catch (InterruptedException ex) {
            logger.error(ex);
        }
    }

    /**
     * is this an event to process?
     *
     * @param event
     * @return
     */
    private boolean isValidEvent(WatchEvent<?> event) {
        return event.kind() == StandardWatchEventKinds.ENTRY_CREATE || event.kind() == StandardWatchEventKinds.ENTRY_MODIFY;
    }

    /**
     * is this a file to process?
     *
     * @param file
     * @return
     */
    private boolean isValidFile(java.io.File file) {
        return addonList.getWatchedFiles().containsKey(file.getName().toLowerCase());
    }
}

@@ -347,33 +347,8 @@
 ice;
-%0Aimport java.util.Arrays;
 %0A%0Apu
@@ -970,25 +970,16 @@
 toPath()
-%0A        
 .registe
@@ -984,76 +984,16 @@
 ter(
-%0A            watcher,%0A            Arrays.asList(%0A                   
+watcher,
  Sta
@@ -1068,63 +1068,8 @@
 IFY)
-%0A                .toArray(new WatchEvent.Kind%3C?%3E%5B%5D %7B%7D))
 ;%0A  

Replace Arrays#AsList#toArray with varargs