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

"""
Create a JSON file containing quotes.

File is under EUPL1.2
Author: kujiu
"""

import json
import argparse
import logging

import sys
import os.path
import wikiquote

logger = logging.getLogger(__name__)

ch = logging.StreamHandler(sys.stderr)
ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
ch.setLevel(logging.WARNING)
logger.addHandler(ch)

def generate_quotes(srcfile):
    """
    Generate a dict with all quotes
    """
    quotes = {}
    with open(srcfile) as fin:
        sources_dict = json.load(fin)
    for lang in sources_dict:
        quotes.setdefault(lang, {})
        for category in sources_dict[lang]:
            quotes[lang].setdefault(category, {})
            for author in sources_dict[lang][category]:
                try:
                    quotes[lang][category][author] = wikiquote.quotes(
                        author, lang=lang
                    )
                except Exception:
                    logger.exception(
                        "Error when retrieving %s from %s/%s",
                        author, lang, category)
                    continue

                if not quotes[lang][category][author]:
                    logger.warning(
                        "Source %s from %s/%s has no quote.",
                        author, lang, category
                    )
                else:
                    logger.debug(
                        "Source %s from %s/%s: %d quotes.",
                        author, lang, category,
                        len(quotes[lang][category][author])
                    )
    return quotes

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="""
Generate a JSON file containing quotes from wikiquote.\n
\n
Source file must be a JSON in this format:\n
{\n
    'en': {'fantasy': ['Discworld', 'Terry Pratchett']},\n
    'fr': {'fantasy': ['Kaamelott/Arthur'], 'category2': ['Name']}\n
}\n
    """)
    parser.add_argument(
        '--source', default='~/.wikiquote-sources.json',
        help='JSON file with sources')
    parser.add_argument(
        '--dest', default='~/.fortunes.json',
        help='Destination file')
    parser.add_argument(
        '-d', '--debug', action='store_true', help="Debug mode")
    parser.add_argument(
        '-v', '--verbose', help="Verbose mode", action='store_true')
    options = parser.parse_args()

    class VerboseFilter(logging.Filter):
        def filter(self, rec):
            return rec.levelno in (logging.DEBUG, logging.INFO)

    if hasattr(options, 'debug') and options.debug:
        ch = logging.StreamHandler(sys.stdout)
        ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
        ch.setLevel(logging.DEBUG)
        ch.addFilter(VerboseFilter())
        logger.addHandler(ch)
        logger.setLevel(logging.DEBUG)

    elif hasattr(options, 'verbose') and options.verbose:
        ch = logging.StreamHandler(sys.stdout)
        ch.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
        ch.setLevel(logging.INFO)
        ch.addFilter(VerboseFilter())
        logger.addHandler(ch)
        logger.setLevel(logging.INFO)

    source = os.path.expanduser(
        os.path.expandvars(options.source)
    )

    dest = os.path.expanduser(
        os.path.expandvars(options.dest)
    )

    with open(dest, 'w') as fout:
        json.dump(generate_quotes(source), fout, indent=4)
