import ast
import os
from flask import json


class Config(object):

    DEBUG = True
    REGIONS = json.loads(os.getenv('REGIONS', '{}'))
    BATCH_NODE = ast.literal_eval(os.getenv('BATCH_NODE', 'False'))

    SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI')
    SQLALCHEMY_TRACK_MODIFICATIONS = ast.literal_eval(os.getenv('SQLALCHEMY_TRACK_MODIFICATIONS', 'False'))

    if os.environ.get('OAUTHLIB_INSECURE_TRANSPORT') is None:
        os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

    SENTRY_DSN = os.getenv('SENTRY_DSN')
    SECRET_KEY = os.getenv('SESSION_SECRET_KEY', '1')
    OAUTH_CLIENT_ID = os.getenv('OAUTH_CLIENT_ID')
    OAUTH_CLIENT_SECRET = os.getenv('OAUTH_CLIENT_SECRET')
    OAUTH_AUTH_URL = os.getenv('OAUTH_AUTH_URL')
    OAUTH_TOKEN_URL = os.getenv('OAUTH_TOKEN_URL')
    OAUTH_USER_URL = os.getenv('OAUTH_USER_URL')
    OAUTH_LOGOUT_URL = os.getenv('OAUTH_LOGOUT_URL')
    OAUTH_REDIRECT_URL = os.getenv('OAUTH_REDIRECT_URL')
    BACKSTAGE_BAR_ADDRESS = os.getenv('BACKSTAGE_BAR_ADDRESS')

    ELASTICSEARCH_URL = os.getenv('ELASTICSEARCH_URL')
    ELASTICSEARCH_PORT = os.getenv('ELASTICSEARCH_PORT')
    ELASTICSEARCH_CLIENT = os.getenv('ELASTICSEARCH_CLIENT')
    ELASTICSEARCH_INDEX = os.getenv('ELASTICSEARCH_INDEX')
    ELASTICSEARCH_TYPE = os.getenv('ELASTICSEARCH_TYPE')
    LOGSTASH_HOST = os.getenv('LOGSTASH_HOST')
    LOGSTASH_PORT = os.getenv('LOGSTASH_PORT')

    USAGE_API_BATCH_SIZE = os.getenv('USAGE_API_BATCH_SIZE', '100')
    USAGE_REGIONS = os.getenv('USAGE_REGIONS', '').split(',')
    USAGE_CACHE_TIME = int(os.getenv('USAGE_CACHE_TIME', 60*60*24))  # 1 day cache
    USAGE_MINIMUM_TIME = os.getenv('USAGE_MINIMUN_TIME', 0.5)  # 1 hour

    USAGE_DEFAULT_OFFERING_CPU = int(os.getenv('USAGE_DEFAULT_OFFERING_CPU', 12))
    USAGE_DEFAULT_OFFERING_MEMORY = int(os.getenv('USAGE_DEFAULT_OFFERING_MEMORY', 8192))

    USAGE_DEFAULT_PROJECT_ID = os.getenv('USAGE_DEFAULT_PROJECT_ID', 'default')
    USAGE_DEFAULT_PROJECT_NAME = os.getenv('USAGE_DEFAULT_PROJECT_NAME','Default ACS project')
    USAGE_DEFAULT_PROCESS_ID = os.getenv('USAGE_DEFAULT_PROCESS_ID', '7a9456320f328700fd7f91dbe1050e27')
    USAGE_DEFAULT_PROCESS_ID_DEV = os.getenv('USAGE_DEFAULT_PROCESS_ID_DEV', 'f29456320f328700fd7f91dbe1050e28')
    USAGE_DEFAULT_BUSINESS_SERVICE_ID = os.getenv('USAGE_DEFAULT_BUSINESS_SERVICE_ID', 'f8246ff9db9f43409fc15458dc9619a0')
    USAGE_NOT_CLASSIFIED_BUSINESS_SERVICE_ID = os.getenv('USAGE_NOT_CLASSIFIED_BUSINESS_SERVICE_ID', '3dcf5f75db9f43409fc15458dc961992')
    USAGE_DEFAULT_CLIENT_ID = os.getenv('USAGE_DEFAULT_CLIENT_ID', 'f57e85f9db6fcf409fc15458dc961936')
    USAGE_DEFAULT_COMPONENT_ID = os.getenv('USAGE_DEFAULT_COMPONENT_ID', 'ca72e379db9f43409fc15458dc961960')
    USAGE_DEFAULT_SUB_COMPONENT_ID = os.getenv('USAGE_DEFAULT_SUB_COMPONENT_ID', 'ca72e379db9f43409fc15458dc961960')
    BILLING_COLLECTOR_ENDPOINT = os.getenv('BILLING_COLLECTOR_ENDPOINT')

    EVENT_QUEUE_EXCHANGE = os.getenv('EVENT_QUEUE_EXCHANGE', 'cloudstack-events')
    EVENT_QUEUE_NAME = os.getenv('EVENT_QUEUE_NAME', 'iaas-usage-events')
    EVENT_ROUTING_KEY_TEMPLATE = os.getenv('EVENT_ROUTING_KEY_TEMPLATE', 'management-server.ActionEvent.%s.*.*')
    EVENT_LIST = [
        'LB-CREATE',
        'LB-DELETE',
        'LB-ASSIGN-TO-RULE',
        'LB-REMOVE-FROM-RULE',
        'LB-HEALTHCHECKPOLICY-CREATE',
        'LB-HEALTHCHECKPOLICY-UPDATE',
        'LB-STICKINESSPOLICY-CREATE',
        'NETWORK-DELETE',
        'NETWORK-CREATE',
        'NETWORK-RESTART',
        'PROJECT-ACCOUNT-ADD',
        'PROJECT-CREATE',
        'PROJECT-DELETE',
        'SERVICE-OFFERING-CREATE',
        'SERVICE-OFFERING-DELETE',
        'VMSNAPSHOT-CREATE',
        'VMSNAPSHOT-DELETE',
        'VMSNAPSHOT-REVERT',
        'TEMPLATE-COPY',
        'TEMPLATE-CREATE',
        'TEMPLATE-DELETE',
        'TEMPLATE-EXTRACT',
        'USER-LOGIN',
        'NIC-CREATE',
        'NIC-DELETE',
        'NIC-UPDATE',
        'AUTOSCALE-SCALEUP',
        'AUTOSCALE-SCALEUP-FAILED',
        'AUTOSCALE-SCALEDOWN',
        'AUTOSCALE-SCALEDOWN-FAILED',
        'AUTOSCALEVMGROUP-CREATE',
        'AUTOSCALEVMGROUP-UPDATE',
        'AUTOSCALEVMGROUP-DELETE',
        'AUTOSCALEVMGROUP-DISABLE',
        'AUTOSCALEVMGROUP-ENABLE',
        'VM-CREATE',
        'VM-DESTROY',
        'VM-MIGRATE',
        'VM-REBOOT',
        'VM-RESTORE',
        'VM-START',
        'VM-STOP',
        'VM-EXPUNGE',
        'VM-UPGRADE',
        'ROUTER-REBOOT',
        'ROUTER-RESTART',
        'ROUTER-STOP',
        'ROUTER-DESTROY',
        'ROUTER-START',
        'ROUTER-MIGRATE',
        'VOLUME-ATTACH',
        'VOLUME-CREATE',
        'VOLUME-DELETE',
        'VOLUME-DETACH'
    ]


class ProdConfig(Config):

    SQLALCHEMY_POOL_SIZE = 20
    SQLALCHEMY_POOL_RECYCLE = 20


class DevConfig(Config):

    SQLALCHEMY_DATABASE_URI = 'mysql://root:@0.0.0.0/iaas_usage'
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    EVENT_QUEUE_USER = 'guest'
    EVENT_QUEUE_PASSWORD = 'guest'
    REGIONS = {'lab': 'RJLAB', 'cta': 'RJCTA', 'cme': 'RJCME', 'dev': 'RJDEV'}


class TestConfig(Config):

    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite://'
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    REGIONS = {'cme': 'RJCME', 'lab': 'RJLAB', 'cta': 'RJCTA', 'dev': 'RJDEV'}