--[[
	15 Jonbro 1415
	16 bmhb 1412
	17 haowan 1392
	18 Allbrotnar [Javelaxe] 1280
	19 Bezzy 1108
	20 bhallbhall 1107
	21 crassanova 1009
	22 Dom 860
	23 Dovahkiin 820
	24 HexagonalBolts 10
	25 ido.yehieli 1
]]
fakeScores = {
	{['rank'] = 15, ['username']='jonbro', ['score']=1415},
	{['rank'] = 16, ['username']='bmhb', ['score']=1415},
	{['rank'] = 17, ['username']='haowan', ['score']=1415},
	{['rank'] = 18, ['username']='Allbrotnar [Javelaxe]', ['score']=1415},
	{['rank'] = 19, ['username']='Bezzy', ['score']=1415},
	{['rank'] = 20, ['username']='bhallbhall', ['score']=1415},
	{['rank'] = 21, ['username']='crassanova', ['score']=1415},
	{['rank'] = 22, ['username']='Dom', ['score']=1415},
	{['rank'] = 23, ['username']='Dovahkiin', ['score']=1415},
	{['rank'] = 24, ['username']='HexagonalBolts', ['score']=1415},
	{['rank'] = 25, ['username']='ido.yehieli', ['score']=1415},
	{['rank'] = 15, ['username']='jonbro', ['score']=1415},
	{['rank'] = 16, ['username']='bmhb', ['score']=1415},
	{['rank'] = 17, ['username']='haowan', ['score']=1415},
	{['rank'] = 18, ['username']='Allbrotnar [Javelaxe]', ['score']=1415},
	{['rank'] = 19, ['username']='Bezzy', ['score']=1415},
	{['rank'] = 20, ['username']='bhallbhall', ['score']=1415},
	{['rank'] = 21, ['username']='crassanova', ['score']=1415},
	{['rank'] = 22, ['username']='Dom', ['score']=1415},
	{['rank'] = 23, ['username']='Dovahkiin', ['score']=1415},
	{['rank'] = 24, ['username']='HexagonalBolts', ['score']=1415},
	{['rank'] = 25, ['username']='ido.yehieli', ['score']=1415},
}
local modeTypes = {"Global", "My", "Friends"}

HighscoreState = class(Group, function(o, lastState, levelData)
	Group.init(o)
	o.levelData = levelData
	o.lastState = lastState
	o:setup()
end)
function HighscoreState:setup()
	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.currentMode = 0;

	o.levelTitle = o:add(StringObject(0,25,self.levelData.name .. " " ..modeTypes[self.currentMode+1] .. " highscores", nil, 0.4))
	o.levelTitle:setColor(0,0,0)
	LevelSelectState.centerObject(self,o.levelTitle)
	WorldSelectState.addBackButton(o)
	o.currentOffset = 1;
	-- self:loadedScores(fakeScores)
	-- load in the highscores for this level
	-- should just build a fake list to display on screen and test out the design
	self:placeButtons();
	self:runRequest();
end
function HighscoreState:runRequest()
	if self.outputBox then
		self.outputBox:clear()
	else
		self.outputBox = self:add(Group())
	end
	local loadingText = self.outputBox:add(StringObject(0,100*r_scale,"LOADING", nil, 0.4))
	loadingText:setColor(0,0,0)
	LevelSelectState.centerObject(self,loadingText)

	local sl = SteamLeaderboards();
	sl:getScores(player.lastLevel, self.currentMode, self.currentOffset, function(scoreTable)
		self:loadedScores(scoreTable);
	end)
