-- ************************************ Computer ************************************ --
Computer = deriveLuaClass("Computer", "SpriteExt")
function Computer:onTick(time)
	Computer.tick(self, time)
	if (not self.isPlayingAnimation) then
		self:playAnimation(self.defaultAnimation, -1, nil, self.defaultFrameTime)
	end
end

 -- ************************************ Checkout Desk ************************************ --
CheckOutDesk = getClass("CheckOutDesk")

CheckOutDesk.floaterOffset = {x=30, y=-200}
CheckOutDesk.goldFloaterPos = {x=30, y=-200}

function CheckOutDesk:addSelectedObjectToStationTasks()
	local obj = level.selectedObject
	obj:addTask({class="CallTask", func=function()
		self:comeIn(obj)
		if (level.selectedObject == obj or level.selectedObject == self) then
			level:resetSelectedObject()
		end
		obj:removeOverhead(false)
	end})
	obj:addTask({class="LeaveStationToCheckoutDeskTask"})	
	obj:enterQueue(self.queue)
	obj:addTask({class="PlacePatientTask", target=self})
end

function CheckOutDesk:addToStationTask()
	if (CheckOutDesk.super.addToStationTask(self)) then -- CheckOutDesk derives from Station
		if (not self.overhead) then
			self:createOverhead("ICON_CHECKOUT", true, true)
		end
		return true
	end
	hero:addTask({class="SetTask", positionLock=self})
	hero:addTask({class="WorkTask", positionLock=self, target=self})
	return true
end

function CheckOutDesk:onWorkStart(task)
	CheckOutDesk.super.onWorkStart(self, task)
	task.duration = -1
	self.combo = 0
	self.goldCombo = 0

	self.workAnimationFrameTime = self.workAnimationFrameTime or 60
	self.workAnimation = self.workAnimation or "DO_CHECKOUT"

	self.computer = level:getSpriteExt("computer")
	self.cancelled = false

	return true
end

function CheckOutDesk:onWorkUpdate(task, time)
	local hasPending, hasCharacterToDoNow = false, false
	local character
	local spots = self.queue.spots
	for _,v in ipairs(spots) do
		hasPending = true
		local c = v.inQueueCharacter
		assert(c)
		if (c and c:getState() == "waiting at checkout") then
			hasCharacterToDoNow = true
			character = c
			break
		end
	end

	if (hasPending) then
		if (character and self:heroNotCheckingOut()) then

			-- Score
			local x, y = character.x, character.y-160
			local health = character.health
			if (health == "seriouslysick") then
				level:heartsToBar(1, x, y, false)
			elseif (health == "sick") then
				level:heartsToBar(2, x, y, false)
			elseif (health == "feelingbetter") then
				level:heartsToBar(3, x, y, false)
			elseif (health == "cured") then
				level:heartsToBar(4, x, y, false)
			elseif (health == "healthy") then
				level:heartsToBar(5, x, y, false)
			elseif (health == "gold") then
				level:heartsToBar(7, x, y, true)
				character:handPresent(self.goldCombo)
				self.goldCombo = self.goldCombo + 1
				level:bm_incNumGoldPatients()				
			end

			-- Level info
			level:incPatientsHelped()
			player.gameStats:incPatientsHealed();
			player.trophyManager:checkPatientHealedTrophy();
	
			-- Combo
			self.combo = self.combo + 1

			local comboHearts = 0
			if (self.combo >= 6) then
				comboHearts = 1
			elseif (self.combo >= 5) then
				comboHearts = 2
			elseif (self.combo >= 4) then
				comboHearts = 2
			elseif (self.combo == 3) then
				comboHearts = 2
			elseif (self.combo == 2) then
				comboHearts = 1
			end

			if (comboHearts > 0) then
				-- Do "Combo +1" floater
				level:doFloater(
					string.format(ghf.getString("TEXT_FLOATER_COMBO"), comboHearts),
					character.screenX + self.floaterOffset.x, character.screenY + self.floaterOffset.y)
				level:heartsToBar(comboHearts, x, y, character.health == "gold")
			end

			-- Animations
			hero:playAnimation(self.workAnimation, 1, "stay", self.workAnimationFrameTime)
			self:playSample("SND_PATIENT_CHECKED_OUT:sound")
            
			if (self.computer) then
				self.computer:playAnimation("CHECKOUT", 1, "default", self.computer.checkOutFrameTime)
			end

			-- Character
			character:setState("leaving")
		end
	else
		self:onComboChainEnd()

		if (hero.currentAnimation == self.workAnimation and not hero.isCyclingAnimation) then
			hero:playDefaultAnimation()
		end
	end

	if ((not hasCharacterToDoNow or self.cancelled) and
		self:heroNotCheckingOut() and
		taskSystem:hasAnyTaskFor(hero)) then

		print("cancelled")

		return true
	end

	if (level.hasShiftFinished) then
		return true
	end
	return false
end

function CheckOutDesk:heroNotCheckingOut()
	return not hero.isCyclingAnimation or
		hero.currentAnimation ~= self.workAnimation
end

function CheckOutDesk:onWorkFinish(task)
	CheckOutDesk.super.onWorkFinish(self, task)
	self:onComboChainEnd()
	hero:playDefaultAnimation()
	if (self.computer) then
		self.computer:playAnimation("BLINK", -1, nil, 300)
	end
	self.cancelled = false
end

function CheckOutDesk:onCancelChain(task)
	self.cancelled = true
end

function CheckOutDesk:onComboChainEnd()
	self:removeOverhead()

	if (self.combo > 0) then
        player.trophyManager:checkComboCheckoutTrophy(self.combo)
    end

    if (self.goldCombo > 0) then
        player.trophyManager:checkGoldenPatientTrophy(self.goldCombo)
    end

	if (self.goldCombo >= 2) then
		local x = self.screenX + self.goldFloaterPos.x
		local y = self.screenY + self.goldFloaterPos.y
		level:doFloater(string.format(ghf.getString("TEXT_FLOATER_GOLD_COMBO"), self.goldCombo), x, y)
		level:heartsToBar(self.goldCombo - 1, x, y, true)
--print("goldcombo")
	end

	self.combo = 0
	self.goldCombo = 0
end
