import os
from ngSkinTools2.python_compatibility import Object
from maya import OpenMayaUI as omui
from PySide2.QtWidgets import QWidget
from shiboken2 import wrapInstance

from PySide2 import QtWidgets, QtGui


def wrap_layout_into_widget(layout):
    w = QtWidgets.QWidget()
    w.setLayout(layout)
    return w


def signals_blocked(widget):
    return SignalBlockContext(widget)


class SignalBlockContext(Object):
    def __init__(self, widget):
        self.widget = widget

    def __enter__(self):
        self.prevState = self.widget.blockSignals(True)

    def __exit__(self, *args):
        self.widget.blockSignals(self.prevState)


class updateGuard(Object):
    def __init__(self):
        self.updating = False

    def __enter__(self):
        self.updating = True

    def __exit__(self, *args):
        self.updating = False


def on(*signals):
    """
    decorator for function: list signals that should fire for this function.

    instead of:

        def something():
            ...
        btn.clicked.connect(something)

    do:

        @qt.on(btn.clicked)
        def something():
            ...
    """

    def decorator(fn):
        for i in signals:
            i.connect(fn)
        return fn

    return decorator


class SingleWindowPolicy(Object):
    def __init__(self):
        self.lastWindow = None

    def setCurrent(self, window):
        if self.lastWindow:
            self.lastWindow.close()
        self.lastWindow = window

        on(window.finished)(self.cleanup)

    def cleanup(self):
        self.lastWindow = None


def alternative_palette_light():
    palette = QtGui.QPalette()
    palette.setColor(QtGui.QPalette.Window, QtGui.QColor(243, 244, 246))
    palette.setColor(QtGui.QPalette.WindowText, QtGui.QColor(33, 37, 41))
    return palette


def bind_action_to_button(action, button):
    """

    :type button: PySide2.QtWidgets.QPushButton
    :type action: PySide2.QtWidgets.QAction
    """

    @on(action.changed)
    def update_state():
        button.setText(action.text())
        button.setEnabled(action.isEnabled())
        button.setToolTip(action.toolTip())
        button.setStatusTip(action.statusTip())
        button.setVisible(action.isVisible())
        if action.isCheckable():
            button.setChecked(action.isChecked())

    button.setCheckable(action.isCheckable())

    on(button.clicked)(action.trigger)
    update_state()

    return button


images_path = os.path.join(os.path.dirname(__file__), "images")


def image_icon(file_name):
    return QtGui.QIcon(os.path.join(images_path, file_name))


mainWindow = wrapInstance(int(omui.MQtUtil.mainWindow()), QWidget)
