--
-- video options menu page
--
----------------------------------------------------------------------------------------------

UI.PageOptionsVideo =
{
	GUI =
	{
		BackStatic=
		{
		classname = "static",
		left = 100, top = 140,
		width = 600, height = 315,
		zorder = -1,
		color = "0 0 0 96",
		bordersize = 1,
		bordercolor = UI.szBorderColor,
		},	
		
		sep_v =
		{
			skin = UI.skins.MenuBorder,

			left = 540, top = 141,
			width = 220, height = 275,
			bordersides = "l",

			zorder = -50,
		},

		sep_h0 =
		{
			skin = UI.skins.MenuBorder,

			left = 540, top = 261,
			width = 240, height = 123,
			bordersides = "t",

			zorder = -50,
		},

		sep_h1 =
		{
			skin = UI.skins.MenuBorder,

			left = 200, top = 415,
			width = 580, height = 243,
			bordersides = "t",

			zorder = -50,
		},

		-- definition of "Apply" button (applies all changes of video options undertaken by user)
		widget_apply =
		{

			tabstop = 11,

			skin = UI.skins.BottomMenuButton,
			top = 460,
			left = 310,

			text = Localize( "Apply" ),

			OnCommand = function( sender )
				UI.PageOptionsVideo.GUI:UpdateGlobals();
			end,
		},

		widget_reset =
		{
			skin = UI.skins.BottomMenuButton,
			left = 801,

			tabstop = 12,

			text = Localize("RestoreDefaults"),

			OnCommand = function( sender )
				UI.YesNoBox( Localize( "ResetToDefault" ), Localize( "GenericAreYouSure" ), UI.PageOptionsVideo.ResetToDefaults );
			end,
		},

		-- definition of "Renderer" combo box (allows user to change render type)
		widget_renderer_text =
		{
			skin = UI.skins.Label,
			left = 240, top = 164,
			width = 142,

			text = Localize( "Renderer" ),
		},

		widget_renderer =
		{
			skin = UI.skins.ComboBox,
			left = 410, top = 166,
			width = 150,
--			height = 26,		
			
			tabstop = 1,

			vscrollbar =
			{
				skin = UI.skins.VScrollBar,
			},

			-- data and code specific for "Renderer" combo box (separated in user table)
			user =
			{
				-- data
				initialSelectionIndex = 0,

				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,
			},
		},

		-- definition of "Resolution" combo box (allows user to change screen resolution)
		widget_resolution_text =
		{
			skin = UI.skins.Label,
			left = 240, top = 204,
			width = 142,

			text = Localize( "Resolution" ),
		},

		widget_resolution =
		{
			skin = UI.skins.ComboBox,
			left = 410, top = 206,
			width = 150,			

			tabstop = 2,

			vscrollbar =
			{
				skin = UI.skins.VScrollBar,
			},

			-- data and code specific for "Resolution" combo box (separated in user table)
			user =
			{
				-- data
				lScreenResolutionAndBpp = nil,

				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,
			},
		},

		-- definition of "Anti Aliasing" combo box (allows user to change FSAA)
		widget_fsaa_text =
		{
			skin = UI.skins.Label,
			left = 240, top = 244,
			width = 142,

			text = Localize( "FSAA" ),
		},

		widget_fsaa =
		{
			skin = UI.skins.ComboBox,
			left = 410, top = 246,
			width = 150,
			
			tabstop = 3,

			vscrollbar =
			{
				skin = UI.skins.VScrollBar,
			},

			-- data and code specific for "Anti Aliasing" combo box (separated in user table)
			user =
			{
				-- data
				initialSelectionIndex = 0,

				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,
			},
		},

		-- definition of "Brightness" combo box (allows user to adjust brightness)
		widget_brightness_text =
		{
			skin = UI.skins.Label,
			left = 240, top = 284,
			width = 142,

			text = Localize( "Brightness" ),
		},

		widget_brightness =
		{
			skin = UI.skins.HScrollBar,

			left = 410, top = 286,
			width = 150, height = 26,

			tabstop = 4,

			OnChanged = function( sender )
				UI.PageOptionsVideo.GUI.widget_brightness.user.OnChanged();
			end,

			-- code specific for "Brightness" combo box (separated in user table)
			user =
			{
				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,

				OnChanged = nil,
			},
		},


		-- definition of "Full Screen" check box (allows user to toggle between full screen and windowed mode)
		widget_fullscreen_text =
		{
			skin = UI.skins.Label,
			left = 240, top = 324,--164,
			width = 142,

			text = Localize( "Fullscreen" ),
		},

		widget_fullscreen =
		{
			skin = UI.skins.CheckBox,
			left = 410,	top = 326,--166,

			tabstop = 7,

			-- code specific for "Full Screen" check box (separated in user table)
			user =
			{
				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,
			},
		},

		-- definition of "enable vertical sync" check box
		widget_vsync_text =
		{
			skin = UI.skins.Label,
			left = 227, top = 364,--208,
			width = 152,

			text = Localize( "VSync" ),
		},

		widget_vsync =
		{
			skin = UI.skins.CheckBox,
			left = 410,	top = 366,--210,

			tabstop = 8,

			-- code specific for "enable vertical sync" check box (separated in user table)
			user =
			{
				-- code (implemented below)
				Initialize = nil,
				UpdateAssignedGlobals = nil,
			},
		},

		-- GUI data members
		relaunchNeeded = 0,

		-- GUI methods
		OnActivate = function( sender )
			for key,val in UI.PageOptionsVideo.GUI do
				if type( val ) == "table" and val.user and val.user.Initialize then
					val.user.Initialize();
				end
			end

			if( not( UI:IsScreenActive( "Options" ) and ( UI:IsScreenActive( "Options" ) ~= 0 ) ) ) then
				UI:ActivateScreen( "Options" );
				UI.PageOptions.GUI.VideoOptions.OnCommand( UI.PageOptions.GUI.VideoOptions );
			end
		end,

		UpdateGlobals = function( self )
			for key,val in UI.PageOptionsVideo.GUI do
				if type( val ) == "table" and val.user and val.user.UpdateAssignedGlobals then
					val.user.UpdateAssignedGlobals();
				end
			end

			if( UI.PageOptionsVideo.GUI.relaunchNeeded ~= 0 ) then
				-- code below no longer needed as relaunch will effectively just reset device when game is running (no real game relaunch)
				--if( ClientStuff ) then
				--	UI.YesNoBox( Localize( "TerminateCurrentGame" ), Localize( "TerminateCurrentGameLabel" ), UI.PageOptionsVideo.Relaunch );
				--else
					UI.PageOptionsVideo.Relaunch();
				--end
			end

			-- to get the page refreshing itself
			--GotoPage("Options");
			--UI.PageOptions.GUI.VideoOptions.OnCommand(UI.PageOptions.GUI.VideoOptions);
		end,
	},

	Relaunch = function( self )
    	System:LogToConsole( "Relaunching..." );
		Game:SendMessage( "Relaunch" );
		UI.PageOptionsVideo.GUI.relaunchNeeded = 0;
	end
}