end
function HighscoreState:loadedScores(scoreTable)
	if self.outputBox then
		self.outputBox:clear()
	else
		self.outputBox = self:add(Group())
	end
	if #scoreTable == 0 then
		local loadingText = self.outputBox:add(StringObject(0,100*r_scale,"No Scores Found", nil, 0.4))
		loadingText:setColor(0,0,0)
		LevelSelectState.centerObject(self,loadingText)
	end
	local currentScore = 0;
	local currentColumn = 0;
	-- show and hide the prev / next buttons based on what is currently showing, and in the tables
	if self.currentOffset > 1 then
		self:show(self.pb)
	else
		self:hide(self.pb)
	end
	if #scoreTable > 10 then
		self:show(self.nb)
	else
		self:hide(self.nb)
	end
	for i,v in ipairs(scoreTable) do
		local ypos = currentScore*15*r_scale+70*r_scale
		local xPos = currentColumn*250*r_scale+25*r_scale
		for k,val in pairs(v) do
			if k == 'rank' then
				local placeStr = self.outputBox:add(StringObject(xPos,ypos, val, nil, 0.4))
				placeStr:setColor(0,0,0,100)
			elseif k == 'username' then
				local namestr = self.outputBox:add(StringObject(xPos+30*r_scale,ypos, val, nil, 0.4))
				namestr:setColor(0,0,0)
			else
				local scorestr = self.outputBox:add(StringObject(xPos+180*r_scale,ypos, val, nil, 0.4))
				scorestr:setColor(0,0,0)
			end
		end
		currentScore = currentScore + 1
		if currentScore > 10 then
			currentScore = 0;
			currentColumn = currentColumn+1;
		end
	end
end
-- data types
-- 0 : global
-- 1 : global centered on player
-- 2 : friends
function HighscoreState:placeButtons()
	local cworld = player.currentWorld
	-- get the distance between the border and the center of the screen (subtracting the hotel width)
	local leftSide = (bludG.camera.w/2)/2
	local pb = self:add(FancyButton(leftSide, bludG.camera.h/2, "", "button_back"))
	pb.pos.x = pb.pos.x - pb.svg:getMaxX()/2
	pb.w = pb.svg:getMaxX()
	self.pb = pb
	local nb = self:add(FancyButton(bludG.camera.w-115, bludG.camera.h/2, "", "button_back"))
	nb.svg:translate(-nb.svg:getMaxX(), 0)
	nb.svg:scale(-1, 1)
	nb.svg:storeTransform()
	pb.onPress = function()
		-- do another request for data and store it in the cache
		self.currentOffset = self.currentOffset-10;
		self:runRequest();
		self:hide(self.pb)
		self:hide(self.nb)
	end
	nb.onPress = function()
		-- do another request for data and store it in the cache
		self.currentOffset = self.currentOffset+10;
		self:runRequest();
		self:hide(self.pb)
		self:hide(self.nb)
	end
	self.nb = nb
	self.pb = pb
	pb:setPosition(bludG.camera.w/5-pb.w/2, bludG.camera.h)
	nb:setPosition(bludG.camera.w/5*4-pb.w/2, bludG.camera.h)
	self.settingsButton = self:add(FancyButton(bludG.camera.w/2-114/2, 600*r_scale*0.5, "Change", "button_draft", 0.35))
	self.settingsButton:setScrollFactor(Vec2(1,1))
	self.settingsButton.onPress = function()
		-- change mode and text
		self.currentMode = (self.currentMode+1)%3
		-- self.settingsButton.string:setValue(modeTypes[self.currentMode+1])
		self:runRequest()
		self.levelTitle:setValue(self.levelData.name .. " " ..modeTypes[self.currentMode+1] .. " highscores")
		LevelSelectState.centerObject(self,self.levelTitle)

	end
	self.settingsButton:setColor(cworld.bottom_color[1], cworld.bottom_color[2], cworld.bottom_color[3])
	self.nb:setColor(cworld.bottom_color[1], cworld.bottom_color[2], cworld.bottom_color[3])
	self.pb:setColor(cworld.bottom_color[1], cworld.bottom_color[2], cworld.bottom_color[3])
end
function HighscoreState:show(b)
	if b.postween then Tweener:removeTween(b.postween) end
	b.postween = Tweener:addTween(b.pos, {y=600*r_scale*0.5})
end
function HighscoreState:hide(b)
	if b.postween then Tweener:removeTween(b.postween) end
	b.postween = Tweener:addTween(b.pos, {y=bludG.camera.h+10*r_scale})
end
function HighscoreState:buttonback_callback()
	mainState:changeState(self.lastState)
	bludG.camera.scroll = Vec2(0,0)
	bludG.camera.scale = 1
end
