import subprocess

# Initialize logging
from libqtile.log_utils import logger


class State:
    ON = "on"
    OFF = "off"
    LIST = ["on", "off"]


class Mode:
    RANDOM = "random"
    CUSTOM = "custom"
    BREATHE = "breathe"
    CYCLE = "cycle"
    WAVE = "wave"
    DANCE = "dance"
    TEMPO = "tempo"
    FLASH = "flash"
    LIST = ["random", "custom", "breathe", "cycle", "wave", "dance", "tempo", "flash"]


class Brightness:
    LOW = 0
    MEDIUM = 1
    HIGH = 2
    FULL = 3
    LIST = [0, 1, 2, 3]


class Side:
    LEFT = "left"
    MIDDLE = "middle"
    RIGHT = "right"
    ALL = "all"
    LIST = ["left", "middle", "right", "all"]


class Color:
    BLACK = "black"
    BLUE = "blue"
    RED = "red"
    MAGENTA = "magenta"
    GREEN = "green"
    CYAN = "cyan"
    YELLOW = "yellow"
    WHITE = "white"
    LIST = ["black", "blue", "red", "magenta", "green", "cyan", "yellow", "white"]


def handle_focus_change(theme):
    qtile = theme.qtile
    window = qtile.currentWindow if qtile else None

    if window:
        wm_class = window.window.get_wm_class() or None
        name = window.name

        if wm_class:
            theme.log_info("{}, {}".format(wm_class, name))

            # Check which window we entered and do some special effects if it is a special window.

            # Make keyboard red/white (pink) while in Osu!
            if "osu!.exe" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'left': Color.WHITE,
                    'middle': Color.RED,
                    'right': Color.WHITE,
                }))
            elif "Firefox" in wm_class[1]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'left': Color.WHITE,
                    'middle': Color.BLUE,
                    'right': Color.WHITE,
                }))
            elif "Thunderbird" in wm_class[1]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'left': Color.BLUE,
                    'middle': Color.WHITE,
                    'right': Color.BLUE,
                }))
            elif "pycharm" in wm_class[1]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.MEDIUM,
                    'left': Color.WHITE,
                    'middle': Color.GREEN,
                    'right': Color.WHITE,
                }))
            elif "franz" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.MEDIUM,
                    'left': Color.BLUE,
                    'middle': Color.WHITE,
                    'right': Color.BLUE,
                }))
            elif "quasselclient" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.MEDIUM,
                    'left': Color.BLUE,
                    'middle': Color.BLUE,
                    'right': Color.BLUE,
                }))
            elif "remmina" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'left': Color.RED,
                    'middle': Color.GREEN,
                    'right': Color.BLUE,
                }))
            elif "spotify" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'left': Color.GREEN,
                    'middle': Color.GREEN,
                    'right': Color.GREEN,
                }))
            elif "pulseeffects" in wm_class[0]:
                BacklightController.reset_backlight(state=KeyboardState(values={
                    'brightness': Brightness.FULL,
                    'mode': Mode.RANDOM,
                }))
            else:
                BacklightController.reset_backlight()


class KeyboardState:
    _instance = None

    state = State.ON
    mode = Mode.CUSTOM
    brightness = Brightness.LOW
    left = Color.WHITE
    middle = Color.WHITE
    right = Color.WHITE

    def __init__(self, values=None):
        """
        :param values: Default values
        :type values: dict
        """
        if values is not None:
            keys = values.keys()
            if 'state' in keys:
                self.state = values['state']
            if 'mode' in keys:
                self.mode = values['mode']
            if 'brightness' in keys:
                self.brightness = values['brightness']
            if 'left' in keys:
                self.left = values['left']
            if 'middle' in keys:
                self.middle = values['middle']
            if 'right' in keys:
                self.right = values['right']

    def __str__(self):
        return "KBState({}, {}, {}, {}, {}, {})".format(
            self.state, self.mode, self.brightness, self.left, self.middle, self.right
        )

    def get_copy(self):
        c = KeyboardState()
        c.state = self.state
        c.mode = self.mode
        c.brightness = self.brightness
        c.left = self.left
        c.middle = self.middle
        c.right = self.right
        return c

    @classmethod
    def get_instance(cls):
        """
        :rtype: KeyboardState
        """
        if cls._instance is None:
            cls._instance = KeyboardState()
        return cls._instance


