bulma = require "./bulma.ls"
h = require 'maquette' .h

AuthWS = (socket-url) ->
	self = {}

	request-types = {
		"get-token":               0
		"add-user":                1
		"get-user":                2
		"get-user-by-credentials": 3
		"mod-user":                4
		"register":                5
		"update-password":         6
		"list-users":              7
		"check-permission":        8
		"set-permission":          9
	}

	response-types = {
		"error":                   0
		"token":                   1
		"user":                    2
		"user-added":              3
		"user-edited":             4
		"users-list":              5
		"permission-check":        6
		"permission-set":          7
	}

	# TODO: naming convention
	# users can record functions to run on events
	self.user-on-socket-error = []
	self.user-on-socket-close = []

	self.callbacks = {}
	for key, value of response-types
		self.callbacks[value] = []

	self.add-event-listener = (type, callback) ->
		type = response-types[type]

		self.callbacks[type] ++= [callback]

	self.open-socket = ->
		self.socket := new WebSocket socket-url

		self.socket.onerror = (event) ->
			for f in self.user-on-socket-error
				f event
			self.socket.close!

		self.socket.onclose = (event) ->
			for f in self.user-on-socket-close
				f event

		self.socket.onmessage = (event) ->
			message = JSON.parse(event.data)

			for f in self.callbacks[message.mtype]
				f JSON.parse(message.payload)

	self.reopen = ->
		self.socket.close!
		self.open-socket!

	self.open-socket!

	self.send = (type, opts) ->
		self.socket.send JSON.stringify { mtype: type, payload: opts }

	self.get-token = (login, password) ->
		self.send request-types[\get-token], JSON.stringify {
			login: login
			password: password
		}

	self.get-user-by-credentials = (login, password) ->
		self.send request-types[\get-user-by-credentials], JSON.stringify {
			login: login
			password: password
		}

	self.login = (login, password) ->
		self.get-token login, password
		self.get-user-by-credentials login, password


	self.get-user = (login_or_uid) ->
		self.send request-types[\get-user], JSON.stringify {
			user: login_or_uid
		}

	self.register = (login, password) ->
		self.send request-types[\register], JSON.stringify {
			login: login
			password: password
		}

	self.get-extra = (token, name) ->
		console.warn "get-extra: deprecated"

	self.set-extra = (token, name, extra) ->
		console.warn "set-extra: deprecated"

	self.update-password = (login, old-password, new-password) ->
		self.send request-types[\update-password], JSON.stringify {
			login: login
			old_password: old-password
			new_password: new-password
		}

	self.list-users = (token) ->
		console.warn "list-users: unstable API"

		self.send request-types[\list-users], JSON.stringify {
			token: token
		}

	# TODO: authd overhaul required
	#self.add-user = (login, password) ->
	#	self.send request-types[\add-user], JSON.stringify {
	#		login: login
	#		password: password
	#	}

	# TODO: authd overhaul required
	#self.mod-user = (uid) ->
	#	self.send request-types[\mod-user], JSON.stringify {
	#		uid: uid
	#	}

	self

module.exports = AuthWS

