import bpy
from . import fn

class GPEXP_OT_set_crop_from_selection(bpy.types.Operator):
    bl_idname = "gp.set_crop_from_selection"
    bl_label = "Set Crop"
    bl_description = "Automatic set crop from selection"
    bl_options = {"REGISTER"}

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

    def execute(self, context):
        scn = context.scene
        err = fn.set_box_from_selected_objects(scn=scn, cam=scn.camera)
        if err:
            self.report({'ERROR'}, err)
            return {"CANCELLED"}

        scn.render.use_border = True
        scn.render.use_crop_to_border = True
        return {"FINISHED"}

class GPEXP_OT_export_crop_coord_to_json(bpy.types.Operator):
    bl_idname = "gp.export_crop_coord_to_json"
    bl_label = "Set Crop"
    bl_description = "Export json of all scenes borders (when enabled)" # Automatic set crop from selection
    bl_options = {"REGISTER"}

    def execute(self, context):
        # scn = context.scene (mono-scene)
        # if not scn.render.use_border or not scn.render.use_crop_to_border:
        #     self.report({'ERROR'}, 'Current scene have cropping disabled or use crop_to_border disabled!')
        #     return {'CANCELLED'}
        crop_dic = fn.export_crop_to_json()
        if not crop_dic:
            self.report({'ERROR'}, 'No crop to export (Border might be deactivated in all scenes)')
        # else:
        #     self.report({'INFO'}, 'Json saved')
        return {"FINISHED"}


classes=(
GPEXP_OT_set_crop_from_selection,
GPEXP_OT_export_crop_coord_to_json,
)

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

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