
from .material_utils import tryFindImage
from ..image_types import ImageSettings, getImageSettings
from ..settings_scene import tryGetMainSceneSettings
from ..utils import generateIdFromString
from .extensionbase import ExtensionBase

EXTENSION_NAME = "NEEDLE_compression_texture"

class NEEDLE_texture_compression_EXT(ExtensionBase):

    def __init__(self):
        self.didAddExtension = False
        pass


    def gather_texture_hook(self, gltf2_texture, blender_shader_sockets, export_settings):
        # get image
        source = gltf2_texture.source
        if source is None:
            return
        res = tryFindImage(blender_shader_sockets, source.name)
        if res is None:
            return
        (blender_image, slot) = res
        if blender_image is None:
            return
        # get image settings
        settings = getImageSettings(blender_image)
        # if we dont have settings overriden
        if settings is None:
            settings = ImageSettings()
        
        # translate settings for extension
        mode = settings.compression
        if mode == "AUTO":
            mode = "auto"
        elif mode == "NONE":
            mode = "none"
        elif mode == "WEBP":
            mode = "webp"

        gltf2_texture.extensions[EXTENSION_NAME] = { 
            "mode": mode,
            "quality": settings.quality,
            "maxSize": int(settings.maxsize),
            "slot": slot,
        }
        self.didAddExtension = True
    
    
    def gather_gltf_extensions_hook(self, gltf2_object, export_settings):
        if self.didAddExtension:
            gltf2_object.extensions_used.append(EXTENSION_NAME)