-- ************************************ Station ************************************ --
WaterCooler = deriveLuaClass("WaterCooler", "Station")

function WaterCooler:init(desc)
	WaterCooler.superInit(desc)
	self:onDisplayStockChange()
end

function WaterCooler:hasWorkToDo()
	return true
end

function WaterCooler:addToStationTask()
	if (level.selectedObject) then
		if (self:canPlace(level.selectedObject)) then
			level:bm_incSpecialStationCount()
			level.selectedObject:cancelTasks()
			self:addSelectedObjectToStationTasks()
			self.clickable = false
			return true
		else
			level:cancelSelectedObject()
			return false
		end
	end
	return false
end

function WaterCooler:canPlace(character)
	if (self.occupied or self.stock <= 0) then
		return false
	elseif (character.hasDiseaseStepsLeft) then
		return character.disease.currentStep.allowDetour
	end
	return true
end

function WaterCooler:onObjectDrop(character)
	WaterCooler.super.onObjectDrop(self, character)

	self:place(character);
	character:setState("waiting in station")

	character:playDefaultAnimation()
	character:addTask({class="WorkTask", target=self})

	return true
end

function WaterCooler:onWorkStart(task)
	local r = WaterCooler.super.onWorkStart(self, task)
	task.actor:setState("working in station")
	task.actor:playAnimation(self.workAnimation, 1, "stay", self.workAnimationFrameTime)
	task.duration = self.workDuration
	self:decStock()
	self:decDisplayStock()
	return r
end

function WaterCooler:onWorkFinish(task)
	WaterCooler.super.onWorkFinish(self, task)
	task.actor:addHealth(1)
	task.actor:playDefaultAnimation()
	task.actor.clickable = (self.stock > 0)
	task.actor:setState("idle")
end

function WaterCooler:onLeave(character)
	WaterCooler.super.onLeave(self, character)
	self.clickable = (self.stock > 0)
end

function WaterCooler:onDisplayStockChange()
	self:setFrame(self.stock)
	self.clickable = (self.stock > 0)
end
