local currentRooms = {}
DraftState = class(Group, function(o, backState, levelData)
	Group.init(o)
	o.backState = backState
	o.levelData = levelData
	o:setup()
end)
function DraftState:setup()
	local o = self
	bludG.camera.scroll.x, bludG.camera.scroll.y = 0, 0
	bludG.camera.scale = 1
	o.currentRooms = currentRooms

	-- top graphic height is 160 on ipad and 134 iphone retina

	-- should move this to some basic background layer
	local groundHeight, bottomGradT, bottomGradB = bludG.camera.h/3, 470, 338
	bottomGradT, bottomGradB = bottomGradT*r_scale/2, bottomGradB*r_scale/2

	local groundRatio = 365/1024
	local groundHeight = letterboxWidth*groundRatio
	local graphicRatio = 134/1024
	local graphicOffset = letterboxWidth*groundRatio

	local tc = player.currentWorld.top_color;
	local bc = player.currentWorld.bottom_color;
	local gc = player.currentWorld.draft_gradient;
	local topGrad = 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)))
	topGrad:setScrollFactor(Vec2(0,0))

	DraftState.placeRoomButtons(o)

	o.descriptionGroup = o:add(Group())
	local bottomGrad = o:add(GradientObject(0,bludG.camera.h*0.5,bludG.camera.w, bludG.camera.h*0.5-groundHeight, bludColor(0,0,0,0), bludColor(gc[1], gc[2], gc[3], 255)))
	bottomGrad:setScrollFactor(Vec2(0,0))

	o.ground = o:add(GradientObject(0,bludG.camera.h-groundHeight,bludG.camera.w, groundHeight, bludColor(0,0,0, 255), bludColor(0,0,0, 255)))
	o.ground:setScrollFactor(Vec2(0,0))

	local bottomGraphic = o:add(Object(0,graphicOffset,bludG.camera.w, bludG.camera.h))
	bottomGraphic.drawType = 'svg'
	bottomGraphic.svg = SvgLoader(svg_root .. "interface/draft_bottom_" .. player.currentWorld.id .. ipad .. ".svg", true, nil, true)
	bottomGraphic.svg:setScrollFactor(0,0)
	bottomGraphic.moveTween = Tweener:addTween(bottomGraphic.pos, {y=0})
	-- make the whole thing scrollable
	o.scroller = o:add(DragArea(0,0,bludG.camera.w, bludG.camera.h))
	o.scroller.onMove = function(da, x, y, id)
		if bludG.camera.tween then Tweener:removeTween(bludG.camera.tween) end
		local yDiff = y-da.lastPos[id].y
		bludG.camera.scroll.y = bludG.camera.scroll.y - yDiff
	end
	o.scroller.onPress = function(da,x,y,id)
		o:fingerRelease(da, x, y, id)
	end

	DraftState.addBottomButtons(o, o.backState)
	WorldSelectState.addBackButton(o, o.lastState)
end
function DraftState:buttonback_callback()
	mainState:changeState(self.backState)
	bludG.camera.scroll = Vec2(0,0)
	bludG.camera.scale = 1
end
function DraftState:addBottomButtons(lastState)
	local buttonH, buttonL, buttonR = 400*r_scale, 40*r_scale, 180*r_scale
	if 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()
		if self.levelData then
			player.achievements:useDraft()
			local ps = PlayState(self.levelData)
			-- double check to make sure that the player has actually changed the rooms from the original ones
			mainState:changeState(ps)
		end
	end
end

function DraftState:placeRoomButtons()
  self.currentRooms = self.levelData.rooms
  local topOffset = 0
  local buttonSize, buttonSpace, buttonPad, buttonLeft = 40, 47, 46, 0
  if ipad == '_ipad' then
    buttonLeft = 63
  end
  local textPadding = 30
  textPadding = textPadding*r_scale
  buttonSize, buttonSpace, buttonPad, buttonLeft = buttonSize*r_scale, buttonSpace*r_scale, buttonPad*r_scale, buttonLeft*r_scale*0.5

  local containersvg = SvgLoader(svg_root .. "interface/draft_selector" .. ipad ..".svg", true)
  local bodyMin = containersvg.id.body.minY
  local bodyW = containersvg.id.body.maxX-containersvg.id.body.minX
  local bodyX = containersvg.id.body.minX
  for i1,roomType in ipairs(roomTypes) do
    local container = self:add(Object(0,topOffset,10, 10))
    container.drawType = 'svg'
    container.svg = SvgLoader(svg_root .. "interface/draft_selector" .. ipad .. ".svg", true)
    -- attach the text area for the name of the room
    local titleTxt = self:add(StringObject(0,container.svg.id.title.minY+topOffset,roomType['title'], nil, 0.7))
    titleTxt:setColor(0,0,0)
    -- center the text on teh screen
    titleTxt:setPosition(bludG.camera.w/2-titleTxt.w/2, titleTxt.pos.y)
    -- for each of the rooms in the room type, add a button for it to the selector
    local descTop = (bodyMin+topOffset)
    for i2,room in ipairs(roomType) do
      local button = self:add(TypeButton(buttonPad+(i2-1)*buttonSpace+buttonLeft,buttonPad+topOffset,buttonSize,buttonSize, o, room))
      button:setScrollFactor(1, 1)
      button:afford(true)
      if not player.unlocks[room] then
        button:afford(false)
      else
        -- attach the actions for the buttons
        button.onPress = function()
          -- check to see if this titletext has a textBlock attached to it, if so, delete, and then buid a new block.
          if titleTxt.textBlock then self.descriptionGroup:remove(titleTxt.textBlock) end
          if roomDescriptions[room] then
            titleTxt.textBlock = self.descriptionGroup:add(MultilineString(textPadding/2, descTop/2, bludG.camera.w-textPadding*2, roomDescriptions[room], nil, 0.5))
            titleTxt.textBlock:center()
            titleTxt.textBlock:setColor(0,0,0)
          end
          -- should check to see if we are turning on or off
          if self.currentRooms[room] then
            self.currentRooms[room] = false
            button:setColor(20, 20, 20, 100)
          else
            -- first make sure that there are less than 5 rooms selected already
            if _(self.currentRooms):chain():values():reduce(0,function(memo, i) if i then memo = memo + 1 end return memo end):value() >= 5 then
            	mainState.particles:addRecycle(TextPop(button:center().x, button:center().y, "Max 5"))
              	return false
            end
            self.currentRooms[room] = true
            local bc = player.currentWorld.bottom_color;
            button:setColor(bc[1], bc[2], bc[3], 255)
          end
          -- if turning on, then make sure that only 4 things are selected already
          return false
        end
      end
      if self.currentRooms[room] then
        local bc = player.currentWorld.bottom_color;
        button:setColor(bc[1], bc[2], bc[3], 255)
      else
        button:setColor(20, 20, 20, 100)
      end
    end
    topOffset = topOffset + containersvg:getMaxY()
  end
  self.maxHeight = topOffset
end
function DraftState:fingerRelease(da, x, y, id)
	-- body
	if bludG.camera.scroll.y < 0 then
		bludG.camera:zoomTo(bludG.camera.scroll.x, 0, bludG.camera.scale)
	elseif bludG.camera.scroll.y+bludG.camera.h > self.maxHeight+60*r_scale+(bludG.camera.h-self.ground.pos.y) then
		bludG.camera:zoomTo(bludG.camera.scroll.x, self.maxHeight+60*r_scale-bludG.camera.h+(bludG.camera.h-self.ground.pos.y), bludG.camera.scale)		
	end

end