# Custom Templates - 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 .classes.ots import CustomTemplatesPreferences, TemplateItem, OT_ExportTemplates, OT_ImportTemplates, CUSTOM_MT_Export_Menu, OT_SelectTemplatePopup, OT_AddTemplatePopup, OT_AddTemplateItem, OT_RemoveTemplateItem, OT_MoveDownTemplateItem, OT_MoveUpTemplateItem, OT_OpenAddonPreferences

bl_info = {
    "id": "custom_templates",
    "name": "Custom Templates",
    "tagline": "Add your own .blend files as template options for new projects",
    "blender": (4, 2, 0),
    "location": "File > New & File > Defaults",
    "category": "System",
    "support": "COMMUNITY",
    "blender_manifest": "blender_manifest.toml"
}

def draw_addon_separator(layout):
    layout.separator()
    layout.label(text="Custom Templates")
    layout.separator()

def draw_file_new_templates(self, context):
    layout = self.layout
    prefs = context.preferences.addons[__package__].preferences

    if len(prefs.projects) > 0:
        draw_addon_separator(layout)

    for project in prefs.projects:
        layout.operator(
            "wm.read_homefile", text=project.name).filepath = project.path

def draw_file_default_operators(self, context):
    layout = self.layout
    draw_addon_separator(layout)
    # Manage Template
    layout.operator("custom_templates.open_preferences",
                    text="Manage templates")
    # Import/Export
    layout.menu("custom_templates.export_menu", text="Import/Export")
    layout.separator()
    # Select new custom template
    layout.operator("custom_templates.select_template_popup",
                    text="Select new custom template")
    # Use current as new template (only with an active saved .blend file opened)
    if bpy.data.filepath != "":
        layout.operator("custom_templates.add_template_popup",
                        text="Use current as new template")

classes = [TemplateItem,
           OT_ExportTemplates,
           OT_ImportTemplates,
           CUSTOM_MT_Export_Menu,
           OT_MoveUpTemplateItem,
           OT_MoveDownTemplateItem,
           OT_AddTemplateItem,
           OT_RemoveTemplateItem,
           OT_AddTemplatePopup,
           OT_SelectTemplatePopup,
           OT_OpenAddonPreferences,
           CustomTemplatesPreferences]

def register():
    for c in classes:
        bpy.utils.register_class(c)
    bpy.types.TOPBAR_MT_file_new.remove(draw_file_new_templates)
    bpy.types.TOPBAR_MT_file_new.remove(draw_file_default_operators)
    bpy.types.TOPBAR_MT_file_new.append(draw_file_new_templates)
    bpy.types.TOPBAR_MT_file_defaults.append(draw_file_default_operators)

def unregister():
    for c in reversed(classes):
        bpy.utils.unregister_class(c)
    bpy.types.TOPBAR_MT_file_new.remove(draw_file_new_templates)
    bpy.types.TOPBAR_MT_file_defaults.remove(draw_file_default_operators)

if __name__ == "__main__":
    register()
