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

import pprint
import os
import sys

import gridfs

import click

from widukind_common.utils import get_mongo_url, get_mongo_client, configure_logging

from dlstats import version

DLSTATS_SETTINGS = dict(auto_envvar_prefix='DLSTATS')

opt_mongo_url = click.option('--mongo-url',
                             envvar='WIDUKIND_MONGODB_URL',
                             default=get_mongo_url(),
                             show_default=True,
                             help="URL for MongoDB connection.")

opt_silent = click.option('--silent', '-S', is_flag=True,
                          help="Suppress confirm")

opt_debug = click.option('--debug', '-D', is_flag=True)

opt_verbose = click.option('-v', '--verbose', is_flag=True,
                           help='Enables verbose mode.')

opt_pretty = click.option('--pretty', is_flag=True,
                          help='Pretty display.')

opt_logger = click.option('--log-level', '-l', 
                          required=False, 
                          type=click.Choice(['DEBUG', 'WARN', 'ERROR', 'INFO', 
                                             'CRITICAL']),
                          default='ERROR', 
                          help='Logging level')

opt_logger_conf = click.option('--log-config', 
                               type=click.Path(exists=True), 
                               help='Logging config filepath')

cmd_folder = os.path.abspath(
                    os.path.join(os.path.dirname(__file__), 'commands'))

class Context(object):
    def __init__(self, mongo_url=None, verbose=False,
                 log_level='ERROR', log_config=None, 
                 debug=False, silent=False, pretty=False):
        self.verbose = verbose
        self.debug = debug
        self.silent = silent
        self.pretty = pretty
        self.mongo_url = mongo_url
        
        self.log_level = log_level
        self.log_config = log_config
        
        if self.verbose:
            self.log_level = 'INFO'
        
        self.client_mongo = None
        self.db_mongo = None
        self.client_es = None
        self.fs_mongo = None
        
        self.logger = configure_logging(debug=self.debug, 
                                #stdout_enable, 
                                config_file=self.log_config, 
                                level=self.log_level)

    def log(self, msg, *args):
        """Logs a message to stderr."""
        if args:
            msg %= args
        click.echo(msg, file=sys.stderr)

    def log_error(self, msg, *args):
        """Logs a message to stderr."""
        if args:
            msg %= args
        click.echo(click.style(msg, fg='red'), file=sys.stderr)

    def log_ok(self, msg, *args):
        """Logs a message to stdout."""
        if args:
            msg %= args
        click.echo(click.style(msg, fg='green'), file=sys.stdout)

    def log_warn(self, msg, *args):
        """Logs a message to stdout."""
        if args:
            msg %= args
        click.echo(click.style(msg, fg='yellow'), file=sys.stdout)

    def vlog(self, msg, *args):
        """Logs a message to stderr only if verbose is enabled."""
        if self.verbose:
            self.log(msg, *args)

    def pretty_print(self, obj):
        pprint.pprint(obj)

    def mongo_client(self):
        if not self.client_mongo:
            self.client_mongo = get_mongo_client(self.mongo_url)
        return self.client_mongo
    
    def mongo_database(self):
        if not self.db_mongo:
            self.db_mongo = self.mongo_client().get_default_database()
        return self.db_mongo
    
    def mongo_fs(self):
        db = self.mongo_database()
        if not self.fs_mongo:
            self.fs_mongo = gridfs.GridFS(db)
        return self.fs_mongo

class ComplexCLI(click.MultiCommand):
    def list_commands(self, ctx):
        rv = []
        for filename in os.listdir(cmd_folder):
            if filename.endswith('.py') and \
               filename.startswith('cmd_'):
                rv.append(filename[4:-3])
        rv.sort()
        return rv

    def get_command(self, ctx, name):
        try:
            mod = __import__('dlstats.commands.cmd_' + name, None, None,
                             ['cli'])
        except ImportError:
            return
        return mod.cli


@click.command(cls=ComplexCLI)
@click.version_option(version=version.version_str(),
                      prog_name="dlstats",
                      message="%(prog)s %(version)s")
def cli():
    pass


def main():
    cli()

if __name__ == "__main__":
    """
    DLSTATS_DEBUG=True dlstats fetchers run -v -S -f BIS
    same:
    dlstats fetchers run --debug -v -S -f BIS
    """

    main()

@@ -1239,27 +1239,241 @@
     
-help='Logging level
+show_default=True,%0A                          help='Logging level')%0A%0Aopt_logger_file = click.option('--log-file', %0A                               type=click.Path(exists=False), %0A                               help='log file for output
 ')%0A%0A
@@ -1880,16 +1880,47 @@
 g=None, 
+log_file=None,                 
 %0A       
@@ -2203,16 +2203,49 @@
 _config%0A
+        self.log_file = log_file%0A
         
@@ -2611,24 +2611,24 @@
 og_config, %0A
-
             
@@ -2668,16 +2668,302 @@
 g_level)
+%0A        if self.log_file:%0A            from logging import FileHandler%0A            handler = FileHandler(filename=self.log_file)%0A            if self.logger.hasHandlers():%0A                handler.setFormatter(self.logger.handlers%5B0%5D.formatter)%0A            self.logger.addHandler(handler)
 %0A%0A    de

add global log-file options for all commands