import bpy
from ..blender_export import ExportContext

class ExtensionBase:

    def getExportSettings(self):
        return self.exportContext.exportSettings

    def getExtension(self, name:str):
        if hasattr(self, "importContext"):
            return self.importContext.getExtensionHandler(name)
        if hasattr(self, "exportContext"):
            return self.exportContext.getExtensionHandler(name)
        return None

    
    def onBeforeImport(self, importContext):
        self.importContext = importContext
        pass

    def onBeforeExport(self, exportContext:ExportContext):
        self.exportContext = exportContext
        self.animationHandler = exportContext.getExtensionHandler("animationhandler")
        pass

    def gather_node_hook(self, gltf2_node, blender_object, node):
        self.gltf2_node = gltf2_node
        self.blender_object = blender_object
        self.node = node
        pass

    # def gather_animation_hook(self, gltf2_animation, blender_action, blender_object, export_settings):
    #     self.exportContext.animationhandler.gather_animation_hook(gltf2_animation, blender_action, blender_object, export_settings)
    
    # this is specific to the ExtensionBase - we want to make sure we can register animations
    def registerAnimation(self, target, property, action, obj : bpy.types.Object = None):
        if obj is None:
            obj = self.blender_object
        self.animationHandler.registerAnimation(obj, target, property, action)
        pass

    def registerPeristentAsset(self, asset):
        ext = self.exportContext.getExtensionHandler("NEEDLE_persistent_assets")
        return ext.registerSerializedAsset(asset)
    


    def resolveJsonPointerImport(self, pointer) -> any:
        '''called during import when an /extension/... pointer is encountered'''
        pass


