import bpy
from .lightmapping.lightmapping_pack import applyLightmapUVsAndRemoveMeta
from .lightmapping.lightmapping_common import LightmapBakeContext
from .lightmapping.lightmapping import bakeLightmaps, getOrCreateLightmapNodeGroup

class bakeLightmapOp(bpy.types.Operator):
    bl_idname = "needle.bake_lightmaps"
    bl_label = "Bake Lightmaps"
    bl_options = {"REGISTER"}
    bl_description = "Bake lightmaps for the scene. Running this will generate a lightmap UVs for all objects that have lightmapping enabled.\nUse \"Lightmap selected\" to only bake the lightmap for selected objects in the scene to quickly preview changes.\nFor final bakes it is recommended use Lightmap All."

    resolution: bpy.props.IntProperty(name="Resolution", default=1024)
    selection_only: bpy.props.BoolProperty(name="Selection Only", default=False)
    view_only: bpy.props.BoolProperty(name="View Only", default=False)
    
    def execute(self, context):
        size = self.resolution
        settings = LightmapBakeContext(size, size)
        settings.selection_only = self.selection_only
        settings.view_only = self.view_only
        bakeLightmaps(settings)
        # createLightmapNodeGroup()
        return {"FINISHED"}
    

class applyLightmapUVsAndRemoveMetaOp(bpy.types.Operator):
    bl_idname = "needle.apply_lightmap_uvs_and_remove_meta"
    bl_label = "Apply Lightmap UVs"
    bl_options = {"REGISTER"}
    bl_description = "Will apply the lightmap UVs of all lightmapped objects in the scene\nAfter running this operation you will not be able to use the lightmaps generated in Needle Engine.\nThis operation will modify the Lightmap_UV layer of all objects in the scene."

    def execute(self, context):
        applyLightmapUVsAndRemoveMeta()
        return {"FINISHED"}


