#!/usr/bin/env python3
from compose_plantuml import ComposePlantuml


if __name__ == '__main__':

    import argparse
    import sys

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--link-graph', action='store_const', const=True,
        help='prints a link graph', default=False, )
    parser.add_argument(
        '--boundaries', action='store_const', const=True,
        help='prints the system boundaries',
        default=False,
    )
    parser.add_argument(
        '--port_boundaries', action='store_const', const=True,
        help='prints the port system boundaries',
        default=False,
    )
    parser.add_argument(
        '--volume_boundaries', action='store_const', const=True,
        help='prints the port system boundaries',
        default=False,
    )
    parser.add_argument(
        '--notes', action='store_const', const=True,
        help='utilize notes for displaying additional information',
        default=False,
    )
    parser.add_argument(
        '--group', action='store_const', const=True,
        help='group similar properties together',
        default=False,
    )
    parser.add_argument('files', nargs=argparse.REMAINDER)
    args = parser.parse_args()
    plantuml = ComposePlantuml()

    if not args.link_graph and not args.boundaries and not args.port_boundaries and not args.volume_boundaries:
        print('specify an output option. e.g.: link_graph or boundaries')
        sys.exit(1)

    def execute(data):
        parsed = plantuml.parse(data)

        if args.link_graph:
            print(plantuml.link_graph(parsed, notes=args.notes))
        if args.boundaries:
            print(plantuml.boundaries(parsed, args.group, notes=args.notes))
        if args.port_boundaries:
            print(plantuml.boundaries(parsed, args.group, notes=args.notes, ports=True, volumes=False))
        if args.volume_boundaries:
            print(plantuml.boundaries(parsed, args.group, notes=args.notes, ports=False, volumes=True))

    if len(args.files) == 0:
        execute(sys.stdin.read())

    for path in args.files:
        with open(path, 'r') as file:
            execute(file.read())
