-- ************************************ Special Event Room 5 (Flu shot) ************************************ --
SpecialRoom5 = deriveLuaClass("SpecialRoom5", "Event")
SpecialRoom5.completed = false
SpecialRoom5.originalClick = nil
SpecialRoom5.firstSwap = true
SpecialRoom5.contaminationTime = 10000
SpecialRoom5.timer = 0
SpecialRoom5.heartDropRateMult = 2

function SpecialRoom5:init(desc)
	SpecialRoom5.superInit(self)
	self.seenHint = false
end

function SpecialRoom5:onInitialized()
	SpecialRoom5.super.onInitialized(self)
end

function SpecialRoom5:onStart()
	eventBar.showValue = false
	eventBar.showTarget = false
	eventBar:activate()
end

function SpecialRoom5:onTick(time)
	local isSpecialPresent = false
	for i = 1,#level.specialPatients do
		isSpecialPresent = true
		if (self.firstSwap == true) then
			self.originalClick = level.specialPatients[i].onClick
			self.firstSwap = false
		end
		if (self.timer <= 0) then
			self.timer = self.contaminationTime
		end
		level.specialPatients[i].onClick = SpecialRoom5.onClickSpecialPatient

		if (not self.seenHint and level.specialPatients[i].state == "waiting in queue") then
			hintManager:handle({id="flu_event", sprite=level.specialPatients[i]})
			self.seenHint = true
		end
	end

	-- There is a flu patient, run down contamination timer
	if (isSpecialPresent == true) then
		self.timer = self.timer - time
		if (self.timer < 0) then
			local patients = level.patients
			local patient = nil
			for i = 1,#patients do
				if (not patients[i].hasBeenIll) then
					patient = patients[i]
					if (patient and not taskSystem:hasTaskFor(patient) and (patient.station == nil or (patient.station and patient.station.workDone))) then
						patient.special = true
						patient.hasBeenIll = true
						local anim = patient.currentAnimation
						patient.baseAnimation = patient.baseAnimation .. "_FLU"
						patient:playAnimation(anim, -1, nil, 100)
						patient.healthDropMultiplier = self.heartDropRateMult
						break
					end
				end
			end

		end
	end
end

function SpecialRoom5:onClickSpecialPatient()
	local node = self.location
	node.y = node.y + 40
	node.x = node.x - 10
	if (self.station) then
		node = self.station.workPos
	end
	hero:addTask({class="WalkToTask", target={node.x,node.y}})
	hero:addTask({class="PlayAnimationTask", animation="INJECTION_FLU", cycles=1, whenFinished="default", frameTime=150, wait=true})
	hero:addTask({class="CallTask", func=SpecialRoom5.fixPatient, object=self, actor=self })
end

function SpecialRoom5:fixPatient()
	self.special = false
	self.onClick = level.event.originalClick
	local anim = self.currentAnimation
	if (string.find(self.baseAnimation, "_FLU", 1)) then
		self.baseAnimation = string.sub(self.baseAnimation, 0, string.len(self.baseAnimation)-4)
	end
	self:playAnimation(anim, -1, nil, 100)
	self.healthDropMultiplier = 1.0
end

function SpecialRoom5:handleTrigger(trigger, meta)
	if (trigger == "spawn") then
		-- Create flu patient?
		if ( meta["special"] == true) then
		
			meta["baseAnimation"] = "PATIENT_PREG_FLU"	
			local val = math.Random(3)
			meta["patient"] = "p" .. val-1			
			if (val > 1) then
				meta["baseAnimation"] = "PATIENT_PREG".. val .. "_FLU"	
			end
			
			meta["healthDropMultiplier"]  = self.heartDropRateMult
			meta["hasBeenIll"] = true
		end
	else
		SpecialRoom5.super.handleTrigger(self, trigger, meta)	
	end
end
