-- buttons restartbutton levelbutton closebutton

PauseState = class(Group, function(o, substate)
	Group.init(o)
	o:setup(substate)
end)
function PauseState:setup(substate)
	local o = self
	o.substate = substate
	o.bg =  o:add(Object(0,0,bludG.camera.w, bludG.camera.h))
	o.bg.svg =  SvgLoader(svg_root .. "interface/pause_menu_" .. player.currentWorld.id .. "_bg.svg", false)
	o.bg.svg:scale(bludG.camera.w/o.bg.svg:getMaxX(), bludG.camera.h/o.bg.svg:getMaxY())
	o.bg.drawType = 'svg'
	o.bg.svg:setScrollFactor(0,0)

	o.interface = o:add(Object(0,0,bludG.camera.w, bludG.camera.h))
	o.interface.svg = pauseSvg
	o.iscale = bludG.camera.h/960
	o.interface.svg:scale(o.iscale, o.iscale)
	o.interface:setPosition(bludG.camera.w/2-o.interface.svg:getMaxX()/2, 0)
	o.interface.drawType = 'svg'
	o.interface.svg:setScrollFactor(0,0)

	-- add the buttons for pause, etc
	if pauseSvgData then
		-- should move this into the svg loader
		for key, rbd in pairs(pauseSvgData) do
			local button = o:add(CameraButton(rbd.minX*o.iscale+o.interface.pos.x, rbd.minY*o.iscale+o.interface.pos.y, (rbd.maxX-rbd.minX)*o.iscale, (rbd.maxY-rbd.minY)*o.iscale))
			button.scrollFactor = Vec2(0,0)
			button.onPress = function()
				print(key .. "_callback")
				if o[key .. "_callback"] then o[key .. "_callback"](o) end
			end
			button:setColor(0,0,0,0)
		end
	end
	-- o:loadNewSettings()
end
function PauseState:onLoad()
	pd:startList("fromOF")
	pd:addSymbol("levelPause")
	pd:addFloat(1)
	pd:finish("fromOF")
	-- attach the text
	if not self.resumeText then
		self:addText('resume')
		self:addText('replay')
		self:addText('menu')
	end
end
function PauseState:addText(id)
	if pauseSvgData[id] then
		local resumeCenter = Vec2(pauseSvgData[id].minX, pauseSvgData[id].minY)
		self[id .. "Text"] = self:add(StringObject(0,0,id, nil, 0.5))
		self[id .. "Text"].scrollFactor = Vec2(0,0)
		self[id .. "Text"]:setColor(0,0,0)
		self[id .. "Text"]:setPosition(resumeCenter.x*self.iscale+self.interface.pos.x, resumeCenter.y*self.iscale+self.interface.pos.y)
	else
		print('pause screen ' .. id .. ' not found')
	end
end
function PauseState:draw()
	self.substate:draw()
	Group.draw(self)
end
function PauseState:closebutton_callback()
	pd:startList("fromOF")
	pd:addSymbol("unPause")
	pd:addFloat(1)
	pd:finish("fromOF")
	mainState:changeState(self.substate)
end
function PauseState:levelselectbutton_callback()
	pd:startList("fromOF")
	pd:addSymbol("levelStop")
	pd:addFloat(1)
	pd:finish("fromOF")
	mainState:changeState(LevelSelectState())
end
function PauseState:replaybutton_callback()
	pd:startList("fromOF")
	pd:addSymbol("levelStop")
	pd:addFloat(1)
	pd:finish("fromOF")
	-- analytics:logEvent("restart_pause", {['filename']=self.substate.waveData.filename, ['restart_time']=self.substate.waveController.time})

	mainState:changeState(PlayState(self.substate.waveData))
end

function PauseState:loadNewSettings()
	local async2 = bludAsycCurl()
	local self = self
	async2:process([[
		require("cURL")
		c = cURL.easy_init()
		c:setopt_url("http://jonathanbrodsky.com/bhsettings/index.php")
		local retVal = ""
		c:perform({writefunction = function(str) 
			retVal = retVal .. str
		end})
		return retVal
	]], function(otherPlayer)
		for line in otherPlayer:gmatch("[^\r\n]+") do
			local parsed = ParseCSVLine(line)
			if parsed[1] and parsed[2] then
				if tonumber(parsed[2]) ~= nil then
					game_settings[parsed[1]] = tonumber(parsed[2])
				else
					game_settings[parsed[1]] = parsed[2]
				end
			end
		end
		local text = self:add(StringObject(20,20,"newSettingsLoaded"))
		text:setColor(200, 0, 0)
		text:setScrollFactor(Vec2(0,0))
	end)
end


function ParseCSVLine (line,sep) 
	local res = {}
	local pos = 1
	sep = sep or ','
	while true do 
		local c = string.sub(line,pos,pos)
		if (c == "") then break end
		if (c == '"') then
			-- quoted value (ignore separator within)
			local txt = ""
			repeat
				local startp,endp = string.find(line,'^%b""',pos)
				txt = txt..string.sub(line,startp+1,endp-1)
				pos = endp + 1
				c = string.sub(line,pos,pos) 
				if (c == '"') then txt = txt..'"' end 
				-- check first char AFTER quoted string, if it is another
				-- quoted string without separator, then append it
				-- this is the way to "escape" the quote char in a quote. example:
				--   value1,"blub""blip""boing",value3  will result in blub"blip"boing  for the middle
			until (c ~= '"')
			table.insert(res,txt)
			assert(c == sep or c == "")
			pos = pos + 1
		else	
			-- no quotes used, just look for the first separator
			local startp,endp = string.find(line,sep,pos)
			if (startp) then 
				table.insert(res,string.sub(line,pos,startp-1))
				pos = endp + 1
			else
				-- no separator found -> use rest of string and terminate
				table.insert(res,string.sub(line,pos))
				break
			end 
		end
	end
	return res
end
