dofile("script/layouts/sarahs_office.lua")
dofile("script/shifts/outro3_animations.lua")

levelDesc.name = "Outro 2"
levelDesc.layoutId = 17
levelDesc.areaId = 0
levelDesc.levelId = 157
levelDesc.music = "SELECTION"
levelDesc.musicMood = ""
levelDesc.isGamePlayLevel = false
levelDesc.hideGUI = true
levelDesc.heroDesc =
{
	name = "hero",
	base = "sarah",
	x = 433,
	y = 575,
}

function levelDesc:onShiftStartBegin()
	self.timerActive = false
	self:shiftStartEnd() -- Start shift without start shift dialog.
end

function levelDesc:handleTrigger(trigger, meta)
	CareLevel.handleTrigger(self, trigger, meta)
	if (trigger == "end outro3") then
		local dlg = scene:createDialog("DIALOG_CREDITS")
		dlg.playShowHideSound = false
		dlg.onClose = function()
			self:onLevelEndContinue()
		end
		scene:openDialog(dlg)
	end
end

function levelDesc:onInitialized()
	self.class.onInitialized(self)
	self:setSpecialState()

	hero:addTask({class="WalkToTask", target={930,300}})
	hero:addTask({class="WalkToTask", target={930,550}})
	hero:addTask({class="WalkToTask", target={700,550}})
	
	player.trophyManager:resetTrophyLooks()

	self.fish = {
		level:getSpriteExt("TROPHY2"),
		level:getSpriteExt("TROPHY3"),
		level:getSpriteExt("TROPHY4"),
		level:getSpriteExt("TROPHY5"),
		level:getSpriteExt("TROPHY6"),
	}

	for _,v in ipairs(self.fish) do
		if (v) then
			self:newSwim(v)
			local y = v.y
			v:doEvery(math.Random(2000), function()
				local d1 = math.Random(2000, 4000)
				local d2 = math.Random(2000, 4000)
				v:animate(
					{y=y-math.Random(1.5, 4), duration=d1, style="easeInOut"},
					{y=y+math.Random(1.5, 4), duration=d2, style="easeInOut"})
				return d1 + d2 + 10
			end)
			local sy = (v.y-540)/2
			v.shadow = level:add({
				class="SpriteExt", floor=55 - sy,
				x=v.x, y=594 + sy,
				anchor="top",
				baseAnimation="FISH_SHADOW"})
		end
	end
	
	local presentDesc = {
		class = "Object",
		name="present_open",
		x=325, y=419,
		floor = 100,
		baseAnimation = "OUTRO_PRESENT_OPEN",
	}
	self:add(presentDesc)
	
	print("self:add(presentDesc)")
	
end

function levelDesc:newSwim(v)
	v.s = math.Random(0, 0.025)
	v.dir = ((math.Random(1000) < 500) and -1) or 1
	v.timer = math.Random(1000, 10000)
end

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

	for _,v in ipairs(self.fish) do
		if (v) then
			v.x = v.x + v.s * time * v.dir
			if (v.x > 896) then
				v.x = 896
				self:newSwim(v)
				v.dir = -1
			elseif (v.x < 733) then
				v.x = 733
				self:newSwim(v)
				v.dir = 1
			else
				v.timer = v.timer - time
				if (v.timer <= 0) then
					self:newSwim(v)
				end
			end
			v.flipHorizontal = (v.dir == 1)
			v.shadow.x = v.x + v.width/2
		end
	end
end
