dofile("script/layouts/room1_defaults.lua")
dofile("script/shifts/shift1_5_animations.lua")

levelDesc.name = "Shift 1.5"
levelDesc.levelId = 15
levelDesc.musicMood = "1"
levelDesc.trigger =
{
	class = "TriggerSystem",
	triggers =
	{
		{ trigger = "spawn", delay=1000, hearts=3, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "FIND_SORE_SPOT", "CHECK_UP"} } }, --~1
		{ trigger = "spawn", delay=8000, hearts=1, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "STETHOSCOPE",  "MEASURE_BLOOD_PRESSURE"} } }, --~2
		{ trigger = "startEvent", delay = 1000, },
		{ trigger = "spawn", delay=20000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "CHECK_UP"} } }, --~3, solves 1
		{ trigger = "spawn", delay=18000, hearts=3, diseaseDesc = { class="Disease", steps={"FIND_SORE_SPOT", "MEASURE_BLOOD_PRESSURE"} } }, --~4, solves 2
		{ trigger = "spawn", delay=8000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "CHECK_UP"} } }, --~5, solves 3
		{ trigger = "spawn", delay=25000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "PUT_ON_BANDAID"} } }, --~6, solves 4
		{ trigger = "spawn", delay=14000, hearts=1, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "STETHOSCOPE", "MEASURE_BLOOD_PRESSURE" } } }, --~7, solves 5
		{ trigger = "spawn", delay=3000, hearts=1, diseaseDesc = { class="Disease", steps={"STETHOSCOPE", "CHECK_UP", } } }, --~8
		{ trigger = "spawn", delay=15000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "PUT_ON_BANDAID"} } }, --~9,
		{ trigger = "spawn", delay=5000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "CHECK_UP"} } }, --~10, solves 6
		{ trigger = "spawn", delay=25000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "MEASURE_BLOOD_PRESSURE"} } }, --~11, solves 7
		{ trigger = "spawn", delay=16000, hearts=2, diseaseDesc = { class="Disease", steps={"FIND_SORE_SPOT", "PUT_ON_BANDAID"} } }, --~12, solves 8&9&10&11
		{ trigger = "spawn", delay=11000, hearts=2, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "CHECK_UP"} } }, --~13, solves 12
		{ trigger = "spawn", delay=3000, hearts=1, diseaseDesc = { class="Disease", steps={"DRESSING_ROOM", "STETHOSCOPE", "MEASURE_BLOOD_PRESSURE"} } }, --~14
	}
}

function levelDesc.checkPreconditions(event)
	local obj = level:getChild("researchtable_2")
	if (obj) then
		if (not obj.occupied) then
			levelDesc.swapTable(self)
			return true
		end
	else
		obj = level:getChild("event_table")
		if (obj)then
			levelDesc.swapTable(self)
			return true
		end
	end
	return false
end

levelDesc.patientList =
{
	p0 = { weight = 50,
		base = "axel",
	},
	p1 = { weight = 50,
		base = "lady",
	},
	p2 = { weight = 50,
		base = "mother",
	},
	p4 = { weight = 25,
		base = "boy",
	},
	p5 = { weight = 25,
		base = "gran",
	},
}

levelDesc.eventDesc =
{
	class="SpecialRoom1",
	eventBarText = "TEXT_EVENT_SPECIAL1",
	eventBarImage = "EVENTBAR_ICON_R1_S5:icons",
	doGestureObject = "event_table",
	checkPreconditions = levelDesc.checkPreconditions,
	eventFinished = levelDesc.eventFinished,
	gestureDuration = 10000,
	gestures = {
			{desc=15, drainSpeed = 20, growSpeed=20, objectId="hero", animation="HOLDDOWN_LEFT", gestureLocation={x=90, y=480}, timeBeforeStart=0},
			{desc=15, drainSpeed = 18, growSpeed=20, objectId="hero", animation="HOLDDOWN_LEFT", gestureLocation={x=90, y=480}, timeBeforeStart=20000},
			{desc=15, drainSpeed = 18, growSpeed=20, objectId="hero", animation="HOLDDOWN_LEFT", gestureLocation={x=90, y=480}, timeBeforeStart=30000},
	},
	scoreOneDone=1,
	scoreAllDone=5,
}

