-- ************************************ SneezeEvent ************************************ --
SneezeEvent = deriveLuaClass("SneezeEvent", "Event")

function SneezeEvent:init(desc)
	SneezeEvent.superInit(desc)

	self.scoreFailed = 20					-- score for being too late
	self.scoreSuccess = 20						-- score for helping in time
	self.intervalMin = 20000					-- min interval between occurances
	self.intervalMax = 20000					-- max interval between occurances

	self.hesitateTime = 4000					-- time you have to respond
	self.hesitateAnimation = "SNEEZE_HESITATE"	-- animation that you should respond to
	self.hesitateFrameTime = 200

	self.noSneezeTime = 8000					-- time the patient will not sneeze again
	self.sneezeAnimation = "SNEEZE"
	self.sneezeFrameTime = 140
	
	self.stationName = "desinfection_station"
end

function SneezeEvent:onInitialized()
	SneezeEvent.super.onInitialized(self)

	eventBar.showValue = false
	eventBar.showTarget = false
	eventBar.showTimer = false

	self.steambath = level:getObject(self.stationName)
	self.steambath.effectEnabled = false
	self.steambath.smoke = self.steambath:newChild({
		class = "ParticleEffect",
		x = self.steambath.effect.x,
		y = self.steambath.effect.y,
		library = "STEAMBATH_SMOKE", section = "objects",
		play = true,
	})
	self.steambath.onWorkStart = function(sb, task)
		sb.super.onWorkStart(sb, task)
		sb.effectEnabled = true
		sb.effectTimer = 2000
		self.steambath.onTick = function(sb, time)
			sb:tick(time)
			if (self.effectTimer and self.effectTimer > 0) then
				self.effectTimer = self.effectTimer - time
				if (self.effectTimer <= 0) then
					self.effectEnabled = false
				end
			end
		end
		
		-- Re-add to stay on top:
		sb:addChild(sb.effect)
		sb:addChild(sb.smoke)
	end
	self.steambath.onWorkFinish = function(sb, task)
		sb.super.onWorkFinish(sb, task)
		sb.effectEnabled = false
	end
end

function SneezeEvent:pickNewTime()
	self.sneezeTimer = math.Random(self.intervalMin, self.intervalMax)
end

function SneezeEvent:onStart()
	eventBar:showEventIcon()
	eventBar:activate()

	self:pickNewTime()
	self.sneezeTimer = 500
end

function SneezeEvent:onTick(time)
	if (self.sneezeTimer == nil) then
		return
	end

	if (self.sneezeTimer > 0) then
		self.sneezeTimer = self.sneezeTimer - time
		if (self.sneezeTimer <= 0) then
			local patientFound = false
			local patients = level.patients
			while (#patients > 0) do
				local idx = math.Random(#patients)
				local patient = patients[idx]
				if (not patient.isSneezing and
					patient:getState() == "waiting in queue" and
					patient.station == nil)
				then
					patient.animationBeforeSneeze = patient.currentAnimation
					patient.hesitateTimer = self.hesitateTime
					patient.noSneezeTimer = self.noSneezeTime

					patient.isSneezing = true
					patient:playAnimation(self.hesitateAnimation, nil, nil, self.hesitateFrameTime)
					patient:insertStep("STEAM_BATH")
					patient:createStationOverhead();

					hintManager:handle({id="patient_sneeze", sprite=level:getObject("desinfection_station"), tip="topright", })

					patientFound = true
					break
				end
				table.remove_at(patients, idx)
			end
			if (patientFound) then
				self:pickNewTime()
			else
				self.sneezeTimer = 500
			end
		end
	end

	if (level.inputEnabled) then
		local patients = level.patients
		for _,patient in pairs(patients) do
			if (patient.isSneezing and patient:getState() == "waiting in queue") then
				if (patient.hesitateTimer > 0) then
					patient.hesitateTimer = patient.hesitateTimer - time
					if (patient.hesitateTimer <= 0) then
						patient:addTask({
							class = "PlayAnimationTask",
							animation = self.sneezeAnimation,
							cycles = 2,
							frameTime = self.sneezeFrameTime,
							wait = true,
						})
						patient:addTask({
							class = "PlayAnimationTask",
							animation = patient.animationBeforeSneeze,
						})

						--patient:addHeartsWithFloater(-self.scoreFailed)
						patient:removeCurrentStep()
						patient:createStationOverhead();
					end
				elseif (patient.noSneezeTimer > 0) then
					patient.noSneezeTimer = patient.noSneezeTimer - time
					if (patient.noSneezeTimer <= 0) then
						patient.isSneezing = false
					end
				end
			end
		end
	end
end

function SneezeEvent:onEnterStation(station, character)
	if (station.name == self.stationName) then
		character.isSneezing = false
		--character:addHeartsWithFloater(self.scoreSuccess)
	end
end
