dofile("script/layouts/room3_defaults.lua")
dofile("script/shifts/shift3_4_animations.lua")

levelDesc.name = "Shift 3.4"
levelDesc.levelId = 34
levelDesc.musicMood = "2"
levelDesc.trigger =
{
	class = "TriggerSystem",
	triggers =
	{
		{ trigger = "spawn", delay=1, patient="p0", hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "GIVE_INJECTION", "REMOVAL"} } },
		{ trigger = "spawn", delay=1000, patient="p3",hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "BANDAID", "SPATULA"} } },
		{ trigger = "spawn", delay=20000, patient="p1", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "SPATULA",} } },
		{ trigger = "spawn", delay=1000, patient="p4", hearts=3, diseaseDesc = { class="Disease", steps={"TOILET","REMOVAL"} } },
		{ trigger = "startEvent", delay = 20000, },
		{ trigger = "spawn", delay=15000, patient="p2", hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "BANDAID", "SPATULA",} } },
		{ trigger = "spawn", delay=1000, patient="p5", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "GIVE_INJECTION",} } },
		{ trigger = "spawn", delay=25000, patient="p0", hearts=2, diseaseDesc = { class="Disease", steps={"BANDAID", "SPATULA"} } },
		{ trigger = "spawn", delay=4000, patient="p3", hearts=3, diseaseDesc = { class="Disease", steps={"GIVE_INJECTION"} } },
		{ trigger = "spawn", delay=25000, patient="p2", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "REMOVAL"} } },
		{ trigger = "spawn", delay=1000, patient="p5", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "SPATULA"} } },
		{ trigger = "spawn", delay=5000, patient="p1", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "BANDAID"} } },
		{ trigger = "spawn", delay=25000, patient="p4", hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "GIVE_INJECTION", "SPATULA",} } },
		{ trigger = "spawn", delay=30000, patient="p0", hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "BANDAID", "REMOVAL",} } },
		{ trigger = "spawn", delay=1000, patient="p5", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "GIVE_INJECTION"} } },
		{ trigger = "spawn", delay=23000, patient="p2", hearts=2, diseaseDesc = { class="Disease", steps={"BANDAID", "SPATULA"} } },
		{ trigger = "spawn", delay=1000, patient="p4", hearts=2, diseaseDesc = { class="Disease", steps={"GIVE_INJECTION", "SPATULA"} } },
		{ trigger = "spawn", delay=23000, patient="p1", hearts=3, diseaseDesc = { class="Disease", steps={"GIVE_INJECTION",} } },
		{ trigger = "spawn", delay=4000, patient="p3", hearts=3, diseaseDesc = { class="Disease", steps={"SPATULA",} } },
		{ trigger = "spawn", delay=20000, patient="p0", hearts=1, diseaseDesc = { class="Disease", steps={"TOILET", "BANDAID", "REMOVAL",} } },
		{ trigger = "spawn", delay=8000, patient="p2", hearts=2, diseaseDesc = { class="Disease", steps={"BANDAID", "SPATULA"} } },
		{ trigger = "spawn", delay=1000, patient="p5", hearts=2, diseaseDesc = { class="Disease", steps={"TOILET", "GIVE_INJECTION"} } },
	}
}
levelDesc.patientList =
{
	p0 = { weight = 500,
		base = "boy",
	},
	p1 = { weight = 500,
		base = "boy2",
	},
	p2 = { weight = 500,
		base = "boy3",
	},
	p3 = { weight = 500,
		base = "girl",
	},
	p4 = { weight = 500,
		base = "girl2",
	},
	p5 = { weight = 500,
		base = "girl3",
	},
}

function levelDesc.checkPreconditions(event)
--print("preconditions")
	local obj = level:getChild("lice_chair_right")
	if (obj) then
		if (not obj.occupied) then
			levelDesc.swapChair(self)			
			return true
		end
	else
		obj = level:getChild("event_temp_chair")
		if (obj)then
			levelDesc.swapChair(self)
			return true
		end
	end
	return false
