# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import os

import rhea

from hestia.auth import AuthenticationTypes
from hestia.user_path import polyaxon_user_path

TMP_AUTH_TOKEN_PATH = '/tmp/.polyaxon/.authtoken'
CLIENT_CONFIG_PATH = os.path.join(polyaxon_user_path(), '.polyaxonclient')

config = rhea.Rhea.read_configs([
    rhea.ConfigSpec(CLIENT_CONFIG_PATH, config_type='.json', check_if_exists=False),
    os.environ,
    rhea.ConfigSpec(TMP_AUTH_TOKEN_PATH, config_type='.json', check_if_exists=False)
])

IN_CLUSTER = config.get_boolean('POLYAXON_IN_CLUSTER',
                                is_optional=True,
                                default=False)
NO_OP = config.get_boolean('POLYAXON_NO_OP',
                           is_optional=True,
                           default=False)
API_HOST = config.get_string('POLYAXON_API_HOST',
                             is_optional=True)
HTTP_PORT = config.get_int('POLYAXON_HTTP_PORT',
                           is_optional=True)
WS_PORT = config.get_int('POLYAXON_WS_PORT',
                         is_optional=True)
USE_HTTPS = config.get_boolean('POLYAXON_USE_HTTPS',
                               is_optional=True,
                               default=False)
VERIFY_SSL = config.get_boolean('POLYAXON_VERIFY_SSL',
                                is_optional=True)
API_HTTP_HOST = config.get_string('POLYAXON_API_HTTP_HOST',
                                  is_optional=True)
API_WS_HOST = config.get_string('POLYAXON_API_WS_HOST',
                                is_optional=True)
SECRET_USER_TOKEN_KEY = 'POLYAXON_SECRET_USER_TOKEN'  # noqa
SECRET_USER_TOKEN = config.get_string(SECRET_USER_TOKEN_KEY,
                                      is_optional=True)
SECRET_EPHEMERAL_TOKEN_KEY = 'POLYAXON_SECRET_EPHEMERAL_TOKEN'  # noqa
SECRET_EPHEMERAL_TOKEN = config.get_string(SECRET_EPHEMERAL_TOKEN_KEY,
                                           is_optional=True)
INTERNAL_TOKEN_KEY = 'POLYAXON_SECRET_INTERNAL_TOKEN'  # noqa
SECRET_INTERNAL_TOKEN = config.get_string(INTERNAL_TOKEN_KEY,
                                          is_optional=True)
AUTHENTICATION_TYPE = config.get_string('POLYAXON_AUTHENTICATION_TYPE',
                                        is_optional=True,
                                        default=AuthenticationTypes.TOKEN)
API_VERSION = config.get_string('POLYAXON_API_VERSION',
                                is_optional=True,
                                default='v1')
HASH_LENGTH = config.get_int('POLYAXON_HASH_LENGTH',
                             is_optional=True,
                             default=12)
INTERNAL_HEADER = config.get_string('POLYAXON_INTERNAL_HEADER',
                                    is_optional=True)
INTERNAL_HEADER_SERVICE = config.get_string('POLYAXON_INTERNAL_HEADER_SERVICE',
                                            is_optional=True)
SCHEMA_RESPONSE = config.get_boolean('POLYAXON_SCHEMA_RESPONSE',
                                     is_optional=True,
                                     default=False)
INTERNAL_HEALTH_CHECK_URL = config.get_string('POLYAXON_INTERNAL_HEALTH_CHECK_URL',
                                              is_optional=True)

DEFAULT_HTTP_PORT = 80
DEFAULT_HTTPS_PORT = 443
MIN_TIMEOUT = config.get_int('POLYAXON_MIN_TIMEOUT',
                             is_optional=True,
                             default=1)
TIMEOUT = config.get_int('POLYAXON_TIMEOUT',
                         is_optional=True,
                         default=20)
REQUEST_TIMEOUT = config.get_int('POLYAXON_REQUEST_TIMEOUT',
                                 is_optional=True,
                                 default=25)
LONG_REQUEST_TIMEOUT = config.get_int('POLYAXON_LONG_REQUEST_TIMEOUT',
                                      is_optional=True,
                                      default=3600)
INTERVAL = config.get_int('POLYAXON_INTERVAL',
                          is_optional=True,
                          default=1)
HEALTH_CHECK_INTERVAL = config.get_int('HEALTH_CHECK_INTERVAL',
                                       is_optional=True,
                                       default=60)
QUEUE_CALL = config.get_int('POLYAXON_INTERVAL',
                            is_optional=True,
                            default=200)
LOGS_LEVEL = config.get_int('POLYAXON_LOGS_LEVEL',
                            is_optional=True)

@@ -325,16 +325,205 @@
 lient')%0A
+CONFIG_PATH = os.path.join(polyaxon_user_path(), '.polyaxonconfig')%0A%0Aglobal_config = rhea.Rhea.read_configs(%5B%0A    rhea.ConfigSpec(CONFIG_PATH, config_type='.json', check_if_exists=False)%0A%5D)
 %0Aconfig 
@@ -1124,393 +1124,1198 @@
 ue)%0A
-HTTP_PORT = config.get_int('POLYAXON_HTTP_PORT',%0A                           is_optional=True)%0AWS_PORT = config.get_int('POLYAXON_WS_PORT',%0A                         is_optional=True)%0AUSE_HTTPS = config.get_boolean('POLYAXON_USE_HTTPS',%0A                               is_optional=True,%0A                               default=False)%0AVERIFY_SSL = config.get_boolean('POLYAXON_VERIFY_SSL',%0A
+if not API_HOST:  # Check global config config%0A    API_HOST = global_config.get_string('host',%0A                                        is_optional=True)%0A%0AHTTP_PORT = config.get_int('POLYAXON_HTTP_PORT',%0A                           is_optional=True)%0Aif not HTTP_PORT:  # Check global config config%0A    HTTP_PORT = global_config.get_int('http_port',%0A                                      is_optional=True)%0AWS_PORT = config.get_int('POLYAXON_WS_PORT',%0A                         is_optional=True)%0Aif not WS_PORT:  # Check global config config%0A    WS_PORT = global_config.get_int('ws_port',%0A                                    is_optional=True)%0AUSE_HTTPS = config.get_boolean('POLYAXON_USE_HTTPS',%0A                               is_optional=True)%0Aif USE_HTTPS is None:  # Check global config config%0A    USE_HTTPS = global_config.get_boolean('use_https',%0A                                          is_optional=True,%0A                                          default=False)%0AVERIFY_SSL = config.get_boolean('POLYAXON_VERIFY_SSL',%0A                                is_optional=True)%0Aif VERIFY_SSL is None:  # Check global config config%0A    VERIFY_SSL = global_config.get_boolean('verify_ssl',%0A           
     

Update client to use cli config if found