import bpy
from .import fn
from .constant import RD_SCENE_NAME

class GPEXP_OT_render_scene_switch(bpy.types.Operator):
    bl_idname = "gp.render_scene_switch"
    bl_label = "Render Scene Switch"
    bl_description = "Switch between render"
    bl_options = {"REGISTER"}

    @classmethod
    def poll(cls, context):
        return True

    scene : bpy.props.StringProperty(default='', options={'SKIP_SAVE'})

    def execute(self, context):
        scenes = bpy.data.scenes
        if len(scenes) < 2:
            self.report({'ERROR'},'No other scene to go to')
            return {"CANCELLED"}

        ## Case where a scene name is passed
        if self.scene:
            scn = bpy.data.scenes.get(self.scene)
            if not scn:
                self.report({'ERROR'},f'No scene "{self.scene}"')
                return {"CANCELLED"}
            self.report({'INFO'},f'Switched to scene "{scn.name}"')
            bpy.context.window.scene = scn
            return {"FINISHED"}

        if context.scene.name == RD_SCENE_NAME:
            scn = scenes.get('Scene')
            if not scn: # get the next available scene
                self.report({'WARNING'},'No scene named "Scene"')
                slist = [s.name for s in scenes]
                scn = scenes[(slist.index(bpy.context.scene.name) + 1) % len(scenes)]

        else:
            scn = fn.get_render_scene(create=False)
            if not scn:
                self.report({'ERROR'},f'No "{RD_SCENE_NAME}" scene yet')
                return {"CANCELLED"}

        self.report({'INFO'},f'Switched to scene "{scn.name}"')
        bpy.context.window.scene = scn
        return {"FINISHED"}

class GPEXP_OT_swap_render_cams(bpy.types.Operator):
    bl_idname = "gp.swap_render_cams"
    bl_label = "Swap Cameras"
    bl_description = "Toggle between anim and bg cam"
    bl_options = {"REGISTER"}

    def execute(self, context):
        anim_cam = bpy.context.scene.objects.get('anim_cam')
        bg_cam = bpy.context.scene.objects.get('bg_cam')

        if not anim_cam or not bg_cam:
            self.report({'ERROR'}, 'anim_cam or bg_cam is missing')
            return {"CANCELLED"}

        cam = context.scene.camera
        if not cam:
            context.scene.camera = anim_cam
            fn.set_resolution_from_cam_prop()
            return {"FINISHED"}

        in_draw = False
        if cam.parent and cam.name in ('draw_cam', 'action_cam'):
            if cam.name == 'draw_cam':
                draw_cam = cam
                in_draw = True
            cam = cam.parent

        ## swap
        # context.scene.camera = bg_cam if cam is anim_cam else anim_cam
        if cam is anim_cam:
            main = context.scene.camera = bg_cam
            anim_cam.hide_viewport = True
            bg_cam.hide_viewport = False
        else:
            main = context.scene.camera = anim_cam
            anim_cam.hide_viewport = False
            bg_cam.hide_viewport = True

        if in_draw:
            draw_cam.parent = main
            draw_cam.data = main.data
            # back in draw_cam
            context.scene.camera = draw_cam
            bg_cam.hide_viewport = anim_cam.hide_viewport = True

        # set res
        ret = fn.set_resolution_from_cam_prop(main)
        if ret:
            self.report({ret[0]}, ret[1])

        return {"FINISHED"}

class GPEXP_OT_set_gp_render_workspace(bpy.types.Operator):
    bl_idname = "gp.set_gp_render_workspace"
    bl_label = 'Set GP Render Workspace'
    bl_description = "Switch to GP render workspace, append and activate if not exists"
    bl_options = {'REGISTER', 'INTERNAL'}

    def execute(self, context):
        fn.activate_workspace(name='GP Render', context=context)
        return {"FINISHED"}

classes=(
GPEXP_OT_render_scene_switch,
GPEXP_OT_swap_render_cams,
GPEXP_OT_set_gp_render_workspace,
)

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

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