import bpy
import re
from pathlib import Path
from . import gen_vlayer, fn
from bpy.props import (BoolProperty, StringProperty, EnumProperty)


def batch_setup_render_scene(context=None, render_scn=None, node_scene=None, preview=True):
    '''A series of setup actions for Render scene:
    - renumber fileout
    - Clean compo Tree
    - Go to camera view in visible viewports
    - Swap to bg cam
    '''

    if context is None:
        context = bpy.context
    if render_scn is None:
        render_scn = fn.get_render_scene(create=False)
        if not render_scn:
            print('Render scene not found in batch_setup_render_scene')
            return
    if node_scene is None:
        node_scene = render_scn

    ## Renumber File outputs
    print('Renumber File outputs')
    for fo in render_scn.node_tree.nodes:
        if fo.type == 'OUTPUT_FILE':
            fn.renumber_keep_existing(fo)

    ## Swap to bg_cam (if any)
    # if render_scn.objects.get('bg_cam') and (not render_scn.camera or render_scn.camera.name != 'bg_cam'):
    #     print('Swap to bg cam')
    #     bpy.ops.gp.swap_render_cams()

    if render_scn.objects.get('bg_cam'):
        render_scn.camera = render_scn.objects.get('bg_cam')
        fn.set_resolution_from_cam_prop(scene=render_scn)

    ## Go to camera view in visible viewports (! Need timer + Already done in workspace script!)
    # if not bpy.app.background:
    #     print('Go to camera view in visible viewports')
    #     if render_scn.camera:
    #         for window in bpy.context.window_manager.windows:
    #             screen = window.screen
    #             for area in screen.areas:
    #                 if area.type == 'VIEW_3D':
    #                     print('3D viewport found, Go in Camera')
    #                     area.spaces.active.region_3d.view_perspective = 'CAMERA'

    ## Clean compo Tree
    print('Clean compo Tree')

    bpy.ops.gp.clean_compo_tree('EXEC_DEFAULT', scene=render_scn.name, node_scene=node_scene.name)

    ## Trigger check file before finishing ?
    # bpy.ops.gp.check_render_scene('INVOKE_DEFAULT')

    print('batch setup render scene Done')

