from .utils import showMessage
from .component_watcher import scheduleRegenerateTypes, startFileWatcher
from .blender_dialogue import openDialogue
import bpy
from .component_utils import addComponent, createNewComponent
from .panels_components import setIsActive
from .blender_export import exportScene

class op_createNewComponent(bpy.types.Operator):
    bl_idname = 'needle.create_new_component'
    bl_label = 'Create New Component'
    bl_options = {"REGISTER", "UNDO"}
    bl_description = "Creates a new component type (which will then be assigneable to any object in your scene to setup data on your glTF node)"
    # as per documentation this should focus the field by default but it does not
    bl_property = "new_component_name"
    
    new_component_name: bpy.props.StringProperty(name="New Component Name", default="My New Component")

    def execute(self, context):
        openDialogue({
            "title": "Create New Component",
            "draw": self.drawDialogue,
            "accept": self.onCreateNewComponent,
            "cancel": None,
        })
        return {"FINISHED"}

    def drawDialogue(self, layout, context):
        layout.prop(self, "new_component_name") 
        pass
    
    def onCreateNewComponent(self):
        if self.new_component_name == "":
            showMessage("Please enter a name for the new component", title="Missing Name")
            return
        createNewComponent(self.new_component_name)
        startFileWatcher()
        scheduleRegenerateTypes()

class op_addNewComponent(bpy.types.Operator):
    bl_idname = 'needle.addcomponent'
    bl_label = 'Add Component'
    bl_options = {"REGISTER", "UNDO"}
    bl_description = "Adds a new component to the current object"

    name: bpy.props.StringProperty(name="Key", default="")
    displayName: bpy.props.StringProperty(name="DisplayName", default="")

    def execute(self, context):
        addComponent(context, self.name)
        startFileWatcher()
        # setIsActive(getattr(context.object, getComponentKey(self.name)), True)
        return {"FINISHED"} 


class op_RemoveComponent(bpy.types.Operator):
    bl_idname = 'needle.removecomponent'
    bl_label = 'Remove Component'
    bl_options = {"REGISTER", "UNDO"}
    bl_description = "Removes a component from the current object"

    key: bpy.props.StringProperty(name="Key", default="")
    displayName: bpy.props.StringProperty(name="Name", default="")

    def execute(self, context):
        selected_objects = context.selected_objects
        for obj in selected_objects:
            comp = getattr(obj, self.key)
            setIsActive(comp, False)
        context.area.tag_redraw()
        return {"FINISHED"}


# Export operator
class exportSceneOp(bpy.types.Operator):
    bl_idname = 'needle_scene.export'
    bl_label = 'Export'
    bl_options = {"REGISTER"}
    bl_description = "Exports the scene as a GLB file for the web (for e.g. preview in the local server)"

    immediate: bpy.props.BoolProperty(name="Immediate", default=False)

    def execute(self, context):
        if self.immediate:
            exportScene(self)
        else:
            bpy.app.timers.register(lambda: exportScene(self))
        return {"FINISHED"}



# def register():
#     try:
#         bpy.utils.register_class(op_addNewComponent)
#     except ValueError:
#         pass

#     try:
#         bpy.utils.register_class(op_RemoveComponent)
#     except ValueError:
#         pass
#     # bpy.utils.register_class(exportSceneOp)

# def unregister(): 
#     bpy.utils.unregister_class(op_addNewComponent)
#     bpy.utils.unregister_class(op_RemoveComponent)
#     # bpy.utils.unregister_class(exportSceneOp)