-- ************************************ CareCharacter ************************************ --
CareCharacter = getClass("CareCharacter")

function CareCharacter:canPlayIdleAnimations()
	return
		self.playIdleAnimations and
		isTable(self.idleAnimations) and
		not taskSystem:hasAnimationFor(self)
end

function CareCharacter:playIdleAnimation()
	if (self:canPlayIdleAnimations()) then
		self.previousIdleAnimationIndex = self.previousIdleAnimationIndex or -1
		local paramIdx = self.previousIdleAnimationIndex
		local numAnims = #self.idleAnimations
		if (numAnims <= 2) then
			paramIdx = math.Random(numAnims)
		elseif (numAnims > 0) then
			while (paramIdx == self.previousIdleAnimationIndex) do
				paramIdx = math.Random(numAnims)
			end
		end
		self.previousIdleAnimationIndex = paramIdx
		local params = self.idleAnimations[paramIdx]
		if (isTable(params)) then
			local animationName = params[1]
			local frameTime = params[2]
			local cycles = params[3] or 1
			self:playAnimation(animationName, cycles, "default", frameTime)
		end
	end
end

function CareCharacter:startPickupDroolSequence()
	self:addTask({class="CallTask", func=function() self.patientPickup = self.patientPickup + 1 end})
end

function CareCharacter:addPickupDroolSequence(patient)
	self:addTask({class="WalkToTask", target=patient})
	self:addTask({class="PlayAnimationTask", animation="COME_PATIENT", cycles=1, whenFinished="default", frameTime=250, wait=true});
	self:addTask({class="DroolTask", patient=patient})
	self:addTask({class="WalkToTask", target=level.currentRoom.exit});
	self:addTask({class="Task", duration=600})
end

function CareCharacter:endPickupDroolSequence()
	self:addTask({class="CallTask", func=function()
		self.patientPickup = self.patientPickup - 1
		if (not level.hasDroolPatients and not level.hasShiftFinished) then
			self:addTask({class="WalkToTask", target=level.currentRoom.residentSpot});
			self:addTask({class="CallTask", func=function()
				if (self.x > 512) then
					self.direction = "left"
				end
			end})
		end
	end})
end

function CareCharacter:getRandomPositiveSoundId()
	return self.positiveSoundIds[math.Random(table.count(self.positiveSoundIds))]
end

-- ************************************ Patient ************************************ --
Patient = getClass("Patient")

function Patient:setWalkPatient()
	level:cancelSelectedObject()

	level.selectedObject = self
	self.clickable = false
	self.selected = true
end

function Patient:onStateEnter(state, prevState)
	Patient.super.onStateEnter(self, state, prevState)
	if (state == "leaving") then
		self.clickable = false
		if (level.selectedObject == self) then
			level:resetSelectedObject()
		end
	end
end

-- ************************************ Janitor ************************************ --
Janitor = deriveLuaClass("Janitor", "CareCharacter")

function Janitor:onTick(time)
--print(self.currentAnimation)
	self:tick(time)

	if (self.currentAnimation == "IDLE_CLEANING") then
		if (not self.playingCleaningLoop) then
			self.playingCleaningLoop = true
			self:loopSample("SND_JANITOR_CLEANING:sound", "janitor")
		end
	else
		if (self.playingCleaningLoop) then
			self.playingCleaningLoop = false
			self:stopLoopSample("SND_JANITOR_CLEANING:sound", "janitor")
		end
	end
end
