path = require('path')
fs = require('fs')
config = require('./config')
# loader = require('./loader')
ServiceRpc = require('./rpc').ServiceRpc
sync = require('./sync').sync

class App
	constructor: ()->
		@services = {}		

	# opts: {
	#	servicePath: 'location of services' - default is './services'
	# }
	init: (@server, opts={})->
		@config = config.parseConfig()
		@initData()	
		
	rpc: (endpoint)->
		@rpc = new ServiceRpc(@, @server, endpoint)

	requireDir: (dir)->
		requirePath = (file)->
			path.join(
				path.resolve(dir), 
				file.replace('.coffee', '')
			)
		require(requirePath(file)) for file in fs.readdirSync(dir)

	service: (cls)=>
		@services[cls.name] = new cls(@)

	page: (endpoint, page)->
		@server

	initData: ()->
		sw = require('./sync-wrappers')
		if @config.mongo
			@mongo = new sw.MongoDb(@config.mongo)
			sync ()=> @mongo.open()
		@redis = new sw.Redis(@config.redis) if @config.redis
		# @dynamo = new DynamoDb(@config.dynamo) if @config.dynamo
		# @sdb = new SimpleDb(@config.sdb) if @config.sdb

exports.App = App