-- and create screen from table
UI:CreateScreenFromTable( "VideoOptions", UI.PageOptionsVideo.GUI );

-- code for specific behaviour of individual widgets
UI.PageOptionsVideo.GUI.widget_renderer.user.Initialize = function( self )
	-- initalize widget
	UI.PageOptionsVideo.GUI.widget_renderer:Clear();
	UI.PageOptionsVideo.GUI.widget_renderer:AddItem( Localize( "Direct3D9" ) );
	UI.PageOptionsVideo.GUI.widget_renderer:AddItem( Localize( "OpenGL" ) );

	-- let widget reflect state of globals
	local curRenderer = strlower( getglobal( "r_Driver" ) );
	if( curRenderer == "direct3d9" ) then
		UI.PageOptionsVideo.GUI.widget_renderer:SelectIndex( 1 );
	elseif( curRenderer == "opengl" ) then
		UI.PageOptionsVideo.GUI.widget_renderer:SelectIndex( 2 );
	end;

	-- save initial selection state
	UI.PageOptionsVideo.GUI.widget_renderer.user.initialSelectionIndex =
		UI.PageOptionsVideo.GUI.widget_renderer:GetSelectionIndex();

	UI:DisableWidget( UI.PageOptionsVideo.GUI.widget_renderer );
