local require = require
local setmetatable, getmetatable, assert, type, error = setmetatable, getmetatable, assert, type, error
local json = require "json"
local EventEmitter = require "luanode.event_emitter"
local Class = require "luanode.class"
local process = process

module((...))

local core = require "LnConfigurationControl.core"

Class.MakeInheritFrom(core.Client, EventEmitter)

--
-- Construct the luaCppBridge object, and the build the EventEmitter using that
function core.Client:__init (...)
	return Class.construct(core.Client, core.Client:new(...))
end

local mt = getmetatable(_M)
if not mt then
	mt = {}
	setmetatable(_M, mt)
end

--
-- Make it possible to call LnConfigurationControl(bla, bla, bla)
mt.__call = function(foo, args)
	assert(type(args) == "table", "You must supply a table with the desired configuration")

	local client
	
	if type(args.input) == "string" and type(args.output) == "string" then
		client = core.Client("file", args.input, args.output)
	elseif type(args.ip) == "string" and type(args.port) == "number" then
		client = core.Client("http", args.ip, args.port)
	else
		error("incorrect parameters")
	end
	
	process:on("exit", function()
		client:TriggerNodeDown()
	end)
	
	return client
end
