package com.metacodestudio.hotsuploader.files;

import javafx.collections.ObservableList;

import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Map;
import java.util.Queue;

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.OVERFLOW;

public class WatchHandler implements Runnable {

    private WatchService watchService;
    private Map<Status, ObservableList<ReplayFile>> fileMap;
    private Queue<ReplayFile> uploadQueue;

    public WatchHandler(final WatchService watchService, final Map<Status, ObservableList<ReplayFile>> fileMap, final Queue<ReplayFile> uploadQueue) {
        this.watchService = watchService;
        this.fileMap = fileMap;
        this.uploadQueue = uploadQueue;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void run() {
        for (; ; ) {
            WatchKey key;
            try {
                key = watchService.take();
            } catch (InterruptedException e) {
                return;
            }

            for (final WatchEvent<?> watchEvent : key.pollEvents()) {
                WatchEvent.Kind<?> kind = watchEvent.kind();
                if (kind == OVERFLOW || kind != ENTRY_CREATE) {
                    continue;
                }
                WatchEvent<Path> event = (WatchEvent<Path>) watchEvent;
                final Path fileName = event.context();

                final Path parent = fileName.getParent();
                if(!parent.endsWith("Multiplayer")) {
                    System.err.println("Improperly initialized");
                    break;
                }

                ReplayFile replayFile = new ReplayFile(fileName.toFile());

                fileMap.get(Status.NEW).add(replayFile);
                uploadQueue.add(replayFile);
            }

            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }
    }

}

@@ -41,16 +41,52 @@
 files;%0A%0A
+import javafx.application.Platform;%0A
 import j
@@ -120,16 +120,37 @@
 eList;%0A%0A
+import java.io.File;%0A
 import j
@@ -1566,25 +1566,17 @@
     
-final Path parent
+File file
  = f
@@ -1587,17 +1587,14 @@
 ame.
-getParent
+toFile
 ();%0A
@@ -1617,14 +1617,22 @@
 if(!
-parent
+file.getName()
 .end
@@ -1642,25 +1642,158 @@
 th(%22
-Multi
+.StormRe
 play
-er
 %22)) %7B%0A
+                    continue;%0A                %7D%0A%0A                long stamp = 0;%0A                try %7B%0A                    do %7B%0A    
     
@@ -1815,19 +1815,19 @@
  System.
-err
+out
 .println
@@ -1832,34 +1832,80 @@
 ln(%22
-Improperly initialized%22
+Waiting%22);%0A                        System.out.println(file.getName()
 );%0A
+%0A
     
@@ -1924,125 +1924,374 @@
     
-break;%0A                %7D%0A%0A                ReplayFile replayFile = new ReplayFile(fileName.toFile());%0A%0A               
+    Thread.sleep(10000L);%0A                        System.out.println(stamp);%0A                        System.out.println(System.currentTimeMillis() - 20000L);%0A                    %7D while ((stamp = file.lastModified()) %3E System.currentTimeMillis() - 20000L);%0A                    ReplayFile replayFile = new ReplayFile(file);%0A                    Platform.runLater(() -%3E
  fil
@@ -2318,32 +2318,33 @@
 .add(replayFile)
+)
 ;%0A              
@@ -2345,16 +2345,20 @@
         
+  
+  
 uploadQu
@@ -2382,35 +2382,121 @@
 e);%0A            
-%7D%0A%0A
+    %7D catch (InterruptedException e) %7B%0A                    break;%0A                %7D%0A%0A    
             bool
@@ -2528,24 +2528,28 @@
             
+    
 if (!valid) 
@@ -2558,39 +2558,61 @@
                 
+  
+  
 break;%0A
+                %7D%0A
             %7D%0A  
@@ -2625,11 +2625,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

implement watch service(buggy but functional)