class GPEXP_OT_render_auto_build(bpy.types.Operator):
    bl_idname = "gp_export.render_auto_build"
    bl_label = "Auto-Build"
    bl_description = "Trigger all operation to make build render scene with default settings"
    bl_options = {"REGISTER"}

    scene : StringProperty(name="Target Scene Name",
        description="Render scene to send GP to on a named scene, abort if not exists (not exposed)",
        default='', options={'SKIP_SAVE'})

    node_scene : StringProperty(name="Compositing Scene",
        description="Name of the scene holding compositing nodes",
        default='', options={'SKIP_SAVE'})

    make_gp_single_user : BoolProperty(name='Set Single User Data', default=True,
        description='Set single user on all objects GP data')

    excluded_prefix : StringProperty(
        name='Excluded Layer By Prefix', default='RG, PO', # MA, MASK, mask, MSK, msk
        description='Exclude layer to send to render by prefix (comma separated list)')

    clean_name_and_visibility : BoolProperty(name='Clean Name And Visibility', default=True,
        description='Add object name to layer name when there is only prefix (ex: "CO_")\
            \nEnable visibility for layer with prefix included in Prefix Filter')

    clean_material_duplication : BoolProperty(name='Clean Material Duplication', default=True,
        description='Clean material stack. i.e: Replace "mat.001" in material stack if "mat" exists and has same color')

    prefix_filter : StringProperty(name='Prefix Filter', default='CO, CU, FX, TO', # , MA # exclude MA if mask are applied
        description='Comma separated prefix to render, Layer with those prefix will be made visible')

    set_layers_colors : BoolProperty(name='Set Layers Colors', default=True,
        description='Set colors for on layers according to prefix (hadrcoded color set)')

    trigger_rename_lowercase : BoolProperty(name='Trigger Rename Lowercase', default=True,
        description='Rename all layer names lowercase')

    trigger_renumber_by_distance : BoolProperty(name='Trigger Renumber By Distance', default=True,
        description='Renumber object accordind to distance from camera and In-Front value')

    export_layer_infos : BoolProperty(name='Export Layer Infos', default=True,
        description='Export layers infos to a Json file')

    reset_layers_state : BoolProperty(name='Reset Layer State', default=True,
        description='Set layer opacity to 100%, Disable use light on all layer, set regular blend mode, hide invisible')

    group_all_adjacent_layer_type : BoolProperty(name='Group All Adjacent Layer Type', default=False,
        description='Fuse output Viewlayer according to adjacent Prefix in layer stack')

    set_active_scene : EnumProperty(name='Set Active Scene', default='NODES',
        items=(
            ('NONE', 'No change', 'Do not set active scene'),
            ('RENDER', 'Render Scene', 'Set Render Scene as active'),
            ('NODES', 'Nodes Scene', 'Set compositing Scene as active (render scene if same scene used)'),
        ),
        description='Set the active scene')

    change_to_gp_workspace : BoolProperty(name='Change To Gp Workspace', default=True,
        description='Switch to "GP Render" workspace shipped with addon')

    batch_setup_render_scene : BoolProperty(name='Batch Setup Render Scene', default=True,
        description='- Renumber fileoutputs\
        \n- Clean compo Tree\
        \n- Go to camera view in visible viewports\
        \n- Swap to bg cam'
        )

    add_preview : BoolProperty(name='Add Preview', default=True,
        description='Create preview with stacked alpha over on render layers')

    exclude_background_gp : BoolProperty(name='Exclude Background GP', default=True,
        description='Exclude GP object with boolean custom property "is_background" at True (created by "Background Plane Manager" addon)')

    # File output templates
    base_path : bpy.props.StringProperty(name='Base Path', default='', options={'SKIP_SAVE'})
    file_slot : bpy.props.StringProperty(name='File Slot', default='', options={'SKIP_SAVE'})
    layer_slot : bpy.props.StringProperty(name='Layer Slot', default='', options={'SKIP_SAVE'})

    def invoke(self, context, event):
        # return self.execute(context)
        return context.window_manager.invoke_props_dialog(self, width=380)

    def draw(self, context):
        layout = self.layout
        col = layout.column()
        col.prop(self, 'clean_name_and_visibility')
        row = col.row()
        row.prop(self, 'prefix_filter')
        row.active = self.clean_name_and_visibility

        col.prop(self, 'make_gp_single_user')
        col.prop(self, 'clean_material_duplication')

        col.prop(self, 'set_layers_colors')
        col.prop(self, 'trigger_rename_lowercase')
        col.prop(self, 'trigger_renumber_by_distance')
        col.prop(self, 'export_layer_infos')
        col.prop(self, 'reset_layers_state')

        col.label(text='Send prefixed layer to render scene (except excluded)')
        col.prop(self, 'excluded_prefix', text='Excluded')

        col.prop(self, 'set_active_scene')
        col.prop(self, 'group_all_adjacent_layer_type')
        col.prop(self, 'change_to_gp_workspace')
        col.prop(self, 'batch_setup_render_scene')
        col.prop(self, 'add_preview')

        # layout.prop(self, 'clear_unused_view_layers')

    def execute(self, context):
        print('-- Auto-build Render scene --\n')

        ## Prefix Filter
        # TODO : add to preferences / environment var
        # prefix_to_render = ['CO', 'CU', 'FX', 'TO', 'MA']
        prefix_to_render = [p.strip() for p in self.prefix_filter.split(',')]
        print('prefix_to_render: ', prefix_to_render)

        render_scn = fn.get_render_scene(create=False)
        if self.scene:
            if bpy.data.scenes.get(self.scene):
                self.report({'ERROR'}, f'Abort, scene "{self.scene}" already exists')
                return {'CANCELLED'}
            render_scn = fn.get_render_scene(scene_name=self.scene, create=True)
        else:
            ## Create render scene with default name
            render_scn = fn.get_render_scene(create=True)

        if self.node_scene:
            node_scene = fn.get_compo_scene(scene_name=self.node_scene, create=True) # create if not exists
            ## Set scene target in source scene
            context.scene.gp_render_settings.node_scene = node_scene.name
        else:
            node_scene = fn.get_compo_scene(create=True)
            if not node_scene:
                node_scene = render_scn

        ## Propagate render scene and compo scene target properties on all scenes
        for scn in bpy.data.scenes:
            scn.gp_render_settings.node_scene = node_scene.name
            scn.gp_render_settings.render_scene = render_scn.name

        all_gp_objects = [o for o in context.scene.objects if o.type == 'GPENCIL']
        
        ## Exclude Background GP (is_background is a custom property set by the addon "Background Plane Manager")
        if self.exclude_background_gp:
            all_gp_objects = [o for o in all_gp_objects if not o.get('is_background')]

        ## Clean name and visibility
        if self.clean_name_and_visibility:
            for o in all_gp_objects:
                if o.hide_render:
                    print(f'skip: {o.name} hide render')
                    continue
                for l in o.data.layers:
                    ## Clean name when layer has no name after prefix
                    if re.match(r'^[A-Z]{2}_$', l.info):
                        l.info = l.info + o.name.lower()

                    ## Make used prefix visible ?? (maybe some layer were intentionally hidden...)
                    # if (res := re.search(r'^([A-Z]{2})_', l.info)):
                    #     if res.group(1) in prefix_to_render and l.hide == True and not 'invisible' in l.info:
                    #         print(f'{o.name} -> {l.info} : Switch visibility On')
                    #         l.hide = False

        if self.clean_material_duplication:
            print('Clean material duplicates')
            for ob in all_gp_objects:
                fn.clean_mats_duplication(ob)

        ## Hide "invisible" material
        mat_invisible = bpy.data.materials.get('invisible')
        if mat_invisible and mat_invisible.is_grease_pencil:
            mat_invisible.grease_pencil.hide = True

        ob_list = [o for o in all_gp_objects if not o.hide_get() and fn.is_valid_name(o.name)]
        if not ob_list:
            self.report({'ERROR'}, 'No GP object to render found')
            return {'CANCELLED'}

        print('GP objects to send:')
        for o in ob_list:
            print(f' - {o.name}')

        ## Set layers colors (skip if colors were already set ?)
        if self.set_layers_colors:
            ## Option: Maybe find a way to create a color from prefix hash ?
            ## (always give unique color with same prefix whatever the project!)
            fn.set_layer_colors(skip_if_colored=False)

        ## Trigger rename lowercase
        if self.trigger_rename_lowercase:
            print('Trigger rename lowercase')
            bpy.ops.gp.lower_layers_name('EXEC_DEFAULT')

        ## Trigger renumber by distance
        if self.trigger_renumber_by_distance:
            print('Trigger renumber by distance')
            bpy.ops.gp.auto_number_object('EXEC_DEFAULT')

        ## Export layer infos ? (skip if json already exists)
        if self.export_layer_infos:
            print('Export layer infos (skip if json already exists)')
            bpy.ops.gp.export_infos_for_compo('INVOKE_DEFAULT', skip_check=True)

        ## Reset layer states -> 100% opacity (except mask layer 'MA' by default), remove use light, set default blend mode, hide material named "invisible".
        if self.reset_layers_state:
            bpy.ops.gp.layers_state('EXEC_DEFAULT', no_popup=True)

        ## Set GP object data to single user (Individual viewlayers)
        if self.make_gp_single_user:
            for o in ob_list:
                if o.data.users > 1:
                    o.data = o.data.copy()

        ## Send all GP to render scene
        print('Send all GP to render scene (Create render scene if needed)')
        # bpy.ops.gp.add_object_to_render(mode="ALL") # Ops to send all
        gen_vlayer.export_gp_objects(ob_list, exclude_list=self.excluded_prefix, scene=render_scn, node_scene=node_scene,
                                     base_path=self.base_path, file_slot=self.file_slot, layer_slot=self.layer_slot) # Create render scene OTF

        ## Switch to new Render Scene
        print('Switch to new Render Scene')
        render_scn = fn.get_render_scene(create=False)
        if not render_scn:
            self.report({'ERROR'}, 'No render scene found')
            return {'CANCELLED'}

        ## Group all adjacent layer type
        if self.group_all_adjacent_layer_type:
            print('Group all adjacent layer type')
            for ob in ob_list:
                fn.group_adjacent_layer_prefix_rlayer(ob, excluded_prefix=self.excluded_prefix, first_name=True)

        if self.set_active_scene == 'RENDER':
            context.window.scene = render_scn
        elif self.set_active_scene == 'NODES':
            context.window.scene = node_scene

        # bpy.ops.gp_export.render_scene_setup() # next render scene setup at once
        ## attempt to refresh scene
        # render_scn.node_tree.nodes.update()
        # context.view_layer.update()
        # context.scene.update_tag()

        ## Change to GP workspace (if needed)
        if self.change_to_gp_workspace:
            if context.window.workspace.name != 'GP Render':
                print('Change to GP workspace')
                if (render_wkspace := bpy.data.workspaces.get('GP Render')):
                    context.window.workspace = render_wkspace
                else:
                    ret = fn.activate_workspace('GP Render')
                    # render_wkspace_filepath = Path(__file__).parent / 'app_templates' / 'GP_render' / 'startup.blend'
                    # ret = bpy.ops.workspace.append_activate(idname='GP Render', filepath=str(render_wkspace_filepath))
                    if not ret:
                        print('No GP render workspace available')

        # context.workspace.update_tag()
        # context.scene.update_tag()

        ## Batch setup render scene
        batch_setup_render_scene(render_scn=render_scn, node_scene=node_scene)

        ## create preview

        if self.add_preview:
            from .OP_merge_layers import merge_compositor_preview
            merge_compositor_preview(scene=node_scene)

        ## No need for timer anymore !
        # if batch_setup_render_scene:
        #     if self.timer > 0:
        #         print(f'batch_setup_render_scene: called with timer {self.timer}s')
        #         # add timer otherwise render scene setup don't do anything
        #         bpy.app.timers.register(batch_setup_render_scene, first_interval=self.timer)
        #     else:
        #         print('batch_setup_render_scene: Direct call')
        #         batch_setup_render_scene(render_scn=render_scn)

        ## set at least one GP object active
        gp_ob = next((o for o in context.view_layer.objects if o.type == 'GPENCIL'), None)
        if gp_ob:
            context.view_layer.objects.active = gp_ob

        ## Trigger check file before finishing ?
        # bpy.ops.gp.check_render_scene('INVOKE_DEFAULT')
        ## Note: After all these operation, a ctrl+Z might crash

        print('\nDone.\n')
        return {"FINISHED"}

class GPEXP_OT_render_scene_setup(bpy.types.Operator):
    bl_idname = "gp_export.render_scene_setup"
    bl_label = "Batch Setup Render Scene"
    bl_description = "Batch some actions to setup render scene:\
                    \n- renumber file output nodes\
                    \n- Clean compo Tree\
                    \n- Go to camera view in visible viewports\
                    \n- Swap to bg cam"
    bl_options = {"REGISTER"}

    def execute(self, context):
        print('-- Auto-setup Render scene --\n')
        batch_setup_render_scene(context=context)
        return {"FINISHED"}

classes=(
GPEXP_OT_render_auto_build,
GPEXP_OT_render_scene_setup,
)

def register():
    for cls in classes:
        bpy.utils.register_class(cls)

def unregister():
    for cls in reversed(classes):
        bpy.utils.unregister_class(cls)