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

function DrawingTable:init(desc)
	self.heroDuration = 1000
	self.heroAnimation = desc.heroAnimation or "RESTOCK_PENCILS"
	self.patientTask = nil

	self.patientProgress = 0
	self.heroProgress = 0

	self.workDuration = 1000

	DrawingTable.superInit(desc)

	self.pencil = self:newChild({class="Sprite", x=148, y=33, image="ROOM3_PENCIL_STOCK:objects"})

	local frame = self.stock - 1
	if (frame < 0) then
		self.pencil:setVisible(false)
	else
		self.pencil:setFrame(frame)
	end

	self.heroWorkPos = self.workPos
	self.patientWorkPos = desc.patientWorkPos
end

function DrawingTable:hasWorkToDo()
	return true
end

function DrawingTable:onLeave(character)
	DrawingTable.super.onLeave(self, character)
	self.clickable = true
	self.patientActive = false

	local frame =  - 1
	if (self.stock <= 0) then
--		self:createOverhead("ICON_NO_STOCK:icons")
        self.clickable = false
	end
end


function DrawingTable:onDisplayStockChange()
	local frame = self.stock - 1
	if (frame < 0) then
		self.pencil:setVisible(false)
	else
		self.pencil:setFrame(frame)
		self:removeOverhead()
	end
end

function DrawingTable:addToStationTask()

	if (level.selectedObject) then
		if (self:canPlace(level.selectedObject)) then
			level.selectedObject:cancelTasks()
			self:addSelectedObjectToStationTasks()
			self.clickable = false
		self.patientActive = true
			return true
		else
			level:cancelSelectedObject()
			return false
		end
	else
	--[[
		hero:addTask({class="SetTask", positionLock=self})
		self.workPos = self.heroWorkPos
		if (self:hasWorkToDo()) then
			hero:addTask({class="WorkTask", positionLock=self, target=self})
			self:stopFaultySelectionBlink()
		else
			hero:addTask({class="WalkToTask", target=self})
		end
		self.heroActive = true
		return true
		--]]
	end
	return false
end

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

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

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

	level:bm_incSpecialStationCount()

	self.workPos = self.patientWorkPos
	character:addTask({class="WorkTask", target=self})

	return true
end

function DrawingTable:onWorkStart(task)
	local r = DrawingTable.super.onWorkStart(self, task)
	self.workPos = self.heroWorkPos

	if (task.actor == hero) then
	--[[
		hero:playAnimation(self.heroAnimation, -1, "default", self.heroAnimationFrameTime)
		task.duration = self.heroDuration
		self:playSample("SND_PENCIL_SHARPENER:sound")
		--]]
	else
		self.patientProgress = 0

		if (self.stock > 0) then
			task.actor:playAnimation(self.patientAnimation, -1, "default", self.patientAnimationFrameTime)
			self:decStock()
			self:decDisplayStock()
			task.duration = self.patientDuration
			self:playSample("SND_KID_DRAWING:sound")
		else
			task.actor:playAnimation(self.patientWaitAnimation)
			self.patientTask = task
			task.duration = -1
		end

		task.actor:setState("working in station")
	end

	self.clickable = not self.patientActive or not self.heroActive
	return true
end

function DrawingTable:onWorkFinish(task)
	DrawingTable.super.onWorkFinish(self, task)

	if (task.actor == hero) then
		--[[
		self:resetStock()
		self.heroActive = false
		if (self.patientTask) then
			self.patientTask.actor:playAnimation(self.patientAnimation, -1, "stay", self.patientAnimationFrameTime)
			self:decStock()
			self:decDisplayStock()
			self.patientTask.duration = self.patientDuration
			self:playSample("SND_KID_DRAWING:sound")
		end
		--]]
	else
		self.patientTask = nil
		task.actor:addHealth(1)
		task.actor.locationByTarget = self.characterPos
		task.actor:playDefaultAnimation()
		task.actor:setState("idle")

		self.clickable = false
	end
end

