# 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 __name__ as base_package
from .draw import draw_templates
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
        colx = split.column()
        ct_split = colx.split(factor=0.9)
        colA = ct_split.column()
        if prefs.override_splash and len(prefs.projects) > 0:
            colA.label(text="Custom Templates")
            colx.operator_context = 'INVOKE_DEFAULT'
            draw_templates(colx, context, True)
        else:
            colA.label(text="New File")
            # Call original code
            bpy.types.TOPBAR_MT_file_new.draw_ex(colx, context, use_splash=True)
        colB = ct_split.column()
        colB.menu("CT_MT_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_preset", text="Blender Website", icon='URL').type = 'BLENDER'
            col2.operator("wm.url_open_preset", text="Credits", icon='URL').type = 'CREDITS'

        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="Release Notes", icon='URL').type = 'RELEASE_NOTES'
        col2.operator("wm.url_open_preset", text="Development Fund", icon='FUND').type = 'FUND'

        layout.separator()
        layout.separator()

class CT_MT_splash_mode(bpy.types.Menu):
    bl_idname = "CT_MT_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("ct.open_preferences", text="Manage templates")
        layout.separator()
        layout.operator("ct.splash_custom", text="Use Custom Templates", icon=ct_check)
        layout.operator("ct.splash_default", text="Use Blender's Default", icon=def_check)
        
class CT_OT_splash_default(bpy.types.Operator):
    bl_idname = "ct.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
        context.preferences.is_dirty = True
        return {'FINISHED'}
    
class CT_OT_splash_custom(bpy.types.Operator):
    bl_idname = "ct.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
        context.preferences.is_dirty = True
        return {'FINISHED'}