import logging
from functools import wraps
from typing import Callable

from ..import_utils import unavailable_class
from ..version_utils import has_minimal_version

try:
    if has_minimal_version("bliss", "2.2"):
        from flint.client.plots import BasePlot
        from flint.client.plots import ImageView
        from flint.client.plots import Plot1D
        from flint.client.proxy import FlintClient
    else:
        from bliss.flint.client.plots import BasePlot
        from bliss.flint.client.plots import ImageView
        from bliss.flint.client.plots import Plot1D
        from bliss.flint.client.proxy import FlintClient
except ImportError as ex:
    FlintClient = unavailable_class(ex)
    BasePlot = unavailable_class(ex)
    ImageView = unavailable_class(ex)
    Plot1D = unavailable_class(ex)

logger = logging.getLogger(__name__)


def capture_errors(method) -> Callable:
    @wraps(method)
    def wrapper(*args, **kw):
        try:
            return method(*args, **kw)
        except Exception as e:
            msg = f"Flint plot error: {e}"
            logger.error(msg, exc_info=True)

    return wrapper
