# Ref WM_MT_splash https://projects.blender.org/blender/blender/src/commit/5a9fe638dedb179050c4929ea8fcdec80d221af2/scripts/startup/bl_operators/wm.py#L3296
# Ref TOPBAR_MT_file_new https://projects.blender.org/blender/blender/src/commit/5a9fe638dedb179050c4929ea8fcdec80d221af2/scripts/startup/bl_ui/space_topbar.py#L286
from .. import __package__ as base_package
from .draw import draw_templates_on_col
import bpy
# Clone of the WM_MT_splash (the section under the image of the Splash)
# It's edited (only in #Templates part) to add a menu in the New File section 
# to switch between blender's original templates and Custom Templates.
# If you have no custom templates, the default templates are displayed
class WM_MT_splash(bpy.types.Menu):
    bl_label="Splash"
    
    def draw(self, context):
        layout = self.layout
        prefs = context.preferences.addons[base_package].preferences
        layout.operator_context = 'EXEC_DEFAULT'
        layout.emboss = 'PULLDOWN_MENU'
        split = layout.split()

        # Templates
        col1 = split.column()
        ct_split = col1.split(factor=0.9)
        colA = ct_split.column()
        if prefs.override_splash and len(prefs.projects) > 0:
            colA.label(text="Custom Templates")
            col1.operator_context = 'INVOKE_DEFAULT'
            draw_templates_on_col(col1, context, True)
        else:
            colA.label(text="New File")
            # Call original code
            bpy.types.TOPBAR_MT_file_new.draw_ex(col1, context, use_splash=True)
        colB = ct_split.column()
        colB.menu("custom_templates.splash_mode", icon='DOWNARROW_HLT', text="")
        
        # Recent
        col2 = split.column()
        col2_title = col2.row()

        found_recent = col2.template_recent_files()

        if found_recent:
            col2_title.label(text="Recent Files")
        else:
            # Links if no recent files.
            col2_title.label(text="Getting Started")

            col2.operator("wm.url_open_preset", text="Manual", icon='URL').type = 'MANUAL'
            col2.operator("wm.url_open", text="Tutorials", icon='URL').url = "https://www.blender.org/tutorials/"
            col2.operator("wm.url_open", text="Support", icon='URL').url = "https://www.blender.org/support/"
            col2.operator("wm.url_open", text="User Communities", icon='URL').url = "https://www.blender.org/community/"
            col2.operator("wm.url_open_preset", text="Blender Website", icon='URL').type = 'BLENDER'

        layout.separator()

        split = layout.split()

        col1 = split.column()
        sub = col1.row()
        sub.operator_context = 'INVOKE_DEFAULT'
        sub.operator("wm.open_mainfile", text="Open...", icon='FILE_FOLDER')
        col1.operator("wm.recover_last_session", icon='RECOVER_LAST')

        col2 = split.column()

        col2.operator("wm.url_open_preset", text="Donate", icon='FUND').type = 'FUND'
        col2.operator("wm.url_open_preset", text="What's New", icon='URL').type = 'RELEASE_NOTES'

        layout.separator()
        
        if (not bpy.app.online_access) and bpy.app.online_access_override:
            self.layout.label(text="Running in Offline Mode", icon='INTERNET_OFFLINE')

        layout.separator()

class CUSTOM_MT_splash_mode(bpy.types.Menu):
    bl_idname = "custom_templates.splash_mode"
    bl_label = "Custom Templates Switch"
    bl_description = "Swtich between Blender's default and your Custom Templates"

    def draw(self, context):
        layout = self.layout
        prefs = context.preferences.addons[base_package].preferences
        def_check = 'NONE'
        ct_check = 'NONE'
        if prefs.override_splash:
            ct_check = "CHECKMARK"
        else:
            def_check = "CHECKMARK"
            
        layout.operator("custom_templates.open_preferences", text="Manage templates")
        layout.separator()
        layout.operator("custom_templates.splash_custom", text="Use Custom Templates", icon=ct_check)
        layout.operator("custom_templates.splash_default", text="Use Blender's Default", icon=def_check)
        
class OT_SplashDefault(bpy.types.Operator):
    bl_idname = "custom_templates.splash_default"
    bl_label = "Display default Blender's templates"
    bl_description = "Use Blender's default templates in the Splash Screen"

    def execute(self, context):
        prefs = context.preferences.addons[base_package].preferences
        prefs.override_splash = False
        return {'FINISHED'}
    
class OT_SplashCustom(bpy.types.Operator):
    bl_idname = "custom_templates.splash_custom"
    bl_label = "Display your custom templates"
    bl_description = "Use your custom templates in the Splash Screen (limited to first 5)"

    def execute(self, context):
        prefs = context.preferences.addons[base_package].preferences
        prefs.override_splash = True
        return {'FINISHED'}
    