import sys
import traceback

from lrm.LrmLogging import lrm_log
from lrm.Dialogs import DialogWrapper


def lrm_slot(func):
    # @wraps(func)
    def wrapper(self): #, *args, **kwargs):
        # print("wrapper", str(self), str(args), str(kwargs))
        try:
            func(self) #, *args, **kwargs)
        except BaseException as b:
            msg = "Unexpected exception"
            detail = traceback.format_exc()
            lrm_log.error(msg + "\n\t" + detail.replace("\n", "\n\t"))
            dlg = DialogWrapper(None)
            dlg.error(msg, detail=detail)
            sys.exit(-1)

    return wrapper


def lrm_slot_param(func):
    # @wraps(func)
    def wrapper(self, _dummy, *args, **kwargs):
        # print("wrapper", str(self), str(_dummy), str(args), str(kwargs))
        try:
            func(self, _dummy, *args, **kwargs)
        except BaseException as b:
            msg = "Unexpected exception"
            detail = traceback.format_exc()
            lrm_log.error(msg + "\n\t" + detail.replace("\n", "\n\t"))
            dlg = DialogWrapper(None)
            dlg.error(msg, detail=detail)
            sys.exit(-1)

    return wrapper


def lrm_slot_result(func):
    # @wraps(func)
    def wrapper(self, *args, **kwargs):
        # print("wrapper", str(self), str(args), str(kwargs))
        try:
            return func(self, *args, **kwargs)
        except BaseException as b:
            msg = "Unexpected exception"
            detail = traceback.format_exc()
            lrm_log.error(msg + "\n\t" + detail.replace("\n", "\n\t"))
            dlg = DialogWrapper(None)
            dlg.error(msg, detail=detail)
            sys.exit(-1)

    return wrapper


def lrm_slot_param_return(func):
    # @wraps(func)
    def wrapper(self, _dummy, *args, **kwargs):
        # print("wrapper", str(self), str(_dummy), str(args), str(kwargs))
        try:
            return func(self, _dummy, *args, **kwargs)
        except BaseException as b:
            msg = "Unexpected exception"
            detail = traceback.format_exc()
            lrm_log.error(msg + "\n\t" + detail.replace("\n", "\n\t"))
            dlg = DialogWrapper(None)
            dlg.error(msg, detail=detail)
            sys.exit(-1)
            return None

    return wrapper
