dofile("script/layouts/room5_defaults.lua")
dofile("script/shifts/shift5_2_animations.lua")

levelDesc.name = "Shift 5.2"
levelDesc.levelId = 52
levelDesc.musicMood = "2"
levelDesc.eventDone = true
levelDesc.trigger =
{
	class = "TriggerSystem",
	triggers =
	{
		{ trigger = "spawn", delay=1000, hearts=2, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE", } } },
		{ trigger = "object", delay=1, name="baby_bed_back", type="DIAPER",  },
		{ trigger = "spawn", delay=5000, hearts=2, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE",} } },
		{ trigger = "spawn", delay=6000, hearts=3, diseaseDesc = { class="Disease", steps={"DIAPER_PRACTICE"} } },
		{ trigger = "spawn", delay=14000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
		{ trigger = "spawn", delay=6000, hearts=4, diseaseDesc = { class="Disease", steps={ "YOGA"} } },
		{ trigger = "spawn", delay=10000, hearts=2, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE",} } },
		{ trigger = "object", delay=1, name="baby_bed_front", type="MILK",  },
		{ trigger = "spawn", delay=20000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
		{ trigger = "object", delay=1000, name="baby_bed_back", type="BURP",  },
		{ trigger = "spawn", delay=4000, hearts=1, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE", "YOGA"} } },
		{ trigger = "spawn", delay=15000, hearts=3, diseaseDesc = { class="Disease", steps={"DIAPER_PRACTICE"} } },
		{ trigger = "object", delay=1, name="baby_bed_front", type="DIAPER",  },
		{ trigger = "spawn", delay=20000, hearts=1, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE", "DIAPER_PRACTICE"} } },
		{ trigger = "object", delay=1, name="baby_bed_back", type="MILK",  },
		{ trigger = "spawn", delay=6000, hearts=1, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE", "YOGA"} } },
		{ trigger = "spawn", delay=10000, hearts=3, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE"} } },
		{ trigger = "object", delay=1, name="baby_bed_back", type="DIAPER",  },
		{ trigger = "spawn", delay=22000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
		{ trigger = "spawn", delay=10000, hearts=3, diseaseDesc = { class="Disease", steps={"YOGA"} } },
		{ trigger = "spawn", delay=4000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
		{ trigger = "spawn", delay=20000, hearts=1, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE", "DIAPER_PRACTICE"} } },
		{ trigger = "object", delay=1, name="baby_bed_back", type="MILK",  },
		{ trigger = "spawn", delay=6000, hearts=3, diseaseDesc = { class="Disease", steps={"WEIGHING", "STETHOSCOPE"} } },
		{ trigger = "spawn", delay=17000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
		{ trigger = "object", delay=1, name="baby_bed_front", type="BURP",  },
		{ trigger = "spawn", delay=10000, hearts=3, diseaseDesc = { class="Disease", steps={"YOGA"} } },
		{ trigger = "spawn", delay=1000, hearts=3, diseaseDesc = { class="Disease", steps={"STETHOSCOPE"} } },
	}
}
levelDesc.patientList =
{
	p0 = { weight = 500,
		base = "preg",
	},
	p1 = { weight = 500,
		base = "preg2",
	},
	p2 = { weight = 500,
		base = "preg3",
	},
}

function levelDesc:handleTrigger(trigger, meta)
	CareLevel.handleTrigger(self, trigger, meta)
	if (trigger == "6hourslater") then
		level:incPaused()

		local black = self:add({class="SpriteExt", x=0, y=0, alpha=0, width=1024, height=768, color=0xff000000, draw="color", floor=300})
		local years = self:newChild({class="Label", x=1024/2, y=768/2, font="FONT_MINIGAME_HEADING:default", textId="TEXT_R5_SH2_13", alpha=0, anchor="center"})

		black:animate({
			{alpha=1, duration=2000},
			{delay=600},
			{sprite=years, alpha=1, duration=500},
			{delay=3500},
			{
				{sprite=years, alpha=0, duration=2000},
				{alpha=0, duration=2000},
			},
			{sprite=years, remove=true},
			{remove=true, call=function()
				level:decPaused()
			end},
		})
	end
end

local onEventStarted = function(self)
	GestureEvent.onStart(self) -- This is right
	local woman = level:getChild("event_gesture_kid")
	woman.baseAnimation = "PREGNANT_W_LAY"
	woman:playAnimation("PUFFING")	
end

gestureX = 230
gestureY = 370
levelDesc.eventDesc =
{
	class="GestureEvent",
	eventBarImage = "EVENTBAR_ICON_GESTURE:icons",
	gestureLocation = {x=150, y=500},
	doGestureObject = "event_gesture_kid",
	hintTip = "bottomleft",
	eventScore = 30,
	scoreOneDone = 1,
	scoreAllDone = 1,
	onStart = onEventStarted,
	doWonAnimation = false,
	gestures = {
		{desc=10, objectId="hero", animation="FEEL", gestureLocation={x=gestureX, y=gestureY}},
		{desc=9, objectId="hero", animation="FEEL", gestureLocation={x=gestureX, y=gestureY}},
		{desc=15, drainSpeed = 30, growSpeed=20, objectId="hero", animation="PUFF", gestureLocation={x=gestureX, y=gestureY}},
		{desc=6, objectId="hero", animation="RUB_SWITCH", gestureLocation={x=gestureX, y=gestureY}},
		{desc=5, objectId="hero", animation="RUB_BELLY", gestureLocation={x=gestureX, y=gestureY}},
		{desc=6, objectId="hero", animation="RUB_SWITCH", gestureLocation={x=gestureX, y=gestureY}, trigger="6hourslater"},
		{desc=15, drainSpeed = 30, growSpeed=20, objectId="hero", animation="PUFF", gestureLocation={x=gestureX, y=gestureY}},
		{desc=5, drainSpeed = 30, growSpeed=20, objectId="hero", animation="RUB_BELLY", gestureLocation={x=gestureX, y=gestureY}},
		{desc=15, drainSpeed = 30, growSpeed=20, objectId="hero", animation="PUFF", gestureLocation={x=gestureX, y=gestureY}},
		{desc=6, drainSpeed = 30, growSpeed=20, objectId="hero", animation="RUB_SWITCH", gestureLocation={x=gestureX, y=gestureY}},
		{desc=15, drainSpeed = 30, growSpeed=20, objectId="hero", animation="PUFF", gestureLocation={x=gestureX, y=gestureY}},
		{desc=5, drainSpeed = 30, growSpeed=20, objectId="hero", animation="RUB_BELLY", gestureLocation={x=gestureX, y=gestureY}},
		{desc=15, drainSpeed = 30, growSpeed=20, objectId="hero", animation="PUFF", gestureLocation={x=gestureX, y=gestureY}},
	},
}

-- Overrides createPostShift and eventWon to hack in the 'playing an event before shiftend dialog'
function levelDesc:eventWon()
	level.event.fillSprite.visible = false
	eventBar:deactivate()
	self.completed = true
	self.won = true

	-- ROEL FIX
	local tmp = taskSystem:createAnimation("eventWon")
	if (tmp) then
		tmp:atEnd({class="CallTask", func=function() CareLevel.onShiftFinish(level) end})
	end
end

function levelDesc:createPostShiftAnimation()
	level:getObject("bed_back").clickable = false
		self:fireTrigger("startEvent")
	self.eventDone = false
	level.event.eventWon = levelDesc.eventWon
end

function levelDesc:onTick(time)
    self:tick(time)
	self:randomSneeze(time)

	-- Hack, because shiftfinish sets gestureEvent.completed to true,
	-- I will reset it to false until almost the end of the event, to be sure
	if (not self.eventDone) then
		if (level.event and level.event.currentGesture < table.icount(level.event.gestureObjects)) then
			level.event.completed = false
		else
			self.eventDone = true
		end
	end
end
