# application initiation script
import os, sys, traceback

from glim.facades import Config, Database, Orm, Session, Cookie, IoC, View, Log
from glim.utils import import_module
from glim.dispatch import Glim

import glim.paths as paths

from werkzeug.serving import run_simple
from werkzeug.wsgi import SharedDataMiddleware
from termcolor import colored

class App:

    def __init__(self, commandadapter, env = 'development'):

        self.commandadapter = commandadapter

        self.mconfig = import_module('app.config.%s' % env)
        if self.mconfig is None:
            print colored('Configuration for %s not found' % env, 'red')
            exit()

        self.config = self.mconfig.config

        self.register_config()
        self.register_log()
        self.register_database()
        self.register_extensions()
        self.register_ioc()
        self.register_view()
        
        # find out start
        mstart = import_module('app.start')
        self.before = mstart.before

    def register_config(self):
        Config.register(self.config)

    def register_database(self):
        if 'db' in self.config.keys():
            if self.config['db']:
                Database.register(self.config['db'])
                if 'orm' in self.config.keys():
                    if self.config['orm']:
                        Orm.register(Database.engines)

    def register_extensions(self, extensions = []):
        try:
            for extension, config in self.config['extensions'].items():

                # extension module base string
                ext_bstr = 'ext.%s' % (extension)

                # start script
                ext_sstr = '%s.start' % ext_bstr

                ext_startmodule = import_module(ext_sstr, pass_errors = True)
                if ext_startmodule is not None:
                    before = getattr(ext_startmodule, 'before')
                    before(config)

                # register extension commands if exists
                ext_cmdstr = '%s.%s' % (ext_bstr, 'commands')

                ext_cmd_module = import_module(ext_cmdstr, pass_errors = True)
                if ext_cmd_module is not None:
                    self.commandadapter.register_extension(ext_cmd_module, extension)

        except Exception, e:
            print traceback.format_exc()
            Log.error(e)

    def register_ioc(self):
        IoC.register()

    def register_view(self):
        if 'views' in self.config:
            View.register(self.config['views'])

    def register_log(self):
        if 'log' in self.config:
            Log.register(self.config['log'])
        else:
            Log.register()

    def start(self, host = '127.0.0.1', port = '8080', env = 'development'):
        try:
            self.before()
            mroutes = import_module('app.routes')
            app = Glim(mroutes.urls, self.config['app'])

            if 'static' in self.config['app']:
                app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
                    self.config['app']['static']['url'] : self.config['app']['static']['path']
                })

            run_simple(host, int(port), app, use_debugger = self.config['app']['debugger'], use_reloader = self.config['app']['reloader'])

        except Exception, e:
            print traceback.format_exc()
            exit()