
from ..settings_scene import tryGetMainSceneSettings
from ..utils import generateIdFromString
from .extensionbase import ExtensionBase



# TODO: use texture settings


class NEEDLE_progressive_texture_settings(ExtensionBase):

    def __init__(self):
        self.settings = tryGetMainSceneSettings()
        self.didAddProgressiveTextures = False
        self.didAddProgressiveMeshes = False

    def gather_mesh_hook(self, gltf2_mesh, blender_mesh, blender_object, vertex_groups, modifiers, materials, export_settings):
        if self.settings.useProgressiveMeshes == False:
            guid = str(id(blender_mesh))
            self.didAddProgressiveMeshes = True
            gltf2_mesh.extensions['NEEDLE_progressive_mesh_settings'] = { 
                "guid" : guid,
                "generateLods" : False,
            }
        pass

    def gather_texture_hook(self, gltf2_texture, blender_shader_sockets, export_settings):

        if self.settings.useProgressive:

            source = gltf2_texture.source

            if source is None:
                return

            # dont add extension to lightmap or skybox
            if source.name.endswith(".exr"):
                return

            if source.name.startswith("Lightmap-"):
                return
            
            # assign the name of the source if the texture doesnt have a name
            # this is just for debugging - we could move it behind a flag
            if not gltf2_texture.name:
                gltf2_texture.name = source.name

            self.didAddProgressiveTextures = True
            guid = str(id(source))

            selectedDefaultSize = self.settings.progressiveTextureSize

            gltf2_texture.extensions['NEEDLE_progressive_texture_settings'] = { 
                "maxSize" : int(selectedDefaultSize),
                "guid" : guid
            }
    
    def gather_gltf_extensions_hook(self, gltf2_object, export_settings):
        if self.didAddProgressiveTextures:
            gltf2_object.extensions_used.append("NEEDLE_progressive_texture_settings")
        if self.didAddProgressiveMeshes:
            gltf2_object.extensions_used.append("NEEDLE_progressive_mesh_settings")