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

function CoffeeMachine:init(desc)
	self.durationToActivate = 15000
	CoffeeMachine.superInit(desc)

	self.filling = self:newChild({class="Sprite", x=20, y=123, image="ROOM2_COFFEECUP_FILLING:objects"})
	self.filling:setFrame(1)

	self.cup = self:newChild({class="Sprite", x=22, y=120, image="ROOM2_COFFEECUP:objects"})
	self.cup:setFrame(1)

	self.activateDuration = 0
end

function CoffeeMachine:hasWorkToDo()
	return true
end

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

function CoffeeMachine:canPlace(character)
	if (self.occupied) then
		return false
	elseif (character.hasDiseaseStepsLeft) then
		return character.disease.currentStep.allowDetour
	end
	return true
end

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

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

	level:bm_incSpecialStationCount()

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

	return true
end

function CoffeeMachine:onWorkStart(task)
	local r = CoffeeMachine.super.onWorkStart(self, task)

	task.actor:setState("working in station")
	task.actor:playAnimation(self.workAnimation, 1, "stay", self.workAnimationFrameTime)
	task.duration = self.workDuration

	self.cup:setVisible(false)
	self.cup:setFrame(0)
	self.filling:setFrame(0)

	return r
end

function CoffeeMachine:onWorkFinish(task)
	CoffeeMachine.super.onWorkFinish(self, task)
	task.actor:addHealth(1)
	task.actor:playDefaultAnimation()
	task.actor:setState("idle")
	self.clickable = false
end

function CoffeeMachine:onTick(time)
	CoffeeMachine.tick(self, time)
	if (self.activateDuration > 0) then
		self.activateDuration = self.activateDuration - time
		if (self.activateDuration <= 0) then
			self.clickable = true
			self.filling:setFrame(1)
			self.cup:setVisible(true)
			self.cup:animate({startFrame=0, endFrame=1, frameTime=1000})
			self:playSample("SND_COFFEE_POWER:sound")
		end
	end
end

function CoffeeMachine:onLeave(character)
	CoffeeMachine.super.onLeave(self, character)
	self.activateDuration = self.durationToActivate
end
