-- ************************************ ChangeStation ************************************ --
ChangeStation = deriveLuaClass("ChangeStation", "WorkStation")

function ChangeStation:addToStationTask()
	if (ChangeStation.super.addToStationTask(self)) then

		self.startChangingState = "no clothes"
		if (level:getObject("bathrobe").displayStock > 0) then
			self.startChangingState = "start"
			--print("1 decStock")
			level:getObject("bathrobe"):decStock()
		end

		self.clickable = false
		return true
	end
	return false
end

function ChangeStation:canWalkToStation(character)
	return ChangeStation.super.canWalkToStation(self, character)
end

function ChangeStation:onPlaceCharacter(character)
	ChangeStation.super.onPlaceCharacter(self, character)
	character:playDefaultAnimation()

	if (level:getObject("bathrobe").displayStock == 0) then
		level:getObject("bathrobe"):createOverhead("ICON_NO_STOCK:icons")
	end

	if (self.startChangingState == "start") then
		self:playAnimation("FILLED")
		--print("1 decDisplayStock")
		--level:getObject("bathrobe"):decDisplayStock()
	end
end

function ChangeStation:onLeaveCharacter(character)
	ChangeStation.super.onLeaveCharacter(self, character)
	self:playAnimation(self.defaultAnimation)
	self.clickable = true
end

function ChangeStation:onStockChanged()
	self.startChangingState = self.startChangingState or ""
	if (self.startChangingState == "no clothes" and level:getObject("bathrobe").stock > 0) then
		self.startChangingState = "start"
		self:playAnimation("FILLED")
		--print("2 decStock")
		level:getObject("bathrobe"):decStock()
		--print("2 decDisplayStock")
		--level:getObject("bathrobe"):decDisplayStock()
	end

	if (level:getObject("bathrobe").stock > 0) then
		level:getObject("bathrobe"):removeOverhead()
	end
end

function ChangeStation:onWorkStart(task)
	return ChangeStation.super.onWorkStart(self, task)
end

function ChangeStation:onWorkUpdate(task, time)
	if (self.startChangingState == "changing") then
		return ChangeStation.default_onWorkUpdate(self, task, time)
	elseif (self.startChangingState == "start") then
		--print("3 decDisplayStock")
		level:getObject("bathrobe"):decDisplayStock()
		self.startChangingState = "changing"
		return ChangeStation.default_onWorkUpdate(self, task, time)
	end
	return false
end

function ChangeStation:onWorkFinish(task)
	ChangeStation.super.onWorkFinish(self, task)
	self:playDefaultAnimation()
	task.actor.inHospitalClothes = true
	task.actor:addTask({class="WalkToTask", target=self.exitPos})
end
