-- this could be loaded from somewhere eventually
LevelSelectState = class(Group, function(o, lastState)
	bludG.camera:cancelTween()
	bludG.camera.scroll = Vec2(0,0)
	bludG.camera.scale = 1
	Group.init(o)
	o:setup(lastState)
	CrabBossEnemyUnload()
end)

function LevelSelectState:addLevelButtons()
	local o = self
	local hHeight = self.ground.pos.y-self.hotel.sprite:getHeight()*self.targetScale
	o.levelTitle:setPosition(self.levelTitle.pos.x, hHeight-60*r_scale-80*r_scale)
	local levelData = player.currentWorld.levels
	local buttonW = 44*r_scale
	local totalWidth = buttonW*#levelData
	local left = bludG.camera.w/2-totalWidth/2
	o.levelButtons = o:add(Group())
	for i,v in ipairs(levelData) do
		local levelData = persistence.load(blud.bundle_root .. "/badHotel_/assets/levelData/" .. v .. ".lua");
		if player.unlocks[v] or player.allLevelsUnlocked then
			local l1 = o.levelButtons:add(LevelButton(left+buttonW*(i-1), 200, i))
			if player.highscores[levelData.filename] then l1:setComplete() end
			o:setBelow(l1, o.levelTitle, 10*r_scale)
			l1.levelData = levelData
			l1.onPress = function()
				o:chooseButton(l1)
			end
		else
			local l1 = o.levelButtons:add(LockButton(left+buttonW*(i-1), 200, i))
			o:setBelow(l1, o.levelTitle, 10*r_scale)
		end
	end
	-- add the highscore line below the buttons
	o.highscoreLine = o:add(StringObject(0, 0, "No highscore", nil, 0.6))
	o.highscoreLine:setColor(0, 0, 0, 255)
	o:setBelow(o.highscoreLine, o.levelButtons.members[1], 5*r_scale)
	o:centerObject(o.highscoreLine)
	local foundLevel = false
	if player.lastLevel then
		for i,v in ipairs(o.levelButtons.members) do
			if v.levelData and v.levelData.filename == player.lastLevel then
				o:chooseButton(v)
				foundLevel = true
				break;
			end
		end
		-- find the button that corrosponds to the last level selected
	end
	if not foundLevel and o.levelButtons.members[1] then
		o:chooseButton(o.levelButtons.members[1])
	end
end

function LevelSelectState:setup(lastState)
	local o = self

	local groundRatio = 365/768
	local groundHeight = letterboxWidth*groundRatio

	local tc = player.currentWorld.top_color;
	local bc = player.currentWorld.bottom_color;
	o:add(GradientObject(0,0,bludG.camera.w, bludG.camera.h-groundHeight, bludColor(tc[1],tc[2],tc[3], 255), bludColor(bc[1],bc[2],bc[3], 255)))
	o.ground = o:add(GradientObject(0,bludG.camera.h-groundHeight,bludG.camera.w, groundHeight+1, bludColor(0,0,0, 255), bludColor(0,0,0, 255)))
	-- WorldSelectState.addBorder(o, lastState)

	o.levelTitle = o:add(StringObject(0,25,"Level 1", nil, 0.6))
	o.levelTitle:setColor(0,0,0)
	o:centerObject(o.levelTitle)
	LevelSelectState.addBottomButtons(o, lastState)
	WorldSelectState.addBackButton(o, lastState)
	-- load in the hotel
	self:addHotel(lastState)
	self:addLevelButtons()
end

-- switch resolution and go to screen
function LevelSelectState:addHotel(lastState, scale)
	local scale = scale or 0.4
	scale = scale * r_scale * 0.5
	self.targetScale = scale
	local hotelSpr = sprites['tiles']['silhouette_' .. player.currentWorld.id .. '.png']
	local hWidth = hotelSpr:getWidth()*scale
	local hHeight = hotelSpr:getHeight()*scale
	if lastState and lastState.hotel then
		hWidth = lastState.hotel.w
		hHeight = lastState.hotel.h
	end
	-- add in the hotel
	local hotel = self:add(Object(bludG.camera.w/2-hWidth/2, self.ground.pos.y-hHeight, hWidth, hHeight))
	hotel.sprite = hotelSpr
	hotel.drawType = 'stretched'
	if lastState and lastState.hotel then
		-- tween to the proper position and target
		hWidth = hotelSpr:getWidth()*scale
		hHeight = hotelSpr:getHeight()*scale
		hotel.sizeTween = Tweener:addTween(hotel, {w=hWidth, h=hHeight})		
		hotel.posTween = Tweener:addTween(hotel.pos, {x=bludG.camera.w/2-hWidth/2, y=self.ground.pos.y-hHeight})	
	end
	self.hotel = hotel
	-- add the object that will hold the highscores above the hotel
