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

RoomDialog = deriveLuaClass("RoomDialog", "DialogCare")

levelDesc.name = "Trophy Scene"
levelDesc.levelId = 99
levelDesc.areaId = 0
levelDesc.isGamePlayLevel = false
levelDesc.autoStartShift = false
levelDesc.music = "SELECTION"
levelDesc.heroDesc =
{
	name = "hero",
	base = "sarah",
	x = 1000,
	y = 300,
}

function levelDesc:onMouseMoved(x, y)
    self.boxLabel.textID = self.textBoxID
    self.headerLabel.textID = self.headerBoxID
	if (self.boxLabel) then
        for i = 1,30 do
            local trophy = level:getChild("TROPHY"..i)
            if (trophy and trophy:isInside(x, y)) then
                local upgradeLevel = player:upgradeLevel("TROPHY"..i, 10)
                if (string.find(trophy.baseAnimation, "GHOST")) then
                    upgradeLevel = 0
                end

				local ul = upgradeLevel
				if (ul == 0) then ul = 1 end
                self.headerLabel.text = ghf.getString("TROPHY"..i.."_POPUP_TEXT_"..ul)

				local text = ghf.getString("TROPHY"..i.."_TITLE_"..upgradeLevel)
				self.boxLabel.text = text
				
				-- expert trophies
				if (i>=2 and i<=6 and upgradeLevel==1) then
					self.boxLabel.text = string.format(text, player:getNumShiftsDoneOnLevel(i-1, 2), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))
				elseif (i==7 and upgradeLevel==0) then
					self.boxLabel.text = string.format(text, player:getNumRoomsUpgraded(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))					
				elseif (i==8) then					
					if (upgradeLevel==0) then
						local marathon = ghf.getFloat("MARATHON")
						local travel = player.gameStats.travelProgress
						self.boxLabel.text = string.format(text, ghf.toNumberString({travel * marathon, 2}), ghf.toNumberString({marathon, 2}))
					elseif (upgradeLevel==1) then
						self.boxLabel.text = string.format(text, ghf.toNumberString({marathon, 2}))
					end
				elseif (i==11 and (upgradeLevel==0 or upgradeLevel==1)) then					
					self.boxLabel.text = string.format(text, player:getPresentsCount(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))				
				elseif (i==13 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player:getNumMinigamesPlayed(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))							
				elseif (i==14 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player:getNumGameHouseObjectsFound(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
				elseif (i==15 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player:getNumGuineaPigsFound(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
				elseif (i==16 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player.gameStats.firstAtWorkStation, player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
				elseif (i==17 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player:getNumLevelsHighestGrade(), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
				elseif (i==18 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player:getNumMinigamesAtLevel(5-1), player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
				elseif (i==19 and (upgradeLevel==0)) then					
					self.boxLabel.text = string.format(text, player.gameStats.patientsHealed, player.trophyManager:getGoal("TROPHY"..i, upgradeLevel))												
					
				end				
            end
        end
	end
end

function levelDesc:setTextBoxID(id)
	self.textBoxID = id
	self.boxLabel.textID = self.textBoxID
end

function levelDesc:setHeaderBoxID(id)
	self.headerBoxID = id
	self.boxLabel.textID = self.headerBoxID
end

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

	scene:getChild("BACK").onClick = function()
		ghf.setTransition("defaultTransition");
		local nextScene
		if (scene.comingFromAndReturnToShop) then
			nextScene = ghf.createScene("game")
			nextScene.directlyToShop = true
		else
			nextScene = ghf.createScene("selection")
		end
		ghf.gotoScene(nextScene)
	end

	self.boxLabel = scene:getChild("BOX_TEXT")
	self:setTextBoxID("TROPY_TEXT_DEFAULT")

	self.headerLabel = scene:getChild("BOX_HEADER")
	self:setHeaderBoxID("TROPY_HEADER_DEFAULT")

	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:onMouseMoved(0, 0)

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

	for i=1,30 do
		local t = level:getSpriteExt("TROPHY"..i)
		t.clickable = true
		t.drawOutline = false
		t.onClick = function()
			hero:addTask({class="WalkToTask", target=t})
		end
	end

	for _,v in ipairs(self.fish) do
		if (v) then
			self:newSwim(v)
			v.onClick = function()
				hero:addTask({class="WalkToTask", target=v})
				v.s = 0.5
				v.dir = ((v.x > (896 + 733)/2) and -1) or 1
				v.timer = 1000
			end
			v.clickable = true
			v.drawOutline = false
			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(2.1, 4), duration=d1, style="easeInOut"},
					{y=y+math.Random(2.1, 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
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
