# -*- coding: utf-8 -*-
'''
Module for configuring Windows Firewall
'''

# Import python libs
import re

# Import salt libs
import salt.utils

# Define the module's virtual name
__virtualname__ = 'firewall'


def __virtual__():
    '''
    Only works on Windows systems
    '''
    if salt.utils.is_windows():
        return __virtualname__
    return False


def get_config():
    '''
    Get the status of all the firewall profiles

    CLI Example:

    .. code-block:: bash

        salt '*' firewall.get_config
    '''
    profiles = {}
    curr = None

    cmd = 'netsh advfirewall show allprofiles'
    for line in __salt__['cmd.run'](cmd).splitlines():
        if not curr:
            tmp = re.search('(.*) Profile Settings:', line)
            if tmp:
                curr = tmp.group(1)
        elif line.startswith('State'):
            profiles[curr] = line.split()[1] == 'ON'
            curr = None

    return profiles


def disable():
    '''
    Disable all the firewall profiles

    CLI Example:

    .. code-block:: bash

        salt '*' firewall.disable
    '''
    return __salt__['cmd.run'](
            'netsh advfirewall set allprofiles state off'
            ) == 'Ok.'


def get_rule(name="all"):
    '''
    Get firewall rule(s) info

    CLI Example:

    .. code-block:: bash

        salt '*' firewall.get_rule "MyAppPort"
    '''
    ret = {}
    cmd = 'netsh advfirewall firewall show rule name="{0}"'.format(name)
    ret[name] = __salt__['cmd.run'](cmd)

    if ret[name].strip() == "No rules match the specified criteria.":
        ret = False

    return ret


def add_rule(name, localport, protocol="tcp", action="allow", dir="in"):
    '''
    Add a new firewall rule

    CLI Example:

    .. code-block:: bash

        salt '*' firewall.add_rule "test" "tcp" "8080"
    '''
    cmd = 'netsh advfirewall firewall add rule name="{0}" protocol={1} dir={2} localport={3} action={4}'.format(name, protocol, dir, localport, action)
    return __salt__['cmd.run'](cmd) == 'Ok.'

@@ -554,39 +554,43 @@
 None%0A%0A    cmd = 
+%5B
 'netsh
- 
+', '
 advfirewall show
@@ -584,22 +584,28 @@
 firewall
- show 
+', 'show', '
 allprofi
@@ -608,16 +608,17 @@
 rofiles'
+%5D
 %0A    for
@@ -649,16 +649,36 @@
 un'%5D(cmd
+, python_shell=False
 ).splitl
@@ -1117,55 +1117,25 @@
     
-return __salt__%5B'cmd.run'%5D(%0A            
+cmd = %5B
 'netsh
- 
+', '
 advf
@@ -1145,13 +1145,19 @@
 wall
- set 
+', 'set', '
 allp
@@ -1167,32 +1167,81 @@
 iles
- 
+', '
 state
- 
+', '
 off'
+%5D
 %0A    
-        
+return __salt__%5B'cmd.run'%5D(cmd, python_shell=False
 ) ==
@@ -1428,39 +1428,43 @@
  = %7B%7D%0A    cmd = 
+%5B
 'netsh
- 
+', '
 advfirewall fire
@@ -1458,25 +1458,28 @@
 firewall
- 
+', '
 firewall
  show ru
@@ -1474,29 +1474,36 @@
 wall
- show rule 
+', 'show', 'rule', '
 name=
-%22
 %7B0%7D
-%22
 '.fo
@@ -1512,16 +1512,17 @@
 at(name)
+%5D
 %0A    ret
@@ -1553,16 +1553,36 @@
 un'%5D(cmd
+, python_shell=False
 )%0A%0A    i
@@ -1915,15 +1915,19 @@
 d = 
+%5B
 'netsh
- 
+', '
 advf
@@ -1933,25 +1933,28 @@
 firewall
- 
+', '
 firewall
  add rul
@@ -1949,122 +1949,225 @@
 wall
- add rule 
+', 'add', 'rule',%0A           '
 name=
-%22
 %7B0%7D
-%22 protocol=%7B1%7D dir=%7B2%7D localport=%7B3%7D action=%7B4%7D'.format(name, protocol, dir, localport, 
+'.format(name),%0A           'protocol=%7B0%7D'.format(protocol),%0A           'dir=%7B0%7D'.format(dir),%0A           'localport=%7B0%7D'.format(localport),%0A           'action=%7B0%7D'.format(
 action)
+%5D
 %0A   
@@ -2197,16 +2197,36 @@
 un'%5D(cmd
+, python_shell=False
 ) == 'Ok

split win_firewall commands