-- an object to do modal popups

ModalPopup = class(Group, function(o, bodyText, confirmCallback, _type, cancelCallback)
	Group.init(o);
	local size = Vec2(610, 810)
	size:mult(0.5 * r_scale)
	local halfTextWidth = size.x/2;
	local xpos = math.floor(bludG.screen_rect.w/2-halfTextWidth)
	local ypos = math.floor(bludG.screen_rect.h/2-size.y/2)

	if _type == "ok" then
		o.yesButton = o:add(Button(bludG.screen_rect.w/2-sprites["button_ok.png"]:getWidth()/2, ypos-sprites["button_ok.png"]:getHeight()-40, sprites["button_ok.png"]:getWidth(), sprites["button_ok.png"]:getHeight(), sprites["button_ok.png"]));
		o.yesButton.onPress = function()
			mainState:hideModal();
			-- don't bubble
			return false
		end
		o.yesButton.layer = 4
		o.yesButton.scrollFactor = Vec2(0,0)
		o.yesButton.noBubble = true
	else
		-- add some buttons for yes and no
		o.noButton = o:add(Button(math.floor(bludG.screen_rect.w/2-halfTextWidth/2-sprites["button_cancel.png"]:getWidth()/2), math.floor(ypos+size.y-sprites["button_cancel.png"]:getHeight()-40), sprites["button_cancel.png"]:getWidth(), sprites["button_cancel.png"]:getHeight(), sprites["button_cancel.png"]));
		o.noButton.layer = 4
		o.noButton.scrollFactor = Vec2(0,0)
		o.noButton.onPress = function()
			mainState:hideModal();
			if cancelCallback then
				return cancelCallback()
			end
		end
		o.noButton.noBubble = true
		o.yesButton = o:add(Button(math.floor(bludG.screen_rect.w/2+halfTextWidth/2-sprites["button_buy.png"]:getWidth()/2),math.floor(ypos+size.y-sprites["button_cancel.png"]:getHeight()-40), sprites["button_buy.png"]:getWidth(), sprites["button_buy.png"]:getHeight(), sprites["button_buy.png"]));
		o.yesButton.layer = 4
		o.yesButton.scrollFactor = Vec2(0,0)
		if confirmCallback then
			o.yesButton.onPress = function()
				return confirmCallback()
			end
		end
		o.yesButton.noBubble = true
	end
	o.bodyText = bodyText
	-- call draw once to update the positions of everything
	o:draw()
end)

function ModalPopup:draw()
	local size = Vec2(610, 810)
	local padding = 80
	size:mult(0.5* r_scale); padding = padding* r_scale / 2
	local width = size.x

	width = width* r_scale/2; padding = padding* r_scale/2
	-- draw a black out behind everything, waiting on width height controls for the sprite lib
	sheet:addCornerTile(sprites["white.png"], 0,0, bludG.camera.w,0, bludG.camera.w,bludG.camera.h, 0, bludG.camera.h, 4, 0,0,0,100)
	-- todo: move this into the creator
	self.textHeight = tb:getHeight(self.bodyText, size.x-padding, sfontSize*2, true)
	self.textHeight = self.textHeight+padding*4
	-- draw the text box
	local xpos = math.floor(bludG.screen_rect.w/2-size.x/2)
	local ypos = math.floor(bludG.screen_rect.h/2-size.y/2)
	local r = RoundedRect(xpos, ypos, size.x, self.textHeight)
	r.scrollFactor = Vec2(0,0)
	r:setColor(224,222,215)
	r.layer = 4
	r:draw()

	tb:addText(self.bodyText, xpos+40* r_scale/2, ypos+80* r_scale/2, true, size.x-80* r_scale/2, 50* r_scale/2);

	-- move the yes and no buttons up
	self.yesButton.pos.y = ypos+self.textHeight-padding/2-self.yesButton.sprite:getHeight()
	if self.noButton then
		self.noButton.pos.y = ypos+self.textHeight-padding/2-self.noButton.sprite:getHeight()
	end
	-- draw the yes no buttons
	Group.draw(self)
end
function ModalPopup:touchDown(x, y, id)
	Group.touchDown(self, x, y, id)
	return false
end
function ModalPopup:touchMoved(x, y, id)
	Group.touchMoved(self, x, y, id)
	return false
end
function ModalPopup:touchUp(x, y, id)
	Group.touchUp(self, x, y, id)
	return false
end
