#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import logging
from isbntools import (meta, clean, canonical, get_canonical_isbn,
                       config, registry, quiet_errors)
from isbntools.dev.fmt import fmtbib, fmts

logging.basicConfig(level=logging.CRITICAL)


def usage(wservs="wcat|goob|...", ofmts="labels"):
    sys.stderr.write('Usage: isbn_meta ISBN [%s] [%s] [apikey]\n  '
                     '...  or try with '
                     'another service in list!\n' % (wservs, ofmts))
    sys.exit(1)


def parse_args(args):
    service = None
    api = None
    fmt = None
    isbn = get_canonical_isbn(canonical(clean(args[0])))
    if len(args) == 1:
        return (isbn, service, fmt, api)
    del args[0]
    providers = list(registry.services.keys())
    for f in fmts:
        if f in args:
            fmt = f
            args.remove(f)
            break
    for s in providers:
        if s in args:
            service = s
            args.remove(s)
            break
    api = args[0] if args else None
    return (isbn, service, fmt, api)


if __name__ == "__main__":
    sys.excepthook = quiet_errors
    try:
        isbn, service, fmt, apikey = parse_args(sys.argv[1:])
        if not isbn:
            raise
        service = service if service else 'default'
        fmt = fmt if fmt else 'labels'
        if apikey:
            try:
                config.add_apikey(service, apikey)
            except:
                pass
        r = meta(isbn, service)
        print((fmtbib(fmt, r)))
    except:
        providers = list(registry.services.keys())
        providers.remove('default')
        available = '|'.join(providers)
        fmts.remove('labels')
        ofmts = '|'.join(fmts)
        usage(available, ofmts)
