# Path: lain_cli/auth.py
# def sso_login(phase, cid, secret, redirect_uri, username, password):
#     sso_access = SSOAccess.new(phase, cid, secret, redirect_uri)
#     get_code_success, code = sso_access.get_auth_code(username, password)
#     if not get_code_success:
#         warn('get_auth_code failed, username or password may be wrong.')
#         return False
#     get_token_success, access_token, refresh_token = sso_access.get_auth_token(code)
#     if not get_token_success:
#         warn('get_auth_token failed, sso client secret may be wrong.')
#         return False
#     save_token_success = sso_access.save_token(phase, access_token, refresh_token)
#     if not save_token_success:
#         warn('save_token failed')
#         return False
#     return True
# 
# def docker_login(phase, username, password):
#     domain = get_domain(phase)
#     return docker.login(username, password, 'registry.%s' % domain)
# 
# def sso_refresh(phase):
#     sso_access = SSOAccess.new(phase, None, None, None)
#     refresh_token = sso_access.get_refresh_token(phase)
#     if not refresh_token:
#         warn('refresh failed, no refresh token got')
#         return False
#     refresh_success, new_access_token, new_refresh_token = sso_access.refresh_auth_token(refresh_token)
#     if not refresh_success:
#         warn('refresh failed, refresh_auth_token failed ')
#         return False
#     save_token_success = sso_access.save_token(phase, new_access_token, new_refresh_token)
#     if not save_token_success:
#         warn('refresh failed, save_token failed')
#         return False
#     return True
# 
# Path: lain_cli/utils.py
# def check_phase(phase):
#     if phase not in PHASE_CHOICES:
#         error('phase %s not in available phases: %s.' % (phase, PHASE_CHOICES))
#         warn(
#             'phase can be added using: lain config save %s domain {{ domain }}' % phase)
#         exit(1)


import getpass
from argh.decorators import arg
from lain_sdk.util import warn, info, error
from lain_cli.auth import sso_login, docker_login, sso_refresh
from lain_cli.utils import check_phase
# -*- coding: utf-8 -*-



@arg('phase', help="lain cluster phase id, can be added by lain config save")
def refresh(phase):
    """
    Refresh sso token
    """

    check_phase(phase)
    refresh_success = sso_refresh(phase)
    if refresh_success:
        info("Refresh successfully!")
    else:
        warn('Refresh failed, Please try again!')


@arg('phase', help="lain cluster phase id, can be added by lain config save")
def login(phase, cid=None, secret=None, redirect_uri=None):
    """
    Login specific phase, need open auth first

    cid: Client id get from the sso system, default: 3
    secret: Client secret get from the sso system, default: lain-cli_admin
    redirect_uri: Redirect uri get from the sso system, default: https://example.com/
    """


# Path: lain_cli/auth.py
# def sso_login(phase, cid, secret, redirect_uri, username, password):
#     sso_access = SSOAccess.new(phase, cid, secret, redirect_uri)
#     get_code_success, code = sso_access.get_auth_code(username, password)
#     if not get_code_success:
#         warn('get_auth_code failed, username or password may be wrong.')
#         return False
#     get_token_success, access_token, refresh_token = sso_access.get_auth_token(code)
#     if not get_token_success:
#         warn('get_auth_token failed, sso client secret may be wrong.')
#         return False
#     save_token_success = sso_access.save_token(phase, access_token, refresh_token)
#     if not save_token_success:
#         warn('save_token failed')
#         return False
#     return True
# 
# def docker_login(phase, username, password):
#     domain = get_domain(phase)
#     return docker.login(username, password, 'registry.%s' % domain)
# 
# def sso_refresh(phase):
#     sso_access = SSOAccess.new(phase, None, None, None)
#     refresh_token = sso_access.get_refresh_token(phase)
#     if not refresh_token:
#         warn('refresh failed, no refresh token got')
#         return False
#     refresh_success, new_access_token, new_refresh_token = sso_access.refresh_auth_token(refresh_token)
#     if not refresh_success:
#         warn('refresh failed, refresh_auth_token failed ')
#         return False
#     save_token_success = sso_access.save_token(phase, new_access_token, new_refresh_token)
#     if not save_token_success:
#         warn('refresh failed, save_token failed')
#         return False
#     return True
# 
# Path: lain_cli/utils.py
# def check_phase(phase):
#     if phase not in PHASE_CHOICES:
#         error('phase %s not in available phases: %s.' % (phase, PHASE_CHOICES))
#         warn(
#             'phase can be added using: lain config save %s domain {{ domain }}' % phase)
#         exit(1)


# Path: lain_cli/login.py
import getpass
from argh.decorators import arg
from lain_sdk.util import warn, info, error
from lain_cli.auth import sso_login, docker_login, sso_refresh
from lain_cli.utils import check_phase

# -*- coding: utf-8 -*-



@arg('phase', help="lain cluster phase id, can be added by lain config save")
def refresh(phase):
    """
    Refresh sso token
    """

    check_phase(phase)
    refresh_success = sso_refresh(phase)
    if refresh_success:
        info("Refresh successfully!")
    else:
        warn('Refresh failed, Please try again!')


@arg('phase', help="lain cluster phase id, can be added by lain config save")
def login(phase, cid=None, secret=None, redirect_uri=None):
    """
    Login specific phase, need open auth first

    cid: Client id get from the sso system, default: 3
    secret: Client secret get from the sso system, default: lain-cli_admin
    redirect_uri: Redirect uri get from the sso system, default: https://example.com/
    """


    check_phase(phase)