end

UI.PageOptionsVideo.GUI.widget_renderer.user.DefInitialize = function( self )
	UI.PageOptionsVideo.GUI.widget_renderer:SelectIndex( 1 );
end

UI.PageOptionsVideo.GUI.widget_renderer.user.UpdateAssignedGlobals = function( self )
	local curSelectionIndex = UI.PageOptionsVideo.GUI.widget_renderer:GetSelectionIndex();

	-- has selection changed
	if( curSelectionIndex ~= UI.PageOptionsVideo.GUI.widget_renderer.user.initialSelectionIndex ) then
		if( curSelectionIndex == 1 ) then
			setglobal( "r_Driver", "Direct3D9" );
		elseif( curSelectionIndex == 2 ) then
			setglobal( "r_Driver", "OpenGL" );
		end
		System:LogToConsole( "Renderer changed, relaunch necessary..." );
		UI.PageOptionsVideo.GUI.relaunchNeeded = 1;
		UI.PageOptionsVideo.GUI.widget_renderer.user.initialSelectionIndex = curSelectionIndex;
	end
end



UI.PageOptionsVideo.GUI.widget_resolution.user.Initialize = function( self )
	-- initalize widget
	UI.PageOptionsVideo.GUI.widget_resolution:Clear();

	UI.PageOptionsVideo.GUI.widget_resolution.user.ScreenResolutionAndBpp = {};
	local lTmpScreenResolutionAndBpp = System:EnumDisplayFormats();

	local ref = UI.PageOptionsVideo.GUI.widget_resolution.user.ScreenResolutionAndBpp;
	local j = 1;
	for i, DispFmt in lTmpScreenResolutionAndBpp do
		if( DispFmt.bpp == 32 and DispFmt.width > 640 and DispFmt.height > 480 ) then -- filter modes, 32 bit and higher than 640x480 only!
			ref[ j ] = DispFmt;
			j = j + 1;
		end
	end

	for i, DispFmt in ref do
		UI.PageOptionsVideo.GUI.widget_resolution:AddItem( DispFmt.width.."x"..DispFmt.height.."x"..DispFmt.bpp );
	end

	-- let widget reflect state of globals
	local bpp = tonumber( getglobal( "r_ColorBits" ) );
	local sCurrentRes = tonumber( getglobal( "r_Width" ) ).."x"..tonumber( getglobal( "r_Height" ) ).."x"..bpp;
	UI.PageOptionsVideo.GUI.widget_resolution:Select( sCurrentRes );
end

UI.PageOptionsVideo.GUI.widget_resolution.user.DefInitialize = function( self )
	UI.PageOptionsVideo.GUI.widget_resolution:Select( "1024x768x32" );
end



UI.PageOptionsVideo.GUI.widget_resolution.user.UpdateAssignedGlobals = function( self )
	local index = UI.PageOptionsVideo.GUI.widget_resolution:GetSelectionIndex();
	local newRes = UI.PageOptionsVideo.GUI.widget_resolution.user.ScreenResolutionAndBpp[ index ];

	if( tostring( r_Width ) ~= tostring( newRes.width ) or
		tostring( r_Height )~= tostring( newRes.height ) or
		tostring( r_ColorBits ) ~= tostring( newRes.bpp ) ) then

		--System:LogToConsole( "Resolution changed, relaunch necessary..." );
		--UI.PageOptionsVideo.GUI.relaunchNeeded = 1;

		setglobal( "r_Width", newRes.width );
		setglobal( "r_Height", newRes.height );
		setglobal( "r_ColorBits", newRes.bpp );

		g_reload_ui = "cmd_goto_video_options";
		UI:Reload( 1 );
	end
