# Path: googleanalytics/commands/common.py
# @click.group(invoke_without_command=True)
# @click.option('--identity')
# @click.option('--account')
# @click.option('--webproperty')
# @click.option('--profile')
# @click.option('--version', is_flag=True)
# @click.pass_context
# def cli(ctx, identity, account, webproperty, profile, version):
#     ctx.obj = ga.authenticate(
#         identity=identity,
#         account=account,
#         webproperty=webproperty,
#         profile=profile,
#         interactive=True,
#         save=True)
# 
#     if version:
#         click.echo('googleanalytics {}'.format(ga.__version__))


import re
import click
import googleanalytics as ga
from prettytable import PrettyTable
from .common import cli



def table(rows, keys):
    t = PrettyTable(keys)
    t.align = 'l'
    for row in rows:
        t.add_row(ga.utils.pick(row, keys))
    return t


# TODO: after playing around with this, it seems like a recursive view
# would actually be more useful, so you can see accounts, webproperties
# and profiles all at the same time (perhaps configurable with --depth)
@cli.command()
@click.pass_obj
def properties(scope):
    if isinstance(scope, ga.account.WebProperty):
        click.echo(table(scope.profiles, ['name', 'id']))
    elif isinstance(scope, ga.account.Account):
        click.echo(table(scope.webproperties, ['name', 'url', 'id']))
    else:
        click.echo(table(scope, ['name', 'id']))


def matcher(pattern):
    def match(column):
        return re.search(pattern, column.name, re.IGNORECASE)
    return match


# Path: googleanalytics/commands/common.py
# @click.group(invoke_without_command=True)
# @click.option('--identity')
# @click.option('--account')
# @click.option('--webproperty')
# @click.option('--profile')
# @click.option('--version', is_flag=True)
# @click.pass_context
# def cli(ctx, identity, account, webproperty, profile, version):
#     ctx.obj = ga.authenticate(
#         identity=identity,
#         account=account,
#         webproperty=webproperty,
#         profile=profile,
#         interactive=True,
#         save=True)
# 
#     if version:
#         click.echo('googleanalytics {}'.format(ga.__version__))


# Path: googleanalytics/commands/list.py
import re
import click
import googleanalytics as ga
from prettytable import PrettyTable
from .common import cli




def table(rows, keys):
    t = PrettyTable(keys)
    t.align = 'l'
    for row in rows:
        t.add_row(ga.utils.pick(row, keys))
    return t


# TODO: after playing around with this, it seems like a recursive view
# would actually be more useful, so you can see accounts, webproperties
# and profiles all at the same time (perhaps configurable with --depth)
@cli.command()
@click.pass_obj
def properties(scope):
    if isinstance(scope, ga.account.WebProperty):
        click.echo(table(scope.profiles, ['name', 'id']))
    elif isinstance(scope, ga.account.Account):
        click.echo(table(scope.webproperties, ['name', 'url', 'id']))
    else:
        click.echo(table(scope, ['name', 'id']))


def matcher(pattern):
    def match(column):
        return re.search(pattern, column.name, re.IGNORECASE)
    return match


@cli.command()