# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
import click

from anaconda_enterprise.cli.api import create_client


@click.command(name='login')
@click.option('--username', prompt=True)
@click.password_option(confirmation_prompt=False)
@click.pass_obj  # object is a ConfigurationManager loaded with the callback on main
def main(config_manager, username, password):
    """Log in to the platform"""
    client = create_client(config_manager)
    client.login(username, password)
    click.echo('Logged %s in!' % (username, ))


@click.command(name='logout')
@click.pass_obj  # object is a ConfigurationManager loaded with the callback on main
def logout(config_manager):
    """Log out from the platform"""
    client = create_client(config_manager)
    client.token_store.delete_binstar_tokens()
    if config_manager.delete_token():
        click.echo('User logged out successfully.')
    else:
        click.echo("User already logged out: no active session found.")
