from .utils_system_requirements import hasSupportedNodeVersionInstalled
from .utils_auto_update import drawUpdateButton
from .__meta__ import getVersion
import bpy
from .utils import getLatestVersion, getMinSupportedBlenderVersionString
from .panels_project import drawProjectSettings
from .utils_icons import getPreviewIcon
from .utils_web_project import drawCompressionProgress, serverIsRunning

class NEEDLE_PT_PROJECT_SETTINGS_POPOVER(bpy.types.Panel):
    bl_label = "Needle Engine Project Settings"
    bl_idname = "NEEDLE_PT_PROJECT_SETTINGS_POPOVER"
    bl_description = "Configure your Needle Engine project here with just a few settings: Enable WebXR, networking, animation export or adjust rendering settings"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'HEADER'
    bl_ui_units_x = 20

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


def header_search_draw_tools(self, context):
    if not bpy.context.area.spaces.active.show_region_tool_header:
        return
    if bpy.context.mode in ("SCULPT", "PAINT_TEXTURE"):
        return
    layout = self.layout
    layout.label(text="Search Tools:")

def header_draw(self, context):
    layout = self.layout
    # layout.separator_spacer()
    # header_search_draw_tools(self, context)
    # layout.separator_spacer()

def header_editor_menu(self, context):
    layout = self.layout
    if not hasSupportedNodeVersionInstalled():
        return

    layout.separator_spacer()
    # icon
    icon = getPreviewIcon("logo")
    layout.popover("NEEDLE_PT_PROJECT_SETTINGS_POPOVER", text="", icon_value=icon)
    
    latestVersion = getLatestVersion()
    currentVersion = getVersion()
    if latestVersion != currentVersion:
        drawUpdateButton(layout, latestVersion, minimal=True, checkUpdateOnly=True)
        
    # layout.progress(text='', text_ctxt='', translate=True, factor=0.5, type='RING')
    playLayout = layout.row(align=True)
    stopLayoutWidth = 1.1;
    playLayout.ui_units_x = 3.4
    playLayout.scale_y = 1
    if not serverIsRunning():
        playLayout.ui_units_x += stopLayoutWidth
        playLayout.operator("needle.generate_project", text="Start", icon='PLAY', depress=True)
    else:
        playLayout.operator("needle_scene.export", text="Export", icon='PASTEDOWN', depress=True)
        stopLayout = layout.row(align=True)
        stopLayout.alert = True
        stopLayout.scale_x = stopLayoutWidth
        stopLayout.operator("needle.stop_server", text="", icon='SNAP_FACE')
    
    exportButtonLayout = layout.row(align=True)
    exportButtonLayout.ui_units_x = 3
    exportButtonLayout.scale_y = 1
    drawCompressionProgress(exportButtonLayout, text="")
    
    # layout.separator_spacer()

# def object_context_draw(self, context):
#     layout = self.layout
#     layout.separator_spacer()
#     layout.label(text="Hi Needle Object")
#     layout.separator_spacer()


def register():
    bpy.types.VIEW3D_HT_tool_header.append(header_draw)
    bpy.types.VIEW3D_MT_editor_menus.append(header_editor_menu)
    # bpy.types.VIEW3D_MT_object_context_menu.append(object_context_draw)
    pass

def unregister():
    bpy.types.VIEW3D_HT_tool_header.remove(header_draw)
    bpy.types.VIEW3D_MT_editor_menus.remove(header_editor_menu)
    # bpy.types.VIEW3D_MT_object_context_menu.remove(object_context_draw)
    pass