#!/usr/bin/python3 -u
"""
Description: Toggle display isolation on active display
Author: thnikk
"""
from subprocess import run
import json


class Sway():
    def __init__(self):
        self.monitors = self.get_monitors()
        self.focused = self.get_focused()

    def get_monitors(self):
        """ Get monitors """
        return json.loads(run(
            ["swaymsg", "-t", "get_outputs"],
            check=True, capture_output=True).stdout.decode('utf-8'))

    def get_focused(self):
        """ Get focused montior """
        for monitor in self.monitors:
            if monitor["focused"]:
                return monitor["name"]

    def enable_all(self):
        """ Enable all monitors """
        run(
            ["swaymsg", "output", "*", "enable"],
            check=True, capture_output=False)

    def get_active(self):
        """ Get active monitors """
        return [
            monitor for monitor in self.monitors
            if monitor['active']
        ]

    def disable(self, name):
        """ Disable monitor """
        run([
            "swaymsg", "output", name, "disable"
        ], check=True, capture_output=False)

    def isolate(self):
        """ Isolate focused monitor """
        for monitor in self.monitors:
            if monitor["name"] != self.focused:
                self.disable(monitor["name"])


def main():
    """ Main function """
    sway = Sway()
    if len(sway.get_active()) > 1:
        sway.isolate()
    else:
        sway.enable_all()


if __name__ == "__main__":
    main()
