from .utils_system_requirements import tryGetNodeVersion
from .__meta__ import getVersion
import bpy
from .utils_icons import getPreviewIcon

class OBJECT_MT_needle_object_utils(bpy.types.Menu):
    bl_label = "Needle"
    
    def draw(self, context):
        layout = self.layout
        layout.operator('needle.link_meshes', icon='PLUGIN', text="Link Selected Meshes")

class OBJECT_MT_needle_help(bpy.types.Menu):
    bl_label = "Needle"
    preset_subdir = "object/display"
    preset_operator = "script.execute_preset"
    def draw(self, context):
        layout = self.layout
        layout.operator("needle.open_preferences", text="Open Preferences", icon='PREFERENCES')
        download_samples_url = "https://engine.needle.tools/downloads/blender/download-samples?utm_source=blender&utm_medium=menu&utm_campaign=download-samples"
        layout.operator("wm.url_open", text="Download Samples", icon='URL').url = download_samples_url
        layout.separator()
        openLog = layout.operator("needle.open_log", text="Open Current Log", icon='CONSOLE')
        openLog.open_logs_directory = False
        logsDirectory = layout.operator("needle.open_log", text="Open Logs Directory", icon='FILEBROWSER')
        logsDirectory.open_logs_directory = True

        layout.separator()
        version = getVersion()
        layout.label(text="Needle Engine Addon " + version)
        nodeversion = tryGetNodeVersion()
        if nodeversion:
            layout.label(text="Node.js " + nodeversion)
        else:
            layout.label(text="Node.js not found")

def __needle_help(self, context):
    layout = self.layout
    # separator line
    layout.separator()
    row = layout.row(align=True)
    icon = getPreviewIcon("logo_small")
    row.menu(OBJECT_MT_needle_help.__name__, text=OBJECT_MT_needle_help.bl_label, icon_value=icon)




def __needle_object_menu(self, context):
    layout = self.layout
    layout.separator()
    row = layout.row(align=True)
    icon = getPreviewIcon("logo_small")
    row.menu(OBJECT_MT_needle_object_utils.__name__, text=OBJECT_MT_needle_object_utils.bl_label, icon_value=icon)


def register():
    bpy.types.TOPBAR_MT_help.remove(__needle_help)
    bpy.types.TOPBAR_MT_help.append(__needle_help)
    bpy.types.VIEW3D_MT_object_context_menu.remove(__needle_object_menu)
    bpy.types.VIEW3D_MT_object_context_menu.append(__needle_object_menu)

def unregister():
    bpy.types.TOPBAR_MT_help.remove(__needle_help)
    bpy.types.VIEW3D_MT_object_context_menu.remove(__needle_object_menu)
