from .. import __package__ as base_package
import bpy

prop_name = "scene_workspaces";
prop_i_name = "scene_workspaces_active";

def_templates_w_order = [
    ['Layout','Modeling','Sculpting','UV Editing','Texture Paint','Shading','Animation','Rendering','Compositing','Geometry Nodes','Scripting'],
    ['2D Animation','2D Full Canvas','Compositing','Rendering'],
    ['Sculpting','Shading'],
    ['Motion Tracking','Masking','Compositing','Rendering'],
    ['Video Editing','Rendering']
]

def has_data(scene = None, prop = prop_name):
    return prop in bpy.data.scenes[scene if scene else bpy.context.scene.name]

def get_scene_workspaces(scene = None):
    if has_data(scene):
        return bpy.data.scenes[scene if scene else bpy.context.scene.name][prop_name]
    else:
        return []
    
def get_active(scene = None):
    if has_data(scene, prop_i_name):
        i = bpy.data.scenes[scene if scene else bpy.context.scene.name][prop_i_name]
        return i if i else 0
    else:
        return 0
    
def set_active(val = 0):
    bpy.data.scenes[bpy.context.scene.name][prop_i_name] = val
    
def prefs():
    return bpy.context.preferences.addons[base_package].preferences
    
def get_use_topbar():
    return prefs().use_topbar

def get_only_with_multiple():
    return prefs().only_with_multiple_scenes

def get_switch():
    return prefs().switch

def get_compact():
    return prefs().compact

def set_scene_workspaces(workspaces, scene = None):
    s = scene if scene else bpy.context.scene.name
    bpy.data.scenes[s][prop_name] = workspaces
    if not get_active():
        set_active()
        
def workspaces_match_template():
    match = False
    templates = []
    for templs in def_templates_w_order:
        if set([w.name for w in bpy.data.workspaces]) == set(templs):
            match = True
            templates = templs
            break
    return match, templates

def select_all():
    workspaces = get_scene_workspaces()
    match, templates = workspaces_match_template()
    if match and not workspaces:
        workspaces = templates
    else:
        for w in bpy.data.workspaces:
            if w.name not in workspaces:
                workspaces.append(w.name)
    set_scene_workspaces(workspaces)
    
def get_other_scene_names():
    return [s.name for s in bpy.data.scenes if s.name != bpy.context.scene.name]

def tag_redraw_topbar():
    for area in bpy.context.window.screen.areas:
        if area.type == 'TOPBAR':
            area.tag_redraw()
            break

def assign_shortcuts():
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY')
    
    prev = km.keymap_items.new('sw.activate', 'PAGE_UP', 'PRESS', ctrl=True, shift=True)
    prev.properties.next = False
    
    next = km.keymap_items.new('sw.activate', 'PAGE_DOWN', 'PRESS', ctrl=True, shift=True)
    next.properties.next = True
    
def remove_shortcuts():
    wm = bpy.context.window_manager
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY')
    for kmi in km.keymap_items:
        if kmi.idname == "sw.activate":
            km.keymap_items.remove(kmi)

def workspace_ops(layout, i, menu_mode=False, name=None):
    if menu_mode:
        layout.prop(prefs(), "compact", text="Compact Mode")
        layout.separator()
    def remove(l):
        remove = l.operator(
            "sw.remove", text="Unlink workspace from scene" if menu_mode else "", icon='X')
        remove.index = i
    remove(layout)
    layout.separator()
    rename = layout.operator(
        "sw.rename", text="Rename" if menu_mode else "", icon="TEXT")
    rename.current_name = name if name else bpy.context.window.workspace.name
    rename.new_name = rename.current_name
    layout.separator()
    top = layout.operator("sw.move", text="Reorder to Front (scene only)" if menu_mode else "",
                          icon='TRIA_LEFT_BAR' if menu_mode else 'TRIA_UP_BAR')
    top.index = i
    top.top = True
    up = layout.operator("sw.move", text="Move Left (scene only)" if menu_mode else "",
                         icon='TRIA_LEFT' if menu_mode else 'TRIA_UP')
    up.index = i
    up.up = 1
    down = layout.operator("sw.move", text="Move Right (scene only)" if menu_mode else "",
                           icon='TRIA_RIGHT' if menu_mode else 'TRIA_DOWN')
    down.index = i
    down.up = -1
    bottom = layout.operator("sw.move", text="Reorder to Back (scene only)" if menu_mode else "",
                             icon='TRIA_RIGHT_BAR' if menu_mode else 'TRIA_DOWN_BAR')
    bottom.index = i
    bottom.top = False
    layout.separator()
    if menu_mode and bpy.context.window.workspace.name in get_scene_workspaces():
        layout.separator()        
        layout.operator("sw.activate", text="Previous Workspace (scene)" if menu_mode else "").next = False
        layout.operator("sw.activate", text="Next Workspace (scene)" if menu_mode else "").next = True

def workspace_menu(self, context):
    layout = self.layout
    use_topbar = get_use_topbar()
    if use_topbar and has_data():
        workspace_ops(layout, get_scene_workspaces().index(
            context.window.workspace.name), True)
        layout.separator()
