-- ************************************ Special Gesture event Room1 ************************************ --
SpecialRoom1 = deriveLuaClass("SpecialRoom1", "GestureEvent")

SpecialRoom1.missedGestures = 0
SpecialRoom1.triggerCount = 0
SpecialRoom1.triggerMoments = {}
SpecialRoom1.srTimer = 0
SpecialRoom1.scoreOneDone=20
SpecialRoom1.scoreAllDone=200
SpecialRoom1.won=false
SpecialRoom1.lost = false


function SpecialRoom1:eventWon()
	self.won = not self.lost
	self.completed = true
	self.fillSprite.visible = false
	eventBar:deactivate()
	level:getCharacter("staph"):playAnimation({animation="LAY", cycles=0})
end

function SpecialRoom1:createPostShiftAnimation()
	local tmp = {}
	
	self.doGestureObject.removeOverhead(self.doGestureObject)
	if (self.won == true) then
		if (isTable(taskSystem:getAnimationDesc("eventWon"))) then
			tmp = taskSystem:createAnimation("eventWon")
		end		
		self.completed = true
		if (isTable(self.afterGesturesObject)) then
			--table present, set to occupied while patient is there
			if (self.afterGesturesObject.setMaxOccupants) then
				self.afterGesturesObject:setMaxOccupants(1)
			end
			self.afterGesturesObject.visible = true
			self.doGestureObject.visible = false
		end
		self.doGestureObject.clickable = false
	else
		if (isTable(taskSystem:getAnimationDesc("eventLost"))) then
			tmp = self:playIfNotPlayedBefore("eventLost")
		end
	end
	return tmp
end

function SpecialRoom1:init(desc)
	SpecialRoom1.superInit(self, desc)
	for _, v in pairs(desc.gestures) do
		self.triggerMoments[#self.triggerMoments + 1] = v.timeBeforeStart or 0
	end
	self.triggerMoments[#self.triggerMoments + 1] = 5000
	self.srTimer = 0--desc.gestureDuration
	GestureEvent.eventWon = self.eventWon
end


function SpecialRoom1:onInitialized()
	SpecialRoom1.super.onInitialized(self)
end

function SpecialRoom1:initGesture()
	local gesture = self:getCurrentGesture()

	gesture.object = nil
	self.fillSprite.scale = self.startScale - 0.1
	if (gesture.objectId) then
		gesture.object = level:getSpriteExt(gesture.objectId)
		assert(gesture.object)
		if (self.moving) then
			gesture.object.playIdleAnimations = false
		end
	end
	self.fillSprite.visible = gesture.gestureType ~= "line"
	if (not self.moving) then
		self.dot.frame = 0
	end
end

function SpecialRoom1:onStart()
	eventBar.showValue = false
	eventBar.showTarget = false

	SpecialRoom1.super.onStart(self)

	hintManager:removeArrows({id="GestureEvent", animated=false})
end

function SpecialRoom1:showGesture(visible)
	if (not self.completed) then
		self:getCurrentGesture().visible = visible
		self.dot.visible = visible
		self.fillSprite.visible = visible

		if (visible) then
			self:initGesture()
			self:resetDot()

			if (self.moving and self:getCurrentGesture().object) then
				self:getCurrentGesture().object:playAnimation({animation=self:getCurrentGesture().animation, cycles=0})
			end
		else
			level:getCharacter("staph"):playAnimation({animation="LAY", cycles=0})
			self.srTimer = 0
			hero:playDefaultAnimation()
		end
	end
	hintManager:removeArrows({id="SpecialRoom1"})
end

function SpecialRoom1:checkWinConditions()
	return false
end

function SpecialRoom1:onTick(time)
	if (not self.completed) then
		SpecialRoom1.super.onTick(self, time)
		if (self.srTimer > 0 and not self.doGestureObject.isPresent) then
			self.srTimer = self.srTimer - time
			if (self.srTimer <= 0) then
				self.timer = 1
				self.triggerCount = self.triggerCount + 1
				self:nextGesture()
				self.doGestureObject:removeOverhead()
				level:getCharacter("staph"):playAnimation({animation="LAY", cycles=0})
				self.lost = true
			end
		end

		if (self.triggerCount > #self.triggerMoments) then
			self.doGestureObject.removeOverhead(self.doGestureObject)
			self:eventDone()
		end
	end
end

function SpecialRoom1:activateGesture()
	level:getCharacter("staph"):playAnimation({animation="LAY_SHOCKING"})
	self.srTimer = self.gestureDuration
	if (level.activeGesture) then
		if (not level:activeGesture()) then
			SpecialRoom1.super.activateGesture(self)
		end
	end
end

function SpecialRoom1:eventDone()
	if (not self.completed) then
		self.completed = true
	end
end