#!/usr/bin/env python

from mixcoatl.network.firewall import Firewall
from prettytable import PrettyTable
import argparse
import sys

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('firewallid', help='Firewall ID')
    parser.add_argument("--verbose", "-v", help="Produce verbose output", action="store_true")
    cmd_args = parser.parse_args()

    f = Firewall(cmd_args.firewallid)
    result = f.load()

    if result is not None:
        print("Cannot find the Firewall by the ID.")
        sys.exit(1)

    rules = f.rules
    if cmd_args.verbose:
        for rule in rules:
            rule.pprint()
    else:
        firewall_rules_table = PrettyTable(["Firewall Rule ID", "Source", "Source Type",
                                            "Destination", "Destination Type", "Protocol",
                                            "Direction", "Start Port", "End Port",
                                            "Permission", "Precedence"])
        for rule in rules:
            firewall_rules_table.add_row([rule.firewall_rule_id, rule.source, rule.source_type,
                                          rule.destination, rule.destination_type, rule.protocol,
                                          rule.direction, rule.start_port, rule.end_port,
                                          rule.permission, rule.precedence])
        print(firewall_rules_table)
