------------------------------------------------------------------------------
-- class 'Achievment'
-- Базовый клас ачивки, хранит функции обработчики для необходимых событий.
------------------------------------------------------------------------------

class 'Achievment'

------------------------------------------------------------------------------
function Achievment:__init(game_scene)
	self.game_scene = game_scene
	self.name = ''
	self.event_handlers = {}
	self.is_completed = false
	self.is_unlocked = false
end

------------------------------------------------------------------------------
function Achievment:addEventHandler(event_name, handler_func)
	self.event_handlers[event_name] = handler_func
end

------------------------------------------------------------------------------
function Achievment:removeEventHandler(event_name)
	self.event_handlers[event_name] = nil
end

------------------------------------------------------------------------------
function Achievment:handleEvent(event_name, args)
	if table.has_key(self.event_handlers, event_name) then
		self.event_handlers[event_name](args)
	end
end

------------------------------------------------------------------------------
function Achievment:setCompleted(is_completed)
	self.is_completed = is_completed
	if self.is_completed then
		AddUniqueGameState('CompletedAchievments', self.name, EGameStateTag.GENERAL)
	end
end

------------------------------------------------------------------------------
function Achievment:setUnlocked(is_unlocked)
	self.is_unlocked = is_unlocked
	if self.is_unlocked then
		AddUniqueGameState('UnlockedAchievments', self.name, EGameStateTag.GENERAL)
	end
end

------------------------------------------------------------------------------
function Achievment:isCompleted()
	return self.is_completed
end

------------------------------------------------------------------------------
function Achievment:isUnlocked()
	return self.is_unlocked
end

------------------------------------------------------------------------------
function Achievment:unlock(callback)
	self:setUnlocked(true)
	self:showUnlockedWindow(callback)
end

------------------------------------------------------------------------------
function Achievment:getName()
	return self.name
end

------------------------------------------------------------------------------
function Achievment:restoreStates()
	self.is_completed = HasGameState('CompletedAchievments', self.name, EGameStateTag.GENERAL)
	self.is_unlocked = HasGameState('UnlockedAchievments', self.name, EGameStateTag.GENERAL)
end

------------------------------------------------------------------------------
-- override in subclasses, must call callback if there is one ----------------
function Achievment:showUnlockedWindow(callback)
	if callback then
		callback()
	end
end