#!/usr/bin/env python
# Mixcoatl needs to be updated to print destination and permission.

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", "Destination", "Protocol", "From Port", "To Port", "Type", "Permit"])
        for rule in rules:
            start_port = rule.start_port if hasattr(rule, 'start_port') else None
            end_port = rule.end_port if hasattr(rule, 'end_port') else None
            firewall_rules_table.add_row([rule.firewall_rule_id, rule.network_address, "destination ?", rule.protocol, start_port, end_port, rule.direction, "permit ?"])
        print(firewall_rules_table)
