# coding=utf-8
__author__ = "Vivek Anand <vivekanand1101@gmail.com>"
__author__ = "Daniel Arroyo. 3DaGogo, Inc <daniel@astroprint.com>"
__author__ = "Gina Häußge <osd@foosel.net>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'

import os
import logging


from astroprint.printer.manager import printerManager
from astroprint.printfiles.map import SUPPORTED_EXTENSIONS

from watchdog.events import PatternMatchingEventHandler
from watchdog.events import FileSystemEventHandler

from octoprint.settings import settings


class UploadCleanupWatchdogHandler(PatternMatchingEventHandler):
    """
    Takes care of automatically deleting metadata entries for files
    that get deleted from the uploads folder
    """

    patterns = map(lambda x: "*.%s" % x, SUPPORTED_EXTENSIONS)

    def __init__(self):
        PatternMatchingEventHandler.__init__(self)

    def on_deleted(self, event):
        fm = printerManager().fileManager
        filename = fm._getBasicFilename(event.src_path)
        if not filename:
            return

        fm.removeFileFromMetadata(filename)


def _get_gcode_files(directory):
    """ Get gcode files in the dir """
    allfiles = []
    for parent, d, pack in os.walk(directory):
        for files in pack:
            curr_file = {}
            if files.endswith('.gcode'):
                curr_file['fullpath'] = os.path.abspath(os.path.join(parent, files))
                curr_file['filename'] = files
                allfiles.append(curr_file)
    return allfiles


class EtherBoxHandler(FileSystemEventHandler):
    ''' Watch for USB insertion for print from storage feature '''

    def on_created(self, event):
        ''' Called when the media is inserted '''
        usb_path = event.src_path
        gcode_files = _get_gcode_files(usb_path)
        if not gcode_files:
            return
        s = settings()
        s.set(['usb', 'filelist'], gcode_files)

    def on_deleted(self, event):
        s = settings()
        s.set(['usb', 'filelist'], [])

@@ -1653,16 +1653,346 @@
 re '''%0A%0A
+    def __init(self, *args, **kwargs):%0A        ''' Initiate the object '''%0A        super(EtherBoxHandler).__init__(self, *args, **kwargs)%0A        self._callbacks = %5B%5D%0A%0A    def registerCallback(self, callback):%0A        ''' Register the sockjs method to send data about usb to frontend '''%0A        self._callbacks.append(callback)%0A%0A
     def 
@@ -2008,32 +2008,32 @@
 d(self, event):%0A
-
         ''' Call
@@ -2196,16 +2196,114 @@
  return%0A
+        for callback in self._callbacks:%0A            callback.sendEvent(%22usb_inserted%22, usb_path)%0A
         
@@ -2414,32 +2414,32 @@
  s = settings()%0A
-
         s.set(%5B'
@@ -2461,8 +2461,106 @@
 '%5D, %5B%5D)%0A
+%0A        for callback in self._callbacks:%0A            callback.sendEvent(%22usb_removed%22, usb_path)%0A

Send event to frontend when usb is inserted/removed