# Scene Workspaces - Blender Add-On
# Copyright (C) 2024  Francesco Bellini

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
import bpy
from bpy.app.handlers import persistent
from .sw.funcs import select_all, workspace_menu, assign_shortcuts, remove_shortcuts, has_data
from .sw.ops import SW_OT_workspace, SW_OT_link_workspace, SW_OT_move, SW_OT_link_all, SW_OT_remove, SW_OT_switch, SW_OT_rename, SW_OT_copy_from_scene, SW_OT_activate, SW_OT_select
from .sw.topbar import TOPBAR_HT_upper_bar
from .sw.panel import SW_PT_select_scene_workspaces
from .sw.menu import SW_MT_missing_workspaces, SW_MT_copy_from
from .sw.prefs import SW_Preferences

bl_info = {
    "id": "scene_workspaces",
    "name": "Scene Workspaces",
    "tagline": "Filter and sort your workspaces, scene by scene",
    "blender": (4, 2, 0),
    "location": "Workspaces topbar, Properties > Scene",
    "category": ["Scene", "System", "User Interface"],
    "support": "COMMUNITY",
    "blender_manifest": "blender_manifest.toml"
}

classes = [
    SW_OT_workspace, 
    SW_OT_select,  
    SW_OT_switch,
    SW_OT_move, 
    SW_OT_remove,
    SW_OT_rename,
    SW_OT_activate,
    SW_OT_link_workspace,
    SW_OT_link_all, 
    SW_OT_copy_from_scene,
    SW_MT_copy_from,
    SW_MT_missing_workspaces,
    SW_PT_select_scene_workspaces,
    SW_Preferences,
    TOPBAR_HT_upper_bar
]

last_scene = None
@persistent
def on_scene(scene):
    global last_scene
    last_scene = bpy.context.scene.name
    if not has_data():
        select_all()
@persistent
def on_frame(scene):
    # Avoid simple playback or frame change (only scene change)
    global last_scene
    if bpy.context.scene.name != last_scene:
        last_scene = bpy.context.scene.name
        if not has_data():
            select_all()

og_header = None;

def register():
    global og_header
    og_header = bpy.types.TOPBAR_HT_upper_bar;
    for c in classes:
        bpy.utils.register_class(c)
    bpy.types.TOPBAR_MT_workspace_menu.prepend(workspace_menu)
    bpy.app.handlers.frame_change_post.append(on_frame)
    bpy.app.handlers.load_post.append(on_scene)
    assign_shortcuts()

def unregister():
    for c in reversed(classes):
        bpy.utils.unregister_class(c)
    bpy.utils.register_class(og_header)
    bpy.types.TOPBAR_MT_workspace_menu.remove(workspace_menu)
    bpy.app.handlers.frame_change_post.remove(on_frame)
    bpy.app.handlers.load_post.remove(on_scene)
    remove_shortcuts()
    
if __name__ == "__main__":
    register()
