import bpy

TEXTURE_COMPRESSION_OPTIONS = [
    ("AUTO", "AUTO", "The build pipeline decides based on the slot the texture is used in what compression algorithm to use", 1),
    ("NONE", "NONE", "The texture will not be compressed during a production build and just kept in the glTF as is", 2),
    ("ETC1S", "ETC1S", "GPU Memory Usage: LOW\nFile Size: LOW\nQuality: MEDIUM\nWorks for everything but best for color textures", 3),
    ("UASTC", "UASTC", "GPU Memory Usage: LOW\nFile Size: HIGH\nQuality: VERY HIGH\nUse for high-detail data textures\nlike normal maps, roughness, metallic, etc", 4),
    ("WEBP", "WebP", "GPU Memory Usgae: HIGH (uncompressed)\nFile Size: VERY LOW\nQuality: Depends on Quality Setting\nUse where files with ETC1S quality is not\nsufficient but UASTC is too large", 5),
]

TEXTURE_MAX_SIZE_OPTIONS = [
    ("8096", "8096", "", 1),
    ("4096", "4096", "", 2),
    ("2048", "2048", "", 3),
    ("1024", "1024", "", 4),
    ("512", "512", "", 5),
    ("256", "256", "", 6),
    ("128", "128", "", 7),
    ("64", "64", "", 8),
]

PROGRESSIVE_TEXTURE_SIZE_OPTIONS = [
    ("4096", "4096", "", 2),
    ("2048", "2048", "", 3),
    ("1024", "1024", "", 4),
    ("512", "512", "", 5),
    ("256", "256", "", 6),
    ("128", "128", "", 7),
    ("64", "64", "", 8),
    ("32", "32", "", 9),
]

# these are actually on the image type because we dont want to add them to the material texture node ?
# not sure how to get them from a texture

class ImageSettings(bpy.types.PropertyGroup):
    enabled: bpy.props.BoolProperty(name="Override", default=False, description="Toggle ON to override the default compression settings for this texture")
    maxsize: bpy.props.EnumProperty(name="Max Size", items=TEXTURE_MAX_SIZE_OPTIONS, description="The texture will be reduced to this size if it exceeds the selected size here")
    compression: bpy.props.EnumProperty(name="Compression", items=TEXTURE_COMPRESSION_OPTIONS)
    quality: bpy.props.FloatProperty(name="Quality", default=0.8, min=0.0, max=1.0, subtype="FACTOR", description="Quality of the compressed texture. 0.0 is the lowest quality, 1.0 is the highest quality")

    def draw(self, layout, image : bpy.types.Image, slots:list):
        row = layout.split(factor=.45)
        row.alignment = "LEFT"
        row.use_property_decorate = True
        # image width
        width = image.size[0]
        height = image.size[1]
        enabledProp = row.prop(self, "enabled", text=image.name + " - " + str(width) + "x" + str(height))
        # enabledProp.description = "Override the default compression settings for this texture"
        if slots:
            disabledRow = row.row()
            disabledRow.enabled = False
            disabledRow.label(text=", ".join(slots))
        # row.label(text=image.name)
        layout = self.enabled and layout.box() or layout.row()
        if not self.enabled:
            return
        layout.prop(self, "maxsize")
        layout.prop(self, "compression")
        if self.compression == "WEBP":
            layout.prop(self, "quality")



# draw settings for an image
def drawImageSettings(self, image : bpy.types.Image, slots: list):
    layout = self.layout
    if image is None:
        return
    if image.NEEDLE_image_settings is None:
        return
    label = image.name
    image.NEEDLE_image_settings.draw(layout, image, slots)


def getImageSettings(image: bpy.types.Image) -> ImageSettings or None:
    if image is None:
        return None
    return image.NEEDLE_image_settings





def register():
    unregister()
    bpy.utils.register_class(ImageSettings)
    bpy.types.Image.NEEDLE_image_settings = bpy.props.PointerProperty(type=ImageSettings)
    pass

def unregister():
    try:
        bpy.utils.unregister_class(ImageSettings)
        del bpy.types.Image.NEEDLE_image_settings
    except:
        pass