require( 'facebook/common/style.lua' );
dofile( "facebook/common/facebook_functions.lua" );

---------------------------
--- FRIEND PICKER UI ------
---------------------------

-- fonts
facebook_font = 'facebook/common/helvetica.mvec';
facebook_fontcolor = Color(0,0,0,255);

facebook_friendpicker_background = 'facebook/common/friendpicker_bkgd';
facebook_yesno_button = {"buttons/buttonup","buttons/buttondown","buttons/buttonrollover"};

facebook_checkbox_button = {"buttons/checkup", "buttons/checkdown", "buttons/checkover","buttons/checkdownover"};

facebook_friendpicker_backgroundstyle =
{
	image = facebook_friendpicker_background,
	x = kCenter,
	y = kCenter,
};

facebook_friendpicker_titlestyle =
{
	font = {facebook_font, 15, Color(255,255,255,255)},
	flags = kVAlignTop + kHAlignCenter,
	x=25,y=25,w=kMax-25,h=kMax-60
};


facebook_friendpicker_namestyle =
{
	font = {facebook_font, 8, Color(255,255,255,255)},
	flags = kVAlignTop + kHAlignCenter,
	x=30,y=50,w=70,h=50
};

facebook_friendpicker_imagestyle =
{
	x=40,y=0,w=50,h=50
};

facebook_friendpicker_checkboxstyle =
{
	type = kToggle,
	sound = FacebookButtonClickSound,
	rolloversound = FacebookButtonRolloverSound,
	graphics = 	facebook_checkbox_button,
	x=0,
	scale = 0.7,
	y=5,
};

facebook_friendpicker_no_buttonstyle = 
{
	font = {facebook_font, 12, facebook_fontcolor},
	sound = FacebookButtonClickSound,
	rolloversound = FacebookButtonRolloverSound,
	type= kPush,
	graphics = facebook_yesno_button,
    x = kCenter+80,
    y = kMax-50,
};

facebook_friendpicker_continue_buttonstyle = 
{
	font = {facebook_font, 12, facebook_fontcolor},
	sound = FacebookButtonClickSound,
	rolloversound = FacebookButtonRolloverSound,
	type= kPush,
	graphics = facebook_yesno_button,
    x = kCenter-80,
    y = kMax-50,
};

facebook_friendpicker_scrollpane_style = 
{
	x = 36,
    y = 61,
    fit = true,
};

facebook_friendpicker_container_style = 
{
	x = 0,
    y = 0,
    w = 304,
    h = 182,
    startX = 0,
	startY = 0,
	numPerRow = 3,
	xSpacing = 100,
	ySpacing = 100,
};

facebook_friendpicker_slider_style = 
{
	
	railtop = "slider/sliderrailtop",
	railmid = "slider/sliderrailmid",
	railbot = "slider/sliderrailbot",
	sliderimage = "slider/sliderknob",
	sliderrollimage = "slider/sliderknobover",
	x = 300,
	y = 0,
	h = 180,
};


local title = gDialogTable.title;
local requestid = gDialogTable.requestid;
local onlyOne = gDialogTable.onlyOne;
if (onlyOne == nil) then
	onlyOne = false;
end;
onlyOne = true;

local gLastButton = nil;

function MakeFriendChoices()
	friends = {};
	numFriends = Facebook_FriendPicker_NumFriends();

	startX = GetTag(facebook_friendpicker_container_style, 'startX');
	startY = GetTag(facebook_friendpicker_container_style, 'startY');
	numPerRow = GetTag(facebook_friendpicker_container_style, 'numPerRow');
	xSpacing = GetTag(facebook_friendpicker_container_style, 'xSpacing');
	ySpacing = GetTag(facebook_friendpicker_container_style, 'ySpacing');
	local row = 0;
	local col = 0;
	for i=1,numFriends do
		local buttonNum = i;
		table.insert(friends,
			Window
			{
				x = startX + (col * xSpacing);
				y = startY + (row * ySpacing);
				fit = true,
				SetStyle(facebook_friendpicker_namestyle),
				
				Text
				{
					name = 'name',
					label = '#'..Facebook_FriendPicker_GetField(i-1,'name');
				};
				SetStyle(facebook_friendpicker_imagestyle),
				FBUI_Image
				{
					url = Facebook_FriendPicker_GetField(i-1,'pic_small');
				};
				SetStyle(facebook_friendpicker_checkboxstyle),
				
				FBButton
				{
					name = 'friendpick_'..i,
					command = 
						function()
							if (onlyOne and gLastButton ~= nil) then
								SetButtonToggleState(gLastButton, false);
							end	
							gLastButton = 'friendpick_'..buttonNum;
							EnableWindow('friendpicker_continue', true);
						end	
				};
				
			}
		);
		col = col + 1;
		if (col >= numPerRow) then
			row = row + 1;
			col = 0;
		end
	end
	
	MakeDialog
	{
		Group(friends);
	}
end

MakeDialog
{
	name='facebook_friendpicker', 
	SetStyle(facebook_friendpicker_backgroundstyle);
    Bitmap
    {
		FBUI_FriendPicker
		{
			x = 0, y = 0,
			w = kMax, h = kMax,
			requestid = requestid,
			scrollpane = 'scrollpane',
			container = 'container',
			
			SetStyle(facebook_friendpicker_titlestyle),
			Text
			{
				label= title
			};
			
			SetStyle(facebook_friendpicker_scrollpane_style),
			FBUI_ScrollPane
			{
				name = 'scrollpane',
				scrollbar = 'slotscrollbar',
				window = 'fixedwindow',
				scroller = 'container',
				
				SetStyle(facebook_friendpicker_container_style),
				Window
				{
					name = 'fixedwindow',
					Window
					{
						name = 'container',
						x = 0, y = 0, w = 0, h = 0,
						-- Things get filled in here inside of FBUI_FriendPicker::PostChildrenInit()
					};
					
				};
				
				
				
				SetStyle(facebook_friendpicker_slider_style);
				Slider
				{
					name="slotscrollbar",
				};
			};
			
			
			SetStyle(facebook_friendpicker_continue_buttonstyle),
			FBButton
			{
				name = 'friendpicker_continue',
				command=
					function() 
						
						-- Figure out which friend is checked
						local numFriends = Facebook_FriendPicker_NumFriends();
						for i=1,numFriends do
							if (GetButtonToggleState('friendpick_'..i) == true) then
								gReturnValue = 
									{Facebook_FriendPicker_GetField(i-1,'uid'), Facebook_FriendPicker_GetField(i-1,'name')};
							end
						end
						PopModal('facebook_friendpicker');
						
						
					end;
				label =	'facebook_btn_continue',
			},
			
			SetStyle(facebook_friendpicker_no_buttonstyle),
			FBButton
			{
				command=
					function() 
						PopModal('facebook_friendpicker');
					end;
				label =	'facebook_btn_cancel',
			},
        },
    },

} -- MakeDialog

EnableWindow('friendpicker_continue', false);

