import bpy
from .utils_icons import getPreviewIcon
from .lightmapping.lightmapping import drawLightmapSettings, getLastBakeDuration, getLightmapPreview, getLightmapResolutionSetting, hasLightmappingEnabled

def drawLightmapOptions(layout, context):

    previewEnabled = getLightmapPreview()
    drawLightmapSettings(layout, context)
    lightmapOptions = layout.column()
    lightmapOptions.enabled = previewEnabled or (getattr(context.space_data, "shading", None) != None and context.space_data.shading.type == "RENDERED")
    selectedLightmapResolution = getLightmapResolutionSetting()

    bakeOptions = lightmapOptions.column()
    # if bpy.context.mode != "OBJECT":
    #     bakeOptions.enabled = False
    #     bakeOptions.label(text="Enable Object Mode for Lightmap baking")

    lightmapButtonLayout = bakeOptions.row(align=True)
    lightmapButtonLayout.scale_y = 2
    lightmapAll = lightmapButtonLayout.operator("needle.bake_lightmaps", text="Lightmap Objects")
    lightmapAll.resolution = selectedLightmapResolution
    lightmapAll.view_only = False
    lightmapAll.selection_only = False


    bakeOptions.prop(context.scene, "NEEDLE_lightmap_experimental_options", text="Show Experimental Options")
    if context.scene.NEEDLE_lightmap_experimental_options:
        lightmapCol = bakeOptions.split(factor=0.1)
        lightmapCol.column()
        rightCol = lightmapCol.column()
        lightmapViewOnly = rightCol.operator("needle.bake_lightmaps", text="Lightmap in View")
        lightmapViewOnly.resolution = selectedLightmapResolution
        lightmapViewOnly.view_only = True
        lightmapViewOnly.selection_only = False
        lightmapSelected = rightCol.operator("needle.bake_lightmaps", text="Lightmap Selected")
        lightmapSelected.resolution = selectedLightmapResolution
        lightmapSelected.selection_only = True
        lightmapSelected.view_only = False
        
        rightCol.operator("needle.apply_lightmap_uvs_and_remove_meta")

    lastBakeDuration = getLastBakeDuration()
    if lastBakeDuration > 0:
        # round to 2 decimal places
        lastBakeDuration = round(lastBakeDuration, 2)
        lightmapOptions.label(text="Last Bake Duration: " + str(lastBakeDuration) + " seconds")


def register():
    bpy.types.Scene.NEEDLE_lightmap_experimental_options = bpy.props.BoolProperty(name="Experimental Options", default=False)

def unregister():
    del bpy.types.Scene.NEEDLE_lightmap_experimental_options

class LightmappingPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_idname = "OBJECT_PT_NEEDLE_lightmapping"
    bl_label = "Needle Lightmapping"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "render"

    def draw_header(self, context):
        layout = self.layout
        icon = getPreviewIcon("logo_small_bw")
        layout.label(text="", icon_value=icon)

    def draw(self, context):
        layout = self.layout
        drawLightmapOptions(layout, context)




class Viewport_PT_LightmappingPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_idname = "OBJECT_PT_NEEDLE_lightmapping_viewport"
    bl_label = "Lightmapping"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Needle Engine"
    # bl_context = "object"

    def draw_header(self, context):
        layout = self.layout
        icon = getPreviewIcon("logo_small_bw")
        layout.label(text="", icon_value=icon)

    def draw(self, context):
        layout = self.layout

        activeObject = context.active_object
        if activeObject:
            column = layout.column()
            # box.label(text=activeObject.name, icon="OBJECT_DATA")
            label = hasLightmappingEnabled(activeObject) and "Lightmapped" or "Not Lightmapped"
            label = activeObject.name + ": " + label
            column.prop(activeObject, "NEEDLE_isLightmapped", text=label, icon="LIGHT")
            objOptions = column.column()
            objOptions.enabled = hasLightmappingEnabled(activeObject)
            if activeObject.type == "MESH":
                objOptions.prop(activeObject, "NEEDLE_lightmapScale", text="Scale in Lightmap")
            column.separator()

        layout = layout.box()
        drawLightmapOptions(layout, context)


        