from i3pystatus import IntervalModule
from i3pystatus.core.command import run_through_shell

__author__ = 'Pluggi'


class Wireguard(IntervalModule):
    """
    Monitor Wireguard connections.

    .. note::
        You might want to add something like this to /etc/sudoers:

        ``%wheel ALL = NOPASSWD: /bin/systemctl start wg-quick@wg0.service,/bin/systemctl stop wg-quick@wg0.service``

    Formatters:

    * {vpn_name} — Same as setting.
    * {status} — Unicode up or down symbol.
    * {output} — Output of status_command.
    * {label} — Label for this connection, if defined.

    """

    color_up = "#00ff00"
    color_down = "#FF0000"
    status_up = '▲'
    status_down = '▼'
    format = "{vpn_name} {status}"

    status_command = "systemctl is-active wg-quick@%(vpn_name)s"
    vpn_up_command = "sudo /bin/systemctl start wg-quick@%(vpn_name)s.service"
    vpn_down_command = "sudo /bin/systemctl stop wg-quick@%(vpn_name)s.service"

    connected = False
    label = ''
    vpn_name = ''

    settings = (
        ("format", "Format string"),
        ("color_up", "VPN is up"),
        ("color_down", "VPN is down"),
        ("status_down", "Symbol to display when down"),
        ("status_up", "Symbol to display when up"),
        ("vpn_name", "Name of VPN"),
        ("vpn_up_command", "Command to bring up the VPN - default requires editing /etc/sudoers"),
        ("vpn_down_command", "Command to bring up the VPN - default requires editing /etc/sudoers"),
        ("status_command", "command to find out if the VPN is active"),
    )

    def init(self):
        if not self.vpn_name:
            raise Exception("vpn_name is required")

    def toggle_connection(self):
        if self.connected:
            command = self.vpn_down_command
        else:
            command = self.vpn_up_command
        run_through_shell(command % {'vpn_name': self.vpn_name})

    def on_click(self, button, **kwargs):
        self.toggle_connection()

    def run(self):
        command_result = run_through_shell(self.status_command % {'vpn_name': self.vpn_name})
        self.connected = command_result.rc == 0

        if self.connected:
            color, status = self.color_up, self.status_up
        else:
            color, status = self.color_down, self.status_down

        vpn_name = self.vpn_name
        label = self.label

        self.data = locals()
        self.output = {
            "full_text": self.format.format(**self.data),
            'color': color,
        }

@@ -774,26 +774,25 @@
 g-quick@
-%25(
+%7B
 vpn_name
 )s%22%0A    
@@ -783,18 +783,17 @@
 vpn_name
-)s
+%7D
 %22%0A    vp
@@ -839,34 +839,33 @@
 rt wg-quick@
-%25(
+%7B
 vpn_name
 )s.service%22%0A
@@ -844,34 +844,33 @@
 -quick@%7Bvpn_name
-)s
+%7D
 .service%22%0A    vp
@@ -925,18 +925,17 @@
 ick@
-%25(
+%7B
 vpn_name
 )s.s
@@ -934,10 +934,9 @@
 name
-)s
+%7D
 .ser
@@ -1842,37 +1842,40 @@
 hell(command
- %25 %7B'
+.format(
 vpn_name
 ': self.vpn_
@@ -1854,35 +1854,33 @@
 .format(vpn_name
-': 
+=
 self.vpn_name%7D)%0A
@@ -1876,17 +1876,17 @@
 vpn_name
-%7D
+)
 )%0A%0A    d
@@ -2041,21 +2041,24 @@
 mand
- %25 %7B'
+.format(
 vpn_name
 ': s
@@ -2053,19 +2053,17 @@
 vpn_name
-': 
+=
 self.vpn
@@ -2067,17 +2067,17 @@
 vpn_name
-%7D
+)
 )%0A      

Use str.format instead of C-style format