require( "scripts/style" );

totalButtons=2; -- exclude main menu

kButtonAssetHeight=40;
kClientStartOffsetY = 38;
kClientHeight = 188;
kButtonSpacing = kClientHeight/totalButtons;
kButtonY = kClientStartOffsetY + (kButtonSpacing / 2) - (kButtonAssetHeight/2);	-- Three buttons means center 60 pixels above the center

-- When this dialog is popped up, we're entering pause state, so
-- let's just notify TGameState now.
SetState( kPause, true );

buttonResumePos=0;
buttonReplayPos=1;
buttonDiaryPos=1;
buttonTownPos=1;
buttonStudioPos=1;
buttonOptionsPos=1;
buttonQuitPos=2;

local showReplayButton=false;
if (gDialogTable.showReplayButton) then
	showReplayButton = gDialogTable.showReplayButton;
	if (showReplayButton) then
		totalButtons = totalButtons + 1;
		buttonDiaryPos = buttonDiaryPos + 1;
		buttonTownPos = buttonTownPos + 1;
		buttonStudioPos = buttonStudioPos + 1;
		buttonOptionsPos = buttonOptionsPos + 1;
		buttonQuitPos = buttonQuitPos + 1;
		
		kButtonSpacing = kClientHeight / totalButtons;
		kButtonY = kClientStartOffsetY + (kButtonSpacing / 2) - (kButtonAssetHeight/2);
	end
end

local showDiaryButton=false;
if (gDialogTable.showDiaryButton) then
	showDiaryButton = gDialogTable.showDiaryButton;
	if (showDiaryButton) then
		totalButtons = totalButtons + 1;
		buttonTownPos = buttonTownPos + 1;
		buttonStudioPos = buttonStudioPos + 1;
		buttonOptionsPos = buttonOptionsPos + 1;
		buttonQuitPos = buttonQuitPos + 1;
		
		kButtonSpacing = kClientHeight / totalButtons;
		kButtonY = kClientStartOffsetY + (kButtonSpacing / 2) - (kButtonAssetHeight/2);
	end
end

local showTownButton=false;
if (gDialogTable.showTownButton) then
	showTownButton = gDialogTable.showTownButton;
	if (showTownButton) then
		totalButtons = totalButtons + 1;
		buttonStudioPos = buttonStudioPos + 1;
		buttonOptionsPos = buttonOptionsPos + 1;
		buttonQuitPos = buttonQuitPos + 1;
		
		kButtonSpacing = kClientHeight / totalButtons;
		kButtonY = kClientStartOffsetY + (kButtonSpacing / 2) - (kButtonAssetHeight/2);
	end
end

local showStudioButton=false;
if (gDialogTable.showStudioButton) then
	showStudioButton = gDialogTable.showStudioButton;
	if (showStudioButton) then
		totalButtons = totalButtons + 1;
		buttonOptionsPos = buttonOptionsPos + 1;
		buttonQuitPos = buttonQuitPos + 1;
		
		kButtonSpacing = kClientHeight / totalButtons;
		kButtonY = kClientStartOffsetY + (kButtonSpacing / 2) - (kButtonAssetHeight/2);
	end
end

function ShowReplayButton()
	t=
	{
		Button
		{
			x=kCenter,
			y=kButtonY + (kButtonSpacing * buttonReplayPos),
			name="replayButton",
			label="menu_restart",
			close = true,	-- Just close this window
			cancel=true,
			command=
				function()
					SendGameMessage( "replay" );
					SetState( kPause, false );
				end,
			sound = kButtonConfirmSound,
		},
		
		Button
		{
			x=kCenter,
			y=kButtonY + (kButtonSpacing * buttonDiaryPos),
			name="backdiaryButton",
			label="menu_diary",
			close = true,	-- Just close this window
			cancel=true,
			command=
				function()
					SendGameMessage( "backtodiary" );
					SetState( kPause, false );
				end,
			sound = kButtonConfirmSound,
		},
		
		Button
		{
			x=kCenter,
			y=kButtonY + (kButtonSpacing * buttonTownPos),
			name="backtownButton",
			label="menu_town",
			close = true,	-- Just close this window
			cancel=true,
			command=
				function()
					SendGameMessage( "backtotown" );
					SetState( kPause, false );
				end,
			sound = kButtonConfirmSound,
		},
		
		Button
		{
			x=kCenter,
			y=kButtonY + (kButtonSpacing * buttonStudioPos),
			name="backstudioButton",
			label="menu_studio",
			close = true,	-- Just close this window
			cancel=true,
			command=
				function()
					SendGameMessage( "backtostudio" );
					SetState( kPause, false );
				end,
			sound = kButtonConfirmSound,
		}
	}
	
	return Group(t);
end

MakeDialog
{
	-- Name the modal dialog
	name="pausemenu",
	Bitmap
	{
		image="backgrounds/dialog_small",
		x=kCenter,
		y=kCenter,

		SetStyle(ButtonStyle),
		Button
		{
			x=kCenter,
			y=kButtonY,
			name="resumebutton",
			label="resume",
			close = true,	-- Just close this window
			cancel=true,
			command=
				function()
					SendGameMessage( "unpause" );
	                SetState( kPause, false );
				end,
			sound = kButtonConfirmSound,
		},

		Button
		{
			x=kCenter,
			y=kButtonY + (kButtonSpacing * buttonOptionsPos),
			name="optionsbutton",
			label="optionsbutton",
			command=
				function()
					-- Pop up an options screen
					DoModal("scripts/options.lua");
				end
		},

		SetStyle(HelpButtonStyle),
		Button
		{
			x=154,
			y=232,
			--cancel=true,
			command=
				function()
					DoModal('scripts/help.lua');
					--SendGameMessage( "unpause" ); -- remove this once the Help window is hooked into game
					SetState( kPause, false );
				end
		},

		SetStyle(MainMenuButtonStyle),
		Button
		{
			x=194,
			y=232,
			command=
				function()
					PopModal('pausemenu');
					-- DoModal('scripts/gameover.lua');
					-- Our game processes this message--it's not
					-- handled by Playground
                    SendGameMessage( "quit" );
	                SetState( kPause, false );
				end
		},
		
		SetStyle(ButtonStyle),
		ShowReplayButton();
	},
}

SetWindowVisible('replayButton', showReplayButton);
SetWindowVisible('backdiaryButton', showDiaryButton);
SetWindowVisible('backtownButton', showTownButton);
SetWindowVisible('backstudioButton', showStudioButton);