end

function LevelSelectState:buttonback_callback()
	mainState:changeState(WorldSelectState(self))
end

function LevelSelectState:addBottomButtons(lastState)
	local buttonH, buttonL, buttonR = 400*r_scale, 40*r_scale, 180*r_scale
	if lastState and lastState.playButton then
		self.playButton = self:add(lastState.playButton)
	else
	
		local groundHeight = bludG.camera.h/3
		local pbw = 114
		local pby = bludG.camera.h-groundHeight/2-pbw/2
		self.playButton = self:add(FancyButton(bludG.camera.w/2-pbw/2,pby,"Play"))
		self.playButton:setScrollFactor(Vec2(0,0))
		self.playButton:setPosition(bludG.camera.w/2-self.playButton.svg:getMaxX()/2, self.ground.pos.y+self.ground.h/2-self.playButton.svg:getMaxY()/2)
	end
	self.playButton.onPress = function()
		mainState:changeState(PlayState(self.currentLevel.levelData))
	end
	self.playButtonY = self.playButton.pos.y+5*r_scale
	local bc = player.currentWorld.bottom_color;
	if scf:isSteamConnected() then
		self.leaderboardButton = self:add(FancyButton(self.playButton.pos.x-self.playButton.w/2, self.playButton.pos.y+self.playButton.h/2, "", "button_draft", 0.5, "button_score"))
		-- self.leaderboardButton.overlay.svg:scale(r_scale*0.5*r_scale, r_scale*0.5*r_scale)
		self.leaderboardButton:setPosition(bludG.camera.w/5-self.leaderboardButton.svg:getMaxX()/2, bludG.camera.h)
		self.leaderboardButton:centerOverlay()
		self.leaderboardButton.onPress = function()
			mainState:changeState(HighscoreState(self, self.currentLevel.levelData))
		end
		if gk and gk:isConnected() then
			WorldSelectState.show(self, self.leaderboardButton)
		end
		-- force the colors on the buttons
		self.leaderboardButton:setColor(bc[1], bc[2], bc[3])
	end
	self.playButton:setColor(bc[1], bc[2], bc[3])
end

function LevelSelectState:chooseButton(obj)
	for i,v in ipairs(self.levelButtons.members) do
		if v.setAlpha then
			v:setAlpha(0)
		end
	end
	if obj.setAlpha then
		obj:setAlpha(255)
		self.currentLevel = obj
		self.levelTitle:setValue(obj.levelData.name)
		-- if there is a highscore for this level, then set it
		if scf:isSteamConnected() then
			WorldSelectState.hide(self, self.leaderboardButton)
		end
		if player.highscores[obj.levelData.filename] then
			self.highscoreLine:setValue("High Score: " .. math.floor(player.highscores[obj.levelData.filename]))
			self:centerObject(self.highscoreLine)
		else
			self.highscoreLine:setValue("")
		end
		if obj.levelData.filename ~= "1_0" and scf:isSteamConnected() then
			WorldSelectState.show(self, self.leaderboardButton)
		end
		player.lastLevel = obj.levelData.filename
	end
	self:centerObject(self.levelTitle)
end

function LevelSelectState:centerObject(obj, centerObj)
	centerObj = centerObj or bludG.camera
	obj:setPosition(centerObj.pos.x+centerObj.w/2-obj.w/2, obj.pos.y)	
end
function LevelSelectState:setBelow(new, old, padding)
	new:setPosition(new.pos.x, old.pos.y+old.h+padding)
end
-- mainState = WrapperState(Group());
-- ps = LevelSelectState()
-- mainState:changeState(ps)

-- -- ps.particles:stop()
