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

FacebookButtonYBase = 14;
FacebookButtonYGap = 44;

--------------------------------------------------------------------------
-- When exiting this dialog we're assumed to be returning to game play
--------------------------------------------------------------------------
function facebook_exit_share()
	FBPauseAudio(false);
	if (Facebook_Callback_ExitPause) then Facebook_Callback_ExitPause(); end;
	PopModal('fbchoices');
end;

local fb_button_y = FacebookButtonYBase;
local fb_buttoncount = 1; -- count the cancel button

local function FB_ConditionalButton(condition, button)
	if condition then 
		fb_button_y = fb_button_y + FacebookButtonYGap;
		fb_buttoncount = fb_buttoncount + 1;
		return button;
	end;
	
	-- button not used, do a null function
	return function() end;
end;


-- Diner Dash 4 may want context sensitive photo-button label.
function FB_GetLabelForPhoto()
	if (Facebook_Callback_GetLabelForPhoto) then return Facebook_Callback_GetLabelForPhoto(); end;
	return 'facebook_ingame_btn_photo';
end;

---------------------------------------------------------------
-- This is a menu dialog with 2, 3, or 4 buttons
--	Post Update
--	Post Photo
--	Post Screenshot
--	Cancel
--
-- Each button is optional at runtime; 
--    actually 'Post Update' is always available.
--    if lua function CreateFacebookImage doesn't exist then 'Post Photo' doesn't appear
--	  if lua function CrateScreenshot doesn't exist then 'Post Screenshot' doesn't appear
--
--  The background images are numbered by buttoncount.
--     _2  _3 and _4 
---------------------------------------------------------------
MakeDialog
{
	name = 'fbchoices',
	Bitmap
	{
		x=kCenter,
		y=kCenter,
		Window
		{
			name = 'above_divider',
			x = 2,
			y = 2,
			w = kMax - 2,
			h = 34,
			Text
			{
				font = FacebookTitleFont,
				x = 32,
				y = 8,
				w = kMax,
				h = kMax,
				label = 'facebook_ingame_dlg_title'
			},
		},
		Window
		{
			name = 'below_divider',
			x = 2,
			y = 37,
			h = kMax - 2,
			w = kMax - 2,

			SetStyle(FacebookBlueButtonStyle),
			FB_ConditionalButton( FB_CanDoRandomPost(), FBButton
			{
				name = 'facebook_ingame_btn_post',
				label = 'facebook_ingame_btn_post',
				y = fb_button_y,
				command= 
					function()
						FB_SetState("++FBRandomPostClicks");
						local ww = FB_Hideaway();
						if Facebook_PostStreamUI('userpost') then
							facebook_exit_share();
						else
							FB_Unhideaway(ww);
						end;
					end;
			}),

			SetStyle(FacebookBlueButtonStyle),
			FB_ConditionalButton( FB_CanDoScorePost(), FBButton
			{
				name = 'facebook_ingame_btn_postscore',
				label = 'facebook_ingame_btn_postscore',
				y = fb_button_y,
				command= 
					function()
						Facebook_Callback_RequestScoreData();
						
						FB_SetState("++FBScorePostClicks");
						local ww = FB_Hideaway();
						if Facebook_PostStreamUI('score', FB_gScoreOverrides) then
							facebook_exit_share();
							FB_gScoreOverrides = nil;
						else
							FB_Unhideaway(ww);
						end;
					end;
			}),
			
			FB_ConditionalButton( FB_CanDoPhotoPost(), FBButton
			{
				name = 'facebook_ingame_btn_photo',
				label = FB_GetLabelForPhoto(),
				y = fb_button_y,
				command=
					function()

						if not FB_IsVersionTwo() then FB_SetState("++FBAvatarPostClicks"); end;
					
						local closeit = true;
						
						local ww = FB_Hideaway();
						
						if ((not CreateFacebookImage) or (CreateFacebookImage() == false)) then
							facebook_ok_dialog('facebook_msg_nophoto_body', 'facebook_msg_nophoto_title'); 
							closeit = false; -- never closeit
						else
							if FB_IsVersionTwo() then FB_SetState("++FBAvatarPostClicks"); end;
							closeit = Facebook_PostStreamUI('photo'); 
						end
						
						if closeit then
							facebook_exit_share();
						else
							FB_Unhideaway(ww);
						end
					end

			}),
			
			FB_ConditionalButton( FB_CanDoScreenshotPost(), FBButton
			{
				name="facebook_ingame_btn_screenshot",
				label="facebook_ingame_btn_screenshot",
				y = fb_button_y,
				command=
					function()
						if not FB_IsVersionTwo() then FB_SetState("++FBScreenshotPostClicks"); end;
						
						local closeit = false;
						local ww = FB_Hideaway();
						if ((not CreateScreenshot) or (CreateScreenshot(0.75) == false)) then
							facebook_ok_dialog('facebook_msg_noscreenshot_body', 'facebook_msg_noscreenshot_title');
							closeit = false;
						else
							if FB_IsVersionTwo() then FB_SetState("++FBScreenshotPostClicks"); end;
							closeit = Facebook_PostStreamUI('screenshot'); 
						end
						if closeit then
							facebook_exit_share();
						else
							FB_Unhideaway(ww);
						end;
					end
			}),
			
			SetStyle(FacebookGreyButtonStyle),
			FBButton
			{
				name = "facebook_ingame_btn_cancel",
				label = "facebook_ingame_btn_cancel",
				y = fb_button_y,
				cancel = true,
				command = function()
					facebook_exit_share();
				end
			},
		},
		image="facebook/common/fb_share_bg_box_"..tostring(fb_buttoncount),
	}, -- bitmap
}; -- dialog

-- When this dialog is popped up, we're entering pause state, so we need to let the game pause itself.
if (Facebook_Callback_EnterPause) then Facebook_Callback_EnterPause(); end;

FBPauseAudio(true);
CommitQueuedSaves();

FB_SetState("++FBInGameFBConnectClicks");
