import bpy
from bpy.types import Panel
from .funcs import get_scene_workspaces, workspace_ops

class SW_PT_select_scene_workspaces(Panel):
    bl_idname = "sw.select_scene_workspaces"
    bl_label = "Scene Workspaces"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"
    bl_options = {'DEFAULT_CLOSED'}
    
    def draw(self, context):
        layout = self.layout
        
        layout.label(text="Workspaces linked to this Scene:")
        box = layout.box()
        sw = get_scene_workspaces()
        if not sw:
            box.label(text="There are no workspaces linked right now...")
            box.operator("sw.link_all")
        else:    
            i = 0
            for w in sw:
                box.scale_y = .4
                box2 = box.box()
                row = box2.row(align=True)
                row.scale_y = 2.1
                row.label(text=f"{w}")
                workspace_ops(row, i, False, w)
                i += 1
            box.separator()
            
        layout.separator()
        
        layout.label(text="Link other workspaces")
        box2 = layout.box()
        row = box2.row()
        row_count = 0
        l = [x for x in bpy.data.workspaces if x.name not in sw]
        for w in l:
            if row_count > 0 and row_count % 4 == 0:
                row = box2.row()
            row.operator("sw.link_workspace", text=w.name).workspace = w.name
            row_count += 1
        if l:
            layout.separator()
            layout.operator("sw.link_all")
        else:
            box2.label(text="All workspaces are currently linked.")
