import bpy
from bpy.app.handlers import persistent
from .component_watcher import startFileWatcher
from .component_types import generateComponentTypesFromSchemes

lastLoadTime = 0

@persistent
def afterLoadScene(dummy, dummy2):
    global lastLoadTime
    import time
    now = time.time()
    if now - lastLoadTime < 2:
        print("Already loaded")
        return
    lastLoadTime = now
    print("Loaded scene")
    generateComponentTypesFromSchemes()
    # bpy.app.timers.register(startFileWatcher, first_interval=.5)

def register():
    if not afterLoadScene in bpy.app.handlers.load_post:
        bpy.app.handlers.load_post.append(afterLoadScene)

def unregister():
    if afterLoadScene in bpy.app.handlers.load_post:
        bpy.app.handlers.load_post.remove(afterLoadScene)


# start file watcher
bpy.app.timers.register(startFileWatcher, first_interval=.5)