import bpy

from .auth_client import AuthorizationError
from .update_client import check_for_update


class WM_OT_threedripper_helper_check_update(bpy.types.Operator):
    bl_idname = "wm.threedripper_helper_check_update"
    bl_label = "Check for Add-on Updates"
    bl_description = "Check 3DRipper for a newer official Add-on package"

    def execute(self, context):
        try:
            release = check_for_update(context)
        except AuthorizationError as error:
            self.report({"ERROR"}, str(error))
            return {"CANCELLED"}

        if release.update_available:
            self.report(
                {"WARNING"},
                "Add-on {} is available. Use the sidebar download link to install it.".format(
                    release.latest_version
                ),
            )
        else:
            self.report({"INFO"}, "3DRipper Helper is up to date.")
        return {"FINISHED"}