function levelDesc.eventFinished(event)
--print("finish")
	local rTable2 = level:getObject("researchtable_2")
	if (rTable2) then
		rTable2:setMaxOccupants(1)
		rTable2:setVisible(true)
		level:getObject("event_table").visible = false
	end
end

function levelDesc:onInitialized()
	--level:getObject("event_special_patient_2").visible = false
	--create stretcher if there is no 2nd bed
	local desc = {
		class = "DoGestureObject",
		name="event_table",
		x=0,
		y = 526,
		floor = -100,
		baseAnimation = "ROOM1_EVENT_STRETCHER",
		workPos = {x=152, y=663},
		visible=false,
		checkBoxPos= {x=35, y=127},
		obstructs = {{x=5, y=7}, {x=6, y=7}, {x=7, y=7}, {x=8, y=7}, {x=9, y=7}, {x=10, y=7}, {x=11, y=7}, {x=12, y=7}, {x=13, y=7}, {x=14, y=7}, {x=15, y=7}, {x=16, y=7}, {x=17, y=7}, {x=18, y=7}, {x=19, y=7}, {x=20, y=7}, {x=21, y=7}, {x=22, y=7}, {x=23, y=7}, {x=24, y=7}, {x=25, y=7}, {x=26, y=7}, {x=27, y=7},
					 {x=5, y=12}, {x=6, y=12}, {x=7, y=12}, {x=8, y=12}, {x=9, y=12}, {x=10, y=12}, {x=11, y=12}, {x=12, y=12}, {x=13, y=12}, {x=14, y=12}, {x=15, y=12}, {x=16, y=12}, {x=17, y=12}, {x=18, y=12}, {x=19, y=12}, {x=20, y=12}, {x=21, y=12}, {x=22, y=12}, {x=23, y=12}, {x=24, y=12}, {x=25, y=12}, {x=26, y=12}, {x=27, y=12}},
	}
	local obj = level:add(desc)

	if (level:getObject("researchtable_2") == nil) then
		obj.x = obj.x + 20
		obj.visible = true
	end
end

function levelDesc:swapTable()
	--no table present, spawn a stretcher
	--level:getObject("event_special_patient_2").visible = true
	local rTable2 = level:getObject("researchtable_2")
	--Remove original table
	if (rTable2) then
		--table present, set to occupied while patient is there
		rTable2:setMaxOccupants(0)
		rTable2:setVisible(false)
		local obj = level:getObject("event_table")
		obj.y = 513
		obj.baseAnimation = rTable2.baseAnimation
		obj.visible = true
	end
end

levelDesc.gestureCount = 0
function levelDesc:eventThanks()
	-- Gabriel and john swapped bodies so gabriel will pick up the patients
	local john = level:getChild("gabriel")
	john:addTask({class="WalkToTask", target={x=280,y=620}})
	john:addTask({class="PlayAnimationTask", animation="INJECTING", cycles=1, whenFinished="default", wait=true})
	john:addTask({class="Task", duration=1000})
	if (self.gestureCount == 0) then
		john:addTask({class="MessageTask", duration=4000, textId="TEXT_R1_SH5_21"})
	end
	if (level.event.won == false) then
	    john:addTask({class="WalkToTask", target={x=230, y=620}})
		john:addTask({class="PlayAnimationTask", animation="WORKSTAPH", cycles=-1, frameTime=300, whenFinished="default"})
	end
	self.gestureCount = self.gestureCount+1
end

function levelDesc:activeGesture()
	if (self.gestureCount > 0) then
		local john = level:getChild("gabriel")
		john:addTask({class="CallTask", func=function() SpecialRoom1.super.activateGesture(self.event) end})		
		john:addTask({class="MessageTask", duration=4000, textId="TEXT_R1_SH5_22"})
		return true
	end
	return false
end
