__author__ = 'CHEN Zhao'
import json_encoder
import log
import sys
import os.path


APP_SECRET_KEY = 'gMissionForHKUSTSecretKey'
APP_AUTH_HEADER_PREFIX = 'gMission'


def stdout(*lst):
    print '[' + ' '.join(sys.argv) + ']' + ' '.join(map(str, lst))
    sys.stdout.flush()


def config(app, root):
    config_common(app, root)
    check_dir_config(app)


def makedir(dir_path):
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)


def check_dir_config(app):
    for key, value in app.config.items():
        if key.startswith('GMISSION') and key.endswith('DIR'):
            makedir(value)


def config_common(app, root_path):
    app.json_encoder = json_encoder.CustomJSONEncoder

    app.secret_key = 'blabla'
    app.config['SECRET_KEY'] = APP_SECRET_KEY
    app.config['APP_AUTH_HEADER_PREFIX'] = APP_AUTH_HEADER_PREFIX

    app.config['WTF_CSRF_ENABLED'] = False

    app.config['GMISSION_IMAGE_UPLOAD_DIR'] = os.path.join(root_path, 'static', 'image', 'original')
    app.config['GMISSION_IMAGE_THUMB_DIR'] = os.path.join(root_path, 'static', 'image', 'thumb')

    app.config['GMISSION_VIDEO_UPLOAD_DIR'] = os.path.join(root_path, 'static', 'video', 'original')
    app.config['GMISSION_VIDEO_THUMB_DIR'] = os.path.join(root_path, 'static', 'video', 'thumb')

    app.config['GMISSION_AUDIO_UPLOAD_DIR'] = os.path.join(root_path, 'static', 'audio', 'original')

    app.config['GMISSION_LOGS_DIR'] = os.path.join(root_path, '..', '..', 'logs')

    # app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://csp_team:csp2014hkust@127.0.0.1:3306/gmission_hkust'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://csp_team:csp2014hkust@docker-mysql/gmission_hkust'

    log.set_logger(app)
    app.config.from_object('email')