end



UI.PageOptionsVideo.GUI.widget_fsaa.user.Initialize = function( self )
	-- initalize widget
	UI.PageOptionsVideo.GUI.widget_fsaa:Clear();
	
	FSAAModes = System:EnumAAFormats();
	
	UI.PageOptionsVideo.GUI.widget_fsaa:AddItem(Localize("None"));
	
	UI.PageOptionsVideo.GUI.widget_fsaa.user.FSAAModes = {};
	
	for n, mode in FSAAModes do
		local i = UI.PageOptionsVideo.GUI.widget_fsaa:AddItem(mode.desc);
		
		UI.PageOptionsVideo.GUI.widget_fsaa.user.FSAAModes[i] = mode;
	end
	
	-- let widget reflect state of globals
	local fsaa = tonumber(getglobal("r_FSAA" ));
	local fsaaSamples = tonumber(getglobal("r_FSAA_samples"));
	local fsaaQuality = tonumber(getglobal("r_FSAA_quality"));
	if(fsaa ~= 0) then	
		-- find the correct match
		for n, mode in UI.PageOptionsVideo.GUI.widget_fsaa.user.FSAAModes do
			if ((fsaaSamples == tonumber(mode.samples)) and (fsaaQuality == tonumber(mode.quality))) then
				UI.PageOptionsVideo.GUI.widget_fsaa:SelectIndex(n);
				break;
			end			
		end
	else
		UI.PageOptionsVideo.GUI.widget_fsaa:SelectIndex(1);
	end;

	-- save initial selection state
	UI.PageOptionsVideo.GUI.widget_fsaa.user.initialSelectionIndex =
		UI.PageOptionsVideo.GUI.widget_fsaa:GetSelectionIndex();

	-- disable widget when game is running as FSAA can't be adjusted right now
	if( ClientStuff ) then
		UI:DisableWidget( UI.PageOptionsVideo.GUI.widget_fsaa );
	else
		UI:EnableWidget( UI.PageOptionsVideo.GUI.widget_fsaa );
	end
end

UI.PageOptionsVideo.GUI.widget_fsaa.user.DefInitialize = function( self )
	if( UI:IsWidgetEnabled( UI.PageOptionsVideo.GUI.widget_fsaa ) ) then
		UI.PageOptionsVideo.GUI.widget_fsaa:SelectIndex( 1 );
	end;
end

UI.PageOptionsVideo.GUI.widget_fsaa.user.UpdateAssignedGlobals = function( self )
	local curSelectionIndex = UI.PageOptionsVideo.GUI.widget_fsaa:GetSelectionIndex();

	-- has selection changed
	if( curSelectionIndex ~= UI.PageOptionsVideo.GUI.widget_fsaa.user.initialSelectionIndex ) then
		if( curSelectionIndex == 1 ) then
			setglobal( "r_FSAA", 0 );			-- no fsaa
			setglobal( "r_FSAA_samples", 1 );
			setglobal( "r_FSAA_quality", 0 );
		else
			local mode = UI.PageOptionsVideo.GUI.widget_fsaa.user.FSAAModes[curSelectionIndex];
			setglobal( "r_FSAA", 1 );			-- no fsaa
			setglobal( "r_FSAA_samples", mode.samples );
			setglobal( "r_FSAA_quality", mode.quality );
		end
		--System:LogToConsole( "FSAA mode and number of FSAA samples changed, relaunch necessary..." );
		--UI.PageOptionsVideo.GUI.relaunchNeeded = 1;
		UI.PageOptionsVideo.GUI.widget_fsaa.user.initialSelectionIndex = curSelectionIndex;
	end
end