class BacklightController:

    @staticmethod
    def reset_backlight(force=False, state=None):
        """
        Resets the keyboard backlight to the default colors / states
        :param force: Force the reset
        :type force: bool
        :param state: A state to reset to
        :type state: KeyboardState
        """
        if state is None:
            # Create state with default values.
            state = KeyboardState()

        logger.debug("Resetting KB backlight to {}".format(state))

        flags = [BacklightController.set_colors([state.left, state.middle, state.right], force),
                 BacklightController.set_brightness(state.brightness, force),
                 BacklightController.set_state(state.state, force),
                 BacklightController.set_mode(state.mode, force)]

        BacklightController.exec_flags(flags)

    @staticmethod
    def exec_flags(flags):
        """
        Removes duplicate flags and executes the command with the resulting flags, and
        updates the current keyboard state.
        :param flags: List of list of flags, to be executed.
        :return: The return code of the execution
        """
        final_flags = {}
        changes = {}
        for flag in flags:
            for (k, v) in flag:
                final_flags[k] = v
                if k == "-p":
                    changes['state'] = v
                elif k == "-t":
                    changes['mode'] = v
                elif k == "-b":
                    changes['brightness'] = v
                elif k == "-l":
                    changes['left'] = v
                elif k == "-m":
                    changes['middle'] = v
                elif k == "-r":
                    changes['right'] = v
                elif k == "-c":
                    changes['left'] = v
                    changes['middle'] = v
                    changes['right'] = v

        args = []
        for (k, v) in final_flags.items():
            args.append(k)
            args.append(v)

        res = BacklightController._call(args)
        if res == 0:
            # Update state
            css = KeyboardState.get_instance()
            for (k, v) in changes.items():
                css.__setattr__(k, v)

    @staticmethod
    def set_state(state, force=False):
        """
        Turns the backlight on or off
        :param state: State you want ('on' or 'off')
        :type state: str
        :param force: Force execution.
        :type force: bool
        """
        if state not in State.LIST:
            return

        logger.debug("Setting KB state to {}".format(state))

        css = KeyboardState.get_instance()

        if css.state != state or force:
            return [('-p', state)]

        return []

    @staticmethod
    def set_mode(mode, force=False):
        """
        Set the backlight mode
        :param mode: One of "random", "custom", "breathe", "cycle", "wave", "dance", "tempo" or "flash"
        :type mode: str
        :param force: Force execution.
        :type force: bool
        """
        if mode not in Mode.LIST:
            return

        logger.debug("Setting KB mode to {}".format(mode))

        css = KeyboardState.get_instance()
        if css.mode != mode or force:
            return [('-t', mode)]

        return []

    @staticmethod
    def set_brightness(level, force=False):
        """
        Set the brightness level
        :param level: Brightness (0 to 3)
        :type level: int
        :param force: Force execution.
        :type force: bool
        """
        if level not in Brightness.LIST:
            return

        logger.debug("Setting KB brightness to {}".format(level))

        css = KeyboardState.get_instance()
        if css.brightness != level or force:
            return [('-b', '{}'.format(level))]

        return []

    @staticmethod
    def set_color(side, color, force=False):
        """
        Set the backlight color
        :param side: Side of backlight to change, from left, middle, right or all.
        :type side: str
        :param color: The new color, one of "black", "blue", "red", "magenta", "green", "cyan", "yellow" or "white"
        :type color: str
        :param force: Force execution.
        :type force: bool
        """
        if side not in Side.LIST:
            return

        if color not in Color.LIST:
            return

        logger.debug("Setting KB side {} to color {}".format(side, color))

        css = KeyboardState.get_instance()

        if side == "all":
            if css.left != color or css.right != color or css.right != color or force:
                return [('-c', color)]
        elif side == "left":
            if css.left != color or force:
                return [('-l', color)]
        elif side == "right":
            if css.right != color or force:
                return [('-r', color)]
        elif side == "middle":
            if css.middle != color or force:
                return [('-m', color)]

        return []

    @staticmethod
    def set_colors(colors, force=False):
        """
        Set the backlight colors in one go
        :param colors: The new colors, list of three colors, [left, middle, right]. Colors must be one of
                        "black", "blue", "red", "magenta", "green", "cyan", "yellow" or "white"
        :type colors: list
        :param force: Force execution.
        :type force: bool
        """
        if len(colors) != 3:
            return

        for color in colors:
            if color not in Color.LIST:
                return

        logger.debug("Setting KB colors to {}, {}, {}".format(colors[0], colors[1], colors[2]))

        css = KeyboardState.get_instance()

        if css.left != colors[0] or css.middle != colors[1] or css.right != colors[2] or force:
            return [('-l', '{}'.format(colors[0])),
                    ('-m', '{}'.format(colors[1])),
                    ('-r', '{}'.format(colors[2]))]

        return []

    @staticmethod
    def _call(args):
        """
        Call the script.
        :param args: Arguments to the script
        :type args: list
        :return The exit code of the script
        :rtype: int
        """
        logger.debug("Calling kb_backlight' with args {}".format(args))
        return subprocess.call(["sudo", "/home/kevin/bin/kb_backlight"] + args)