########################################################################
#	Hello Worlds - Libre 3D RPG game.
#	Copyright (C) 2020  CYBERDEViL
#
#	This file is part of Hello Worlds.
#
#	Hello Worlds is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	Hello Worlds is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################


class Signal:
	def __init__(self, *argTypes):
		self._argTypes = argTypes
		self._cbFuncs = [] # callbacks

	def connect(self, cbFunc):
		if cbFunc not in self._cbFuncs:
			self._cbFuncs.append(cbFunc)
			return 0
		else:
			print("Signal.connect : Callback function {0} connection already exists.".format(cbFunc))
			return 1

	def disconnect(self, cbFunc=None):
		if cbFunc == None:
			self._cbFuncs.clear()
			return 0
		elif cbFunc in self._cbFuncs:
			self._cbFuncs.remove(cbFunc)
			return 0
		else:
			print("Signal.disconnect : Callback function {0} not found.".format(cbFunc))
			return 1

	def emit(self, *args):
		_args = []
		index = 0
		for argType in self._argTypes:
			if type(args[index]) != argType:
				print("Signal.emit : Expected a {0} type instead got a {1} type for argument {2}".format(argType, type(args[index]), index))
				return 1
			_args.append(args[index])
			index += 1

		for cbFunc in self._cbFuncs.copy():
			cbFunc(*_args)
		return 0