UI.PageOptionsVideo.GUI.widget_brightness.user.Initialize = function( self )
	local brightness = tonumber( getglobal( "r_Brightness" ) );
	UI.PageOptionsVideo.GUI.widget_brightness:SetValue( brightness );
end

UI.PageOptionsVideo.GUI.widget_brightness.user.DefInitialize = function( self )
	UI.PageOptionsVideo.GUI.widget_brightness:SetValue( 0.5 );
end

UI.PageOptionsVideo.GUI.widget_brightness.user.UpdateAssignedGlobals = function( self )
end

UI.PageOptionsVideo.GUI.widget_brightness.user.OnChanged = function( self )
	-- get normalize brightness [0 .. 1] from slider widget
	local brightness = tonumber( UI.PageOptionsVideo.GUI.widget_brightness:GetValue() );
	if( brightness < 0.0 ) then
		brightness = 0.0;
	elseif( brightness > 1.0 ) then
		brightness = 1.0;
	end;
	-- set brightness
	setglobal( "r_Brightness", brightness );
end;



UI.PageOptionsVideo.GUI.widget_fullscreen.user.Initialize = function( self )
	local curValue = tonumber( getglobal( "r_Fullscreen" ) );
	UI.PageOptionsVideo.GUI.widget_fullscreen:SetChecked( curValue ~= 0 );
end

UI.PageOptionsVideo.GUI.widget_fullscreen.user.DefInitialize = function( self )
	UI.PageOptionsVideo.GUI.widget_fullscreen:SetChecked( 1 );
end

UI.PageOptionsVideo.GUI.widget_fullscreen.user.UpdateAssignedGlobals = function( self )
	local newValue = 0;
	local curValue = tonumber( getglobal( "r_Fullscreen" ) );

	if UI.PageOptionsVideo.GUI.widget_fullscreen:GetChecked() then
		newValue = 1;
	end
	if( newValue ~= curValue ) then
		--System:LogToConsole( "Fullscreen changed, relaunch necessary..." );
		--UI.PageOptionsVideo.GUI.relaunchNeeded = 1;
		setglobal( "r_Fullscreen", newValue );
	end
end



UI.PageOptionsVideo.GUI.widget_vsync.user.Initialize = function( self )
	local curValue = tonumber( getglobal( "r_VSync" ) );
	UI.PageOptionsVideo.GUI.widget_vsync:SetChecked( curValue ~= 0 );

	-- disable widget when game is running as VSynch can't be adjusted right now
	if( ClientStuff ) then
		UI:DisableWidget( UI.PageOptionsVideo.GUI.widget_vsync );
	else
		UI:EnableWidget( UI.PageOptionsVideo.GUI.widget_vsync );
	end
end

UI.PageOptionsVideo.GUI.widget_vsync.user.DefInitialize = function( self )
	if( UI:IsWidgetEnabled( UI.PageOptionsVideo.GUI.widget_vsync ) ) then
		UI.PageOptionsVideo.GUI.widget_vsync:SetChecked( 0 );
	end;
end

UI.PageOptionsVideo.GUI.widget_vsync.user.UpdateAssignedGlobals = function( self )
	local newValue = 0;
	local curValue = tonumber( getglobal( "r_VSync" ) );

	if UI.PageOptionsVideo.GUI.widget_vsync:GetChecked() then
		newValue = 1;
	end
	if( newValue ~= curValue ) then
		System:LogToConsole( "VSync changed, relaunch necessary..." );
		UI.PageOptionsVideo.GUI.relaunchNeeded = 1;
		setglobal( "r_VSync", newValue );
	end
end



function UI.PageOptionsVideo.ResetToDefaults()
	for key,val in UI.PageOptionsVideo.GUI do
		if type( val ) == "table" and val.user and val.user.Initialize then
			val.user.Initialize();
			if( val.user.DefInitialize ) then
				val.user.DefInitialize();
			end
		end
	end
	UI.PageOptionsVideo.GUI.widget_brightness.user.OnChanged();
end