"""Cmdlr command line interface."""

import argparse
import textwrap
import sys

from .info import DESCRIPTION
from .info import VERSION

from .conf import Config
from .amgr import AnalyzerManager
from .cmgr import ComicManager
from .loopmgr import LoopManager

from .infoprint import print_analyzer_info
from .infoprint import print_not_matched_urls
from .infoprint import print_comic_info


def _parser_setting():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        description=textwrap.fill(DESCRIPTION, 70))

    parser.add_argument(
        '--version', action='version',
        version='.'.join(map(lambda x: str(x), VERSION)))

    parser.add_argument(
        'urls', metavar='URL', type=str, nargs='*',
        help=('select some books which want to process.\n'
              'if no urls are given, select all subscribed books.\n'
              'if some urls haven\'t been subscribed,'
              ' subscrube these now.\n'
              'more process depend on which flags be given.'))

    parser.add_argument(
        '-m', '--update-meta', dest='update_meta', action='store_true',
        help='request update meta, not only when subscribe.')

    parser.add_argument(
        '-d', '--download', dest='download', action='store_true',
        help='download the volumes files.')

    parser.add_argument(
        '-s', '--skip-download-errors',
        dest='skip_download_errors', action='store_true',
        help=('generate volume files even if some images fetch failed.\n'
              'may cause incomplete volume files, so use carefully.\n'
              'must using with --download flag.'))

    parser.add_argument(
        '-l', '--list', dest='list', action='store_true',
        help=('list exists comics info.\n'
              'also display extra data if URLs are given.\n'
              'this flag will prevent any current status change.'))

    parser.add_argument(
        '-a', dest='analyzer', nargs='?', type=str,
        default=argparse.SUPPRESS,
        help=('list all enabled analyzers.\n'
              'or print the detail if give a name.\n'))

    return parser


def _get_args():
    parser = _parser_setting()
    args = parser.parse_args()

    if args.skip_download_errors and not args.download:
        print('Please use -s options with -d options.', file=sys.stderr)
        sys.exit(1)

    if not args.urls and not sys.stdin.isatty():  # Get URLs from stdin
        args.urls = [url for url in sys.stdin.read().split() if url]

    elif len(sys.argv) == 1:
        print(('Please give at least one arguments or flags.'
               ' Use "-h" for more info.'),
              file=sys.stderr)
        sys.exit(1)

    return args


def main():
    """Command ui entry point."""
    args = _get_args()

    config = Config()
    config_filepaths = [Config.default_config_filepath]
    config.load_or_build(*config_filepaths)

    amgr = AnalyzerManager(config)

    if 'analyzer' in args:
        print_analyzer_info(amgr.get_analyzer_infos(), args.analyzer)
        return

    print_not_matched_urls(amgr, args.urls)

    cmgr = ComicManager(config, amgr)

    if args.list:
        url_to_comics = cmgr.get_url_to_comics(args.urls)
        print_comic_info(url_to_comics, detail_mode=args.urls)

    else:
        lmgr = LoopManager(config.max_concurrent)

        ctrl = {
            'update_meta': args.update_meta,
            'download': args.download,
            'skip_download_errors': args.skip_download_errors
        }

        lmgr.start(config, amgr, cmgr, args.urls, ctrl)

@@ -781,40 +781,33 @@
 ect 
-some books which want to
+which ones should
  process
 .%5Cn'
@@ -802,16 +802,19 @@
  process
+ing
 .%5Cn'%0A   
@@ -829,29 +829,28 @@
    '
-if no urls ar
+no one b
 e given
-,
+ mean
  sel
@@ -861,24 +861,16 @@
 all 
-subscribed books
+existing
 .%5Cn'
@@ -889,146 +889,61 @@
    '
-if some urls haven%5C't been subscribed,'%0A              ' subscrube these now.%5Cn'%0A              'more process depend on which flags be given
+perform subscription if a url hasn%5C't been subscribed
 .'))
@@ -1059,16 +1059,8 @@
 lp='
-request 
 upda
@@ -1070,33 +1070,34 @@
 meta
-, not only when subscribe
+data from remote addresses
 .')%0A
@@ -1221,17 +1221,16 @@
 e volume
-s
  files.'
@@ -1374,15 +1374,22 @@
 p=('
-generat
+force build th
 e vo
@@ -1416,20 +1416,16 @@
 ome 
-images fetch
+download
  fai
@@ -1457,16 +1457,21 @@
 y cause 
+some 
 incomple
@@ -1489,26 +1489,8 @@
 iles
-, so use carefully
 .%5Cn'
@@ -1649,14 +1649,11 @@
 ist 
-exists
+all
  com
@@ -1684,31 +1684,22 @@
    '
-also display extra data
+show more info
  if 
@@ -1707,11 +1707,11 @@
 RLs 
-are
+was
  giv
@@ -1764,28 +1764,31 @@
 ny c
-urrent status change
+hange in current system
 .'))

tweak help messages