from . import HAS_IMPORTLIB_RESOURCE
from ..Qt import QtGui, QtWidgets, QtCore


class DarkPalette(QtGui.QPalette):
    def __init__(self):
        super().__init__()

        self.setColor(QtGui.QPalette.Base, QtGui.QColor(42, 42, 42))
        self.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(66, 66, 66))
        self.setColor(QtGui.QPalette.Window, QtGui.QColor(93, 93, 93))
        self.setColor(QtGui.QPalette.ToolTipBase, QtGui.QColor(93, 93, 93))
        self.setColor(QtGui.QPalette.Button, QtGui.QColor(93, 93, 93))
        
        self.setColor(QtGui.QPalette.WindowText, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.ToolTipText, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.Text, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.BrightText, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.ButtonText, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor(220, 220, 220))
        
        self.setColor(QtGui.QPalette.Shadow, QtGui.QColor(20, 20, 20))
        self.setColor(QtGui.QPalette.Dark, QtGui.QColor(35, 35, 35))
        
        self.setColor(QtGui.QPalette.Highlight, QtGui.QColor(42, 130, 218))
        self.setColor(QtGui.QPalette.Link, QtGui.QColor(220, 220, 220))
        self.setColor(QtGui.QPalette.LinkVisited, QtGui.QColor(108, 180, 218))
        
        # disabled
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Base, QtGui.QColor(80, 80, 80))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Window, QtGui.QColor(80, 80, 80))
        
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Text, QtGui.QColor(127, 127, 127))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, QtGui.QColor(127, 127, 127))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.HighlightedText, QtGui.QColor(127, 127, 127))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, QtGui.QColor(127, 127, 127))
        
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, QtGui.QColor(80, 80, 80))


class LightPalette(QtGui.QPalette):
    def __init__(self):
        super().__init__()
        
        self.setColor(QtGui.QPalette.Base, QtGui.QColor(237, 237, 237))
        self.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor(225, 225, 225))
        self.setColor(QtGui.QPalette.Window, QtGui.QColor(240, 240, 240))
        self.setColor(QtGui.QPalette.ToolTipBase, QtGui.QColor(240, 240, 240))
        self.setColor(QtGui.QPalette.Button, QtGui.QColor(240, 240, 240))
        
        self.setColor(QtGui.QPalette.WindowText, QtGui.QColor(0, 0, 0))
        self.setColor(QtGui.QPalette.Text, QtGui.QColor(0, 0, 0))
        self.setColor(QtGui.QPalette.BrightText, QtGui.QColor(0, 0, 0))
        self.setColor(QtGui.QPalette.ButtonText, QtGui.QColor(0, 0, 0))
        self.setColor(QtGui.QPalette.ToolTipText, QtGui.QColor(0, 0, 0))
        self.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor(0, 0, 0))
        
        self.setColor(QtGui.QPalette.Shadow, QtGui.QColor(20, 20, 20))
        self.setColor(QtGui.QPalette.Dark, QtGui.QColor(225, 225, 225))
        
        self.setColor(QtGui.QPalette.Highlight, QtGui.QColor(218, 66, 42))
        self.setColor(QtGui.QPalette.Link, QtGui.QColor(0, 162, 232))
        self.setColor(QtGui.QPalette.LinkVisited, QtGui.QColor(222, 222, 222))
        
        # disabled
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Base, QtGui.QColor(115, 115, 115))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Window, QtGui.QColor(115, 115, 115))
        
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, QtGui.QColor(115, 115, 115))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Text, QtGui.QColor(115, 115, 115))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, QtGui.QColor(115, 115, 115))
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.HighlightedText, QtGui.QColor(115, 115, 115))
        
        self.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Highlight, QtGui.QColor(190, 190, 190))


class MyProxyStyle(QtWidgets.QProxyStyle):
    def __init__(self, color):
        super().__init__()

        if color == 'dark':
            self._palette = DarkPalette()
        else:
            self._palette = LightPalette()

    def polish(self, obj):
        if isinstance(obj, QtGui.QPalette):
            return self._palette

        elif isinstance(obj, QtWidgets.QApplication):
            if HAS_IMPORTLIB_RESOURCE:
                from importlib.resources import path
                with path('resources.icons', 'style.qss') as fp:
                    with fp.open('r') as f:
                        obj.setStyleSheet(f.read())
            else:
                from pkg_resources import resource_filename
                with open(resource_filename('resources.icons', 'style.qss'), 'r') as f:
                    obj.setStyleSheet(f.read())
        else:
            return super().polish(obj)

    def drawComplexControl(self, control: QtWidgets.QStyle.ComplexControl,
                           opts: QtWidgets.QStyleOptionComplex, painter: QtGui.QPainter, widget=None):
        if control == QtWidgets.QStyle.CC_ComboBox:
            textrect = self.baseStyle().subControlRect(QtWidgets.QStyle.CC_ComboBox, opts, QtWidgets.QStyle.SC_ComboBoxEditField, widget)
            opts.currentText = painter.fontMetrics().elidedText(opts.currentText, QtCore.Qt.ElideRight, textrect.width())
            super().drawComplexControl(control, opts, painter, widget=widget)
        else:
            super().drawComplexControl(control, opts, painter, widget=widget)
