#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import algos
from sys import exit
import helpers as h
from helpers import *

def main():
    als = algos.get_algos()

    width, algo, flags = h.read_args(als)

    if flags.ls:
        for alg in sorted(als.keys()):
            print(h.algo2str(alg))
        exit(0)

    if flags.info:
        name = flags.info
        if name not in als:
            h.unknown_algo_error(name, als)

        fun, sd = als[flags.info]
        h.print_algo_info(name, fun, sd)
        exit(0)

    if algo not in als:
        h.unknown_algo_error(algo, als)

    fn, _ = als[algo]
    res = fn(read_words(width), width)

    if flags.justify:
        res = algos.justify(res, width)

    for line in res:
        # We're wrapping only one paragraph, if there are empty lines,
        # they must be at the end, so we discard them
        if line != []:
            print(' '.join(line))


if __name__ == '__main__':
    main()
