#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Show ip info based on mask of network.

The ipy-show get an ip and mask in slash notation and calcule the network,
broadcast, number of subnets and number max of hosts.
The name ipy-show was concept when i told my work friend @neriberto and he told
me that this name is so gay, and i love it.

PS: Explain the name:
  In portuguese, 'fell in love' is writen with 'apaixonou' and 'ip-show'+'nou'
  in portuguese have same sound than 'apaixonou'.
"""


import re
import sys
from ipy_show import return_ip_data


def check_ip():
    """Check ip/mask and print help if it is wrong or empty."""
    _help = """
The ipy-show gets an ip and mask in slash notation.
IP can not be 0.0.0.0 and mask must be in range 1, 32.
 Usage: ipy-show <ip/mask>
 Example: ipy-show 10.47.31.5/11
 """
    url_pattern = re.compile(r"([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}")
    if len(sys.argv) < 2 or not bool(url_pattern.match(sys.argv[1])) or\
            int(sys.argv[1].split('/')[1]) not in list(range(1, 33)):
        print(_help)
        sys.exit(0)


def print_output(ip_data):
    """Print output of ip data."""
    print(" {0: ^55} ".format("_" * 55))
    print("|{0: ^55}|".format(""))
    print("|{0: ^55}|".format("IPY-Show"))
    print("|{0: ^55}|".format("_" * 55))
    print("|{0: ^55}|".format(""))
    for i in [(x, ip_data[x]) for x in ip_data]:
        print("| {0:<16}: ".format(i[0].replace("_", " "))
              + "{0: <35} |".format(i[1]))
    print("|{0: ^55}|".format("_" * 55))


def main():
    check_ip()
    ip, mask = sys.argv[1].split('/')
    get_ip_data = return_ip_data(ip, mask)
    print_output(get_ip_data)


if __name__ == "__main__":
    main()
