from .. import __package__ as base_package
import bpy
from bpy.types import Header, Panel
from .funcs import get_scene_workspaces, get_use_topbar, get_switch, get_compact, get_only_with_multiple

# Ref TOPBAR_HT_upper_bar https://projects.blender.org/blender/blender/src/commit/2204157a2c9fc926643b0e39968602c750d9b5e6/scripts/startup/bl_ui/space_topbar.py#L14
class TOPBAR_HT_upper_bar(Header):
    bl_space_type = 'TOPBAR'
    
    def draw(self, context):
        region = context.region

        if region.alignment == 'RIGHT':
            self.draw_right(context)
        else:
            self.draw_left(context)

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

        window = context.window
        screen = context.screen

        bpy.types.TOPBAR_MT_editor_menus.draw_collapsible(context, layout)

        layout.separator()
        
        # Edit start
        use_topbar = get_use_topbar()
        compact = get_compact()
        switch= get_switch()
        only_with_multiple = get_only_with_multiple()
        has_multiple = len(bpy.data.scenes) > 1
        multiple_satisfied = (not only_with_multiple or only_with_multiple and has_multiple)

        if not screen.show_fullscreen:
            main_col = layout.column()
            spacer_row = main_col.row()
            spacer_row.scale_y = 1
            main_row = main_col.row()
            sy = 1.18
            sx = 1.18
            if switch and multiple_satisfied:
                col = main_row.column()
                # Show/Hide Custom Topbar
                col.operator("sw.switch", text="", emboss=False, icon="CHECKBOX_HLT" if use_topbar else "CHECKBOX_DEHLT")
            if not use_topbar or not multiple_satisfied:
                # Original
                layout.template_ID_tabs(window, "workspace", new="workspace.add", menu="TOPBAR_MT_workspace_menu")
            else:
                r = main_row.row(align=True)
                r.scale_x = 0.87
                r.scale_y = sy
                sw = get_scene_workspaces()
                # Link all (with no linked workspaces)
                if not sw and bpy.data.workspaces:
                    c = r.column()
                    c.scale_x = 1.4
                    c.operator("sw.link_all", text="Link all workspaces", icon="LINKED")
                active_not_here = True
                for w in sw:
                    i = sw.index(w)
                    active = bpy.context.window.workspace.name == w
                    if active and i > 0 and not compact:
                        r.separator()
                    ab = r.box()
                    ab = ab.row(align=True)
                    # Workspace unlink
                    if active and not compact:
                        col = ab.column()
                        col.scale_x = sx
                        col.operator("sw.remove", text="", icon="X", emboss=False).index = i
                    exist = w in [x.name for x in bpy.data.workspaces]
                    # Workspace
                    ab.operator("sw.workspace", text=w, icon="NONE" if exist else "ERROR", emboss=active, depress=active).workspace = w
                    # Active workspace options
                    if active:
                        col = ab.column()
                        col.scale_x = sx
                        col.menu("TOPBAR_MT_workspace_menu", text="", icon="OPTIONS")
                        active_not_here = False
                        if not compact:
                            r.separator()
                            
                if not compact:
                    r.separator()
                size = sx if compact else sx * 1.25
                # Link other workspaces
                if len(bpy.data.workspaces) > len(sw):
                    c = r.column()
                    c.scale_x = size
                    c.menu("SW_MT_missing_workspaces", text="", icon="WORKSPACE")
                    if not compact:
                        r.separator()
                # Duplicate linked workspaces from other scenes
                if len(bpy.data.scenes) > 1:
                    c = r.column()
                    c.scale_x = size
                    c.menu("SW_MT_copy_from", text="", icon="DUPLICATE")
                    if not compact:
                        r.separator()
                # Original Add Workspace menu
                c = r.column()
                c.scale_x = size
                c = c.box()
                c.operator("workspace.add", text="", icon="ADD", emboss=False)
                if not compact:
                    r.separator()
                    
                # Active (but not linked) workspace
                if active_not_here:
                    r.separator()
                    r = r.box()
                    r = r.row(align=True)
                    b = r.box()
                    b.scale_x = sx
                    b.operator("sw.link_workspace", text=f"{bpy.context.window.workspace.name} (link)", icon="LINKED", emboss=True, depress=True).workspace = bpy.context.window.workspace.name
                    # Active workspace options
                    col = r.box()
                    col.scale_x = sx
                    col.menu("TOPBAR_MT_workspace_menu", text="", icon="OPTIONS")
        # Edit - End
        else:
            layout.operator("screen.back_to_previous", icon='SCREEN_BACK', text="Back to Previous")

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

        window = context.window
        screen = context.screen
        scene = window.scene

        # If statusbar is hidden, still show messages at the top
        if not screen.show_statusbar:
            layout.template_reports_banner()
            layout.template_running_jobs()

        # Active workspace view-layer is retrieved through window, not through workspace.
        layout.template_ID(window, "scene", new="scene.new", unlink="scene.delete")

        row = layout.row(align=True)
        row.template_search(
            window, "view_layer",
            scene, "view_layers",
            new="scene.view_layer_add",
            unlink="scene.view_layer_remove")