-- ************************************ Special Mix n Match event Room2 ************************************ --
SpecialRoom2 = deriveLuaClass("SpecialRoom2", "Event")

SpecialRoom2.presents = {}
SpecialRoom2.doctors = {}
SpecialRoom2.presentsDone = 0

function SpecialRoom2:init(desc)
	SpecialRoom2.superInit(self, desc)
	self.presentsDone = 0
	self.completed = false
end

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

	for i=1, table.icount(self.presents) do
		self.presents[i] = _G[self.presents[i]] or level:getChild(self.presents[i])
		assert(isTable(self.presents[i]))
		self.presents[i].visible = false
	end

end

function SpecialRoom2:onStart()
	for i=1, table.icount(self.presents) do
		self.presents[i]:setEnabled(true)
	end

	for i=1, table.icount(self.doctors) do
		self.doctors[i] = _G[self.doctors[i]] or level:getChild(self.doctors[i])
		assert(isTable(self.doctors[i]))
		self.presents[i].hasPresent = false
		self.presents[i].doctorName = self.doctors[i].name
		self.doctors[i]:createOverhead("ICON_PRESENT" .. i .. ":icons", false)
		self.doctors[i].onClick = SpecialRoom2.onDoctorClick
	end
    
    --Manual playsample, else every doctor plays the sample (spam)
    hero:playSample("SND_TREATMENT_BALLOON:sound")

	hero.eventPresent = "no present"

	hintManager:handle({id="mix_n_match", x=-1, y=-1})

	eventBar.progress = 0
	eventBar.startValue = 0
	eventBar.targetValue = table.icount(self.presents)
	eventBar.showValue = false
	eventBar.showTarget = false
	eventBar:activate()
end

function SpecialRoom2.onDoctorClick(doctor)

	level.event:enableDoctors(false)

	if (hero.eventPresent == doctor.name) then
		-- correct doctor clicked
		hero:addTask({class="WalkToTask", target={x=doctor.worldX, y=doctor.worldY + 40}})
		hero:addTask({class="SetTask", direction="up"})
		hero:addTask({class="Task", duration=600})
		hero:addTask({class="CallTask", func=function()
			doctor.hasPresent = true
			doctor.playIdleAnimations = false
			doctor:playAnimation("IDLE_PRESENT")
			doctor:removeOverhead()
			level.event:nextPresent()
			eventBar:incValue(1)
			if (not doctor.wasWrong) then
				level:addHeartsWithFloater(1, doctor.x, doctor.y - doctor.height, false)
			end
		end})
	else
		-- wrong doctor clicked
		doctor.wasWrong = true
		level.event.somethingWrong = true
		hero:addTask({class="WalkToTask", target={x=doctor.worldX, y=doctor.worldY + 40}})
		hero:addTask({class="CallTask", func=function()
			doctor:addTask({class="SetTask", alpha=1, color=0xff0000})
			doctor:addTask({class="Task", duration=500})
			doctor:addTask({class="SetTask", alpha=1, color=0xffffff})
			doctor:addTask({class="CallTask", func=function()
				hero.eventPresentObject:setEnabled(true)
				level.event:resetPresent()
			end})
		 end})
	end
end

function SpecialRoom2:resetPresent()
	hero.eventPresent = "no present"
	hero.eventPresentObject = nil
	self:enableDoctors(false)
end

function SpecialRoom2:nextPresent()
	self:resetPresent()
	self.presentsDone = self.presentsDone + 1

	if (self.presentsDone == table.icount(self.doctors)) then
		self.completed = true

		hero:addTask({class="Task", duration=2000})
		hero:addTask({class="CallTask", func=function()
			if (not self.somethingWrong) then
				self.won = true
				if (isTable(taskSystem:getAnimationDesc("eventWon"))) then
					local anim = taskSystem:createAnimation("eventWon")
					anim:atEnd({class="CallTask", func=function() CareLevel.createPostShiftAnimation(level) end})
				else
					self.won = true
					CareLevel.createPostShiftAnimation(level)
				end
			else
				self.won = false
				CareLevel.createPostShiftAnimation(level)
			end
		end})
	end
end

function SpecialRoom2:enableDoctors(enable)
	for i=1, table.icount(self.doctors) do
		self.doctors[i].clickable = not self.doctors[i].hasPresent and enable
	end
end

-- PresentObject
PresentObject = deriveLuaClass("PresentObject", "Object")

function PresentObject:setEnabled(enabled)
	self.visible = enabled
	self.clickable = enabled
end

function PresentObject:onClick()
	if (hero.eventPresent == "no present") then
		hero.eventPresent = self.doctorName
		hero.eventPresentObject = self
		hero:addTask({class="WalkToTask", target=self})
		hero:addTask({class="Task", duration=500})
		hero:addTask({class="PlayAnimationTask", animation="PICKUP", cycles=1, wait=true})
		hero:addTask({class="CallTask", func=function()
			self:setEnabled(false)
			level.event:enableDoctors(true)
		end})
	else
		self:addTask({class="SetTask", color=0xff0000, clickable=false})
		self:addTask({class="Task", duration=500})
		self:addTask({class="SetTask", color=0xffffff, clickable=true})
	end
end
