"""
    Main entry point
"""
import os

import logging

import ujson

from pyramid.config import Configurator
from pyramid.renderers import JSON as JSONRenderer

from pyramid_redis_sessions import session_factory_from_settings

from webgnome_api.common.views import cors_policy

logging.basicConfig()


class DummySession(object):
    session_id = 'DummySession'


def reconcile_directory_settings(settings):
    save_file_dir = settings['save_file_dir']

    for d in (save_file_dir,):
        if not os.path.exists(d):
            print 'Creating folder {0}'.format(d)
            os.mkdir(d)
        elif not os.path.isdir(d):
            raise EnvironmentError('Folder path {0} '
                                   'is not a directory!!'.format(d))

    locations_dir = settings['locations_dir']

    if not os.path.exists(locations_dir):
        raise EnvironmentError('Location files folder path {0} '
                               'does not exist!!'.format(locations_dir))
    if not os.path.isdir(locations_dir):
        raise EnvironmentError('Location files folder path {0} '
                               'is not a directory!!'.format(locations_dir))


def load_cors_origins(settings, key):
    if key in settings:
        origins = settings[key].split('\n')
        cors_policy['origins'] = origins


def get_json(request):
    return ujson.loads(request.text)


def main(global_config, **settings):
    settings['package_root'] = os.path.abspath(os.path.dirname(__file__))
    settings['objects'] = {}

    settings['uncertain_models'] = {}
    try:
        os.mkdir('ipc_files')
    except OSError, e:
        # it is ok if the folder already exists.
        if e.errno != 17:
            raise

    reconcile_directory_settings(settings)
    load_cors_origins(settings, 'cors_policy.origins')

    config = Configurator(settings=settings)

    # pyramid_redis_sessions will create a session object for every request,
    # even the CORS preflight requests, and if there is no session cookie,
    # a new session key will be created.  And the CORS preflight requests
    # will never have a session cookie.  So we overload the session factory
    # function here and add a special case for CORS preflight requests.
    session_factory = session_factory_from_settings(settings)

    def overloaded_session_factory(request, **kwargs):
        if request.method.lower() == 'options':
            # OPTIONS requests never have a session cookie, even when there
            # is a valid session
            return DummySession()
        else:
            return session_factory(request, **kwargs)

    config.set_session_factory(overloaded_session_factory)

    # we use ujson to load our JSON payloads
    config.add_request_method(get_json, 'json', reify=True)

    renderer = JSONRenderer(serializer=lambda v, **kw: ujson.dumps(v))
    config.add_renderer('json', renderer)

    config.add_tween('webgnome_api.tweens.PyGnomeSchemaTweenFactory')

    config.add_route('upload', '/upload')
    config.add_route('activate', '/activate')
    config.add_route('download', '/download')
    config.add_route('persist', '/persist')

    config.add_route('map_upload', '/map/upload')
    config.add_route('map_activate', '/map/activate')

    config.add_route('mover_upload', '/mover/upload')

    config.add_route('environment_upload', '/environment/upload')
    config.add_route('environment_activate', '/environment/activate')

    config.add_route('socket.io', '/socket.io/*remaining')
    config.add_route('logger', '/logger')
    config.add_route('export', '/export/*file_path')

    config.scan('webgnome_api.views')

    return config.make_wsgi_app()
