
import bpy
from bpy.types import Menu

from .core.lib_utils import get_active_library


class ASSETLIB_MT_node_editor(Menu):
    bl_label = "Asset"

    def draw(self, context):
        layout = self.layout
        op = layout.operator("assetlibrary.publish_asset", text='Publish Node Group', icon='NODETREE')
        op.data_type = 'NodeTree'

        if context.space_data.tree_type == 'GeometryNodeTree':
            op = layout.operator("assetlibrary.publish_asset", text='Publish Object', icon='OBJECT_DATA')
            op.data_type = 'Object'

        elif context.space_data.tree_type == 'ShaderNodeTree':
            op = layout.operator("assetlibrary.publish_asset", text='Publish Material', icon='MATERIAL')
            op.data_type = 'Material'


def draw_assetbrowser_header(self, context):
    lib = get_active_library()

    if not lib:
        FILEBROWSER_HT_header._draw_asset_browser_buttons(self, context)
        return

    space_data = context.space_data
    params = context.space_data.params

    row = self.layout.row(align=True)
    row.separator()

    row.operator("assetlibrary.bundle", icon='UV_SYNC_SELECT', text='').name = lib.name
    #op
    #op.clean = False
    #op.only_recent = True

    lib.plugin.draw_header(row)

    if context.selected_files and context.active_file:
        row.separator()
        row.label(text=context.active_file.name)

    row.separator_spacer()

    sub = row.row()
    sub.ui_units_x = 10
    sub.prop(params, "filter_search", text="", icon='VIEWZOOM')

    row.separator_spacer()

    row.prop_with_popover(
        params,
        "display_type",
        panel="ASSETBROWSER_PT_display",
        text="",
        icon_only=True,
    )

    row.operator(
        "screen.region_toggle",
        text="",
        icon='PREFERENCES',
        depress=is_option_region_visible(context, space_data)
    ).region_type = 'TOOL_PROPS'


def draw_assetbrowser_asset_menu(self, context):
    layout = self.layout
    layout.operator("assetlibrary.publish_assets", text='Publish Assets', icon='ASSET_MANAGER')

    # if not get_active_library():
    #     return

    # self.layout.separator()
    # box = self.layout.box()
    # row = box.row()
    # row.separator(factor=0.5)
    # row.label(text='Asset Library')
    # row.separator(factor=0.5)

# classes = (,
#     # ASSETLIB_PT_pose_library_editing,
#     # ASSETLIB_PT_pose_library_usage,
#     # ASSETLIB_MT_context_menu,
#     # ASSETLIB_PT_libraries
# )

def draw_asset_preview_menu(self, context):
    layout = self.layout
    layout.operator("assetlibrary.save_asset_preview")


def draw_node_tree_menu(self, context):
    layout = self.layout
    row = layout.row(align=False)
    row.menu('ASSETLIB_MT_node_editor')


bl_classes = (
    ASSETLIB_MT_node_editor,)


def register() -> None:
    for bl_class in bl_classes:
        bpy.utils.register_class(bl_class)

    #bpy.types.ASSETBROWSER_MT_editor_menus.append(draw_assetbrowser_header)
    bpy.types.ASSETBROWSER_MT_metadata_preview_menu.append(draw_asset_preview_menu)
    bpy.types.NODE_MT_editor_menus.append(draw_node_tree_menu)

    bpy.types.ASSETBROWSER_MT_asset.append(draw_assetbrowser_asset_menu)

def unregister() -> None:
    for bl_class in reversed(bl_classes):
        bpy.utils.unregister_class(bl_class)

    bpy.types.ASSETBROWSER_MT_editor_menus.remove(draw_assetbrowser_header)
    bpy.types.NODE_MT_editor_menus.remove(draw_node_tree_menu)
    bpy.types.ASSETBROWSER_MT_asset.remove(draw_assetbrowser_asset_menu)