end

function levelDesc.checkWinConditions(event)
	local toilet = level:getStation("toilet_girls")
	return (not toilet or not toilet.occupied)
end

function levelDesc:handleTrigger(trigger, meta)
	CareLevel.handleTrigger(self, trigger, meta)	
	if (trigger == "ipecac") then
		if (isTable(taskSystem:getAnimationDesc("eventIpecac")))then
			taskSystem:createAnimation("eventIpecac")
		end
	end
end

local doEventFinished = function()
	GestureEvent.eventFinished(level.event)
	local rightChair = level:getObject("lice_chair_right")
	if (rightChair) then
		level:getObject("event_temp_chair"):setObsolete(true)
		rightChair:setMaxOccupants(1)
		rightChair:setVisible(true)		
	end
end

gestureX = 600
gestureY = 400
levelDesc.eventDesc =
{
	class="GestureEvent",
	eventScore="20",
	eventBarImage = "EVENTBAR_ICON_GESTURE:icons",
	gestureLocation = {x=500, y=500},
	doGestureObject = "event_gesture_girl",
	visibleOnStart = true,
	checkPreconditions = levelDesc.checkPreconditions,
	checkWinConditions = levelDesc.checkWinConditions,
	eventFinished = doEventFinished,
	gestures = {
		{desc=12, objectId="hero", animation="DIAGONAL_UP_STOMACH", gestureLocation={x=gestureX, y=gestureY}},
		{desc=8, objectId="hero", animation="RIGHTLEFT_STOMACH", gestureLocation={x=gestureX-25, y=gestureY-10}},
		{desc=13, objectId="hero", animation="DIAGONAL_DOWN_STOMACH", gestureLocation={x=gestureX-5, y=gestureY}},
		{desc=16, drainSpeed = 25, growSpeed=30, objectId="hero", animation="HOLD_STOMACH", gestureLocation={x=gestureX, y=gestureY}, trigger="ipecac"},

		{desc=17, drainSpeed = 30, growSpeed=15, objectId="hero", animation="HORIZONTAL_GRIND", gestureLocation={x=600, y=400}, timeBeforeStart=20},
		{desc=18, drainSpeed = 30, growSpeed=15, objectId="hero", animation="VERTICAL_GRIND", gestureLocation={x=600, y=400}},
		{desc=19, drainSpeed = 30, growSpeed=15, objectId="hero", animation="DIAGONAL_DOWN_GRIND", gestureLocation={x=600, y=400}},
		{desc=20, drainSpeed = 30, growSpeed=15, objectId="hero", animation="DIAGONAL_UP_GRIND", gestureLocation={x=600, y=400}, timeBeforeStart=15000},
		{desc=3, drainSpeed = 25, growSpeed=15, objectId="hero", animation="POUR_CUP", gestureLocation={x=600, y=400}},
		{desc=8, objectId="hero", animation="GIVE_CUP", gestureLocation={x=600, y=400}},
	},
	scoreOneDone=1,
	scoreAllDone=1,
	hintTip = "bottomleft",
}

function levelDesc:onInitialized()
	-- Create stretcher if there is no 2nd bed
	local desc = {
		class = "Object",
		name="event_temp_chair",
		x = 598,
		y = 553,
		floor = -60,
		baseAnimation = "EVENT_TEMP_CHAIR",
		workPos = {x=80, y=590},
	}
	local obj = level:add(desc)

	local rightChair = level:getObject("lice_chair_right")
	if (rightChair) then
		obj.visible = false
	end
end

function levelDesc:swapChair()
	--no table present, spawn a stretcher
	local rightChair = level:getObject("lice_chair_right")
	local obj = level:getObject("event_temp_chair")

	if (rightChair) then
		--chair present, set to occupied while patient is there
		rightChair:setMaxOccupants(0)
		rightChair:setVisible(false)
		obj.baseAnimation = rightChair.baseAnimation
		obj.x = rightChair.x
		obj.y = rightChair.y
	end

	obj.visible = true
end
