options_window = {}

options_window.owner_window = false
options_window.current_screen_mode = nil
options_window.delay_time_count = 0
options_window.delay_time_interval = 5000
options_window.update_delay = 100
options_window.show_hint = true

options_window.main_menu_options_ok_button_string_id = _T("$177")


function options_window.run_resolution_apply_dialog(_owner)
		local app = sf.core.g_Application
		local window = s4.CResolutionApplyDialog(_T("options_window.resolution_apply_dialog_update"), options_window.update_delay)
		window.Load(
			app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_resolution_apply_dialog"), true), 
			app.GetSettings().GetChild(_T("GUITemplates"), false))
		window.UpdateLayout()   
		MoveWindowToCenter(_owner, window)
		
		local window_ptr = sf.gui.CBaseWindowPtrT(window)
		options_window.delay_time_count = options_window.delay_time_interval
		local modal_res = _owner.DoModal(window_ptr)
		window_ptr = nil
		collectgarbage()			
		return modal_res
end

function options_window.resolution_apply_dialog_update(_owner)
	options_window.delay_time_count = options_window.delay_time_count - options_window.update_delay;
	local lable_widget = __cast(_owner.GetWidget(_T("time_label")).get(), "sf.gui.CLabelWidget")
	if options_window.delay_time_count < 0 then
		_owner.EndModal(0)
	else
		if lable_widget then
			lable_widget.SetText(_T(string.format ("%i", (options_window.delay_time_count/1000) )))
		end
	end

end

options_window.sliders_params = {}

options_window.sliders_params["sound_volume_slider"] = {}
options_window.sliders_params["sound_volume_slider"].widget = false
options_window.sliders_params["sound_volume_slider"].min_value = 0 
options_window.sliders_params["sound_volume_slider"].max_value = 100
options_window.sliders_params["sound_volume_slider"].get_function = function () return s4.g_GameOptions.Instance().GetSoundVolume() end
options_window.sliders_params["sound_volume_slider"].set_function = function (_value) s4.g_GameOptions.Instance().SetSoundVolume(_value) 
s4.g_GameOptions.Instance().ApplyMusicChanges()
end

options_window.sliders_params["music_volume_slider"] = {}
options_window.sliders_params["music_volume_slider"].widget = false
options_window.sliders_params["music_volume_slider"].min_value = 0 
options_window.sliders_params["music_volume_slider"].max_value = 100
options_window.sliders_params["music_volume_slider"].get_function = function () return s4.g_GameOptions.Instance().GetMusicVolume() end
options_window.sliders_params["music_volume_slider"].set_function = function (_value) 
s4.g_GameOptions.Instance().SetMusicVolume(_value) 
s4.g_GameOptions.Instance().ApplyMusicChanges()
end

options_window.sliders_params["transparency_slider"] = {}
options_window.sliders_params["transparency_slider"].widget = false
options_window.sliders_params["transparency_slider"].min_value = 0 
options_window.sliders_params["transparency_slider"].max_value = 255
options_window.sliders_params["transparency_slider"].get_function = function () return s4.g_GameOptions.Instance().GetTransparencyHUD() end
options_window.sliders_params["transparency_slider"].set_function = function (_value) s4.g_GameOptions.Instance().SetTransparencyHUD(_value) end

options_window.checkbox_params = {}

options_window.checkbox_params["custom_cursors_checkbox"] = {}
options_window.checkbox_params["custom_cursors_checkbox"].widget = false
options_window.checkbox_params["custom_cursors_checkbox"].get_function = function ()
	return s4.g_GameOptions.Instance().GetCustomCursors() 
end
options_window.checkbox_params["custom_cursors_checkbox"].set_function = function (_value) 
	sf.gui.g_Cursor.Instance().EnableCustomCursors(_value)
	s4.g_GameOptions.Instance().SetCustomCursors(_value) 
end

options_window.checkbox_params["show_hint_checkbox"] = {}
options_window.checkbox_params["show_hint_checkbox"].widget = false
options_window.checkbox_params["show_hint_checkbox"].get_function = function ()
	return options_window.show_hint
end
options_window.checkbox_params["show_hint_checkbox"].set_function = function (_value) 
	options_window.show_hint = _value
end

options_window.checkbox_params["16bpp_checkbox"] = {}
options_window.checkbox_params["16bpp_checkbox"].widget = false
options_window.checkbox_params["16bpp_checkbox"].get_function = function ()
	return not s4.g_GameOptions.Instance().GetScreenMode().Bpp
end
options_window.checkbox_params["16bpp_checkbox"].set_function = function (_value) 
	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	s4.g_GameOptions.Instance().SetScreenMode(s4.ScreenModeInfo(screen_mode.FullScreen, not _value, screen_mode.Width, screen_mode.Height)) 	
end

options_window.checkbox_params["32bpp_checkbox"] = {}
options_window.checkbox_params["32bpp_checkbox"].widget = false
options_window.checkbox_params["32bpp_checkbox"].get_function = function ()
	return s4.g_GameOptions.Instance().GetScreenMode().Bpp
end
options_window.checkbox_params["32bpp_checkbox"].set_function = function (_value) 
	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	s4.g_GameOptions.Instance().SetScreenMode(s4.ScreenModeInfo(screen_mode.FullScreen, _value, screen_mode.Width, screen_mode.Height)) 	
end

options_window.checkbox_params["full_screen_checkbox"] = {}
options_window.checkbox_params["full_screen_checkbox"].widget = false
options_window.checkbox_params["full_screen_checkbox"].get_function = function ()
 	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	return screen_mode.FullScreen
 end
options_window.checkbox_params["full_screen_checkbox"].set_function = function (_value) 
	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	s4.g_GameOptions.Instance().SetScreenMode(s4.ScreenModeInfo(_value, screen_mode.Bpp, screen_mode.Width, screen_mode.Height)) 	
end

options_window.checkbox_params["use_ie_settings_checkbox"] = {}
options_window.checkbox_params["use_ie_settings_checkbox"].widget = false
options_window.checkbox_params["use_ie_settings_checkbox"].get_function = function ()
	return s4.g_GameOptions.Instance().GetUseIESettings()
end

options_window.checkbox_params["use_ie_settings_checkbox"].set_function = function (_value) 
	s4.g_GameOptions.Instance().SetUseIESettings(_value)
end


options_window.combobox_params = {}



options_window.combobox_params["graph_quality_combobox"] = {}
options_window.combobox_params["graph_quality_combobox"].widget = false
options_window.combobox_params["graph_quality_combobox"].Items = {}


options_window.combobox_params["graph_quality_combobox"].Items[1] = { name = "$173", value = s4.CGameOptions.GraphQualityTypeLow}
options_window.combobox_params["graph_quality_combobox"].Items[2] = { name = "$174", value = s4.CGameOptions.GraphQualityTypeMedium}
options_window.combobox_params["graph_quality_combobox"].Items[3] = { name = "$175", value = s4.CGameOptions.GraphQualityTypeHigh}
options_window.combobox_params["graph_quality_combobox"].get_function = function ()  return s4.g_GameOptions.Instance().GetGraphQuality()  end
options_window.combobox_params["graph_quality_combobox"].set_function = function (_value) s4.g_GameOptions.Instance().SetGraphQuality(_value) end

function options_window.form_screen_resolution_value ( _bpp, _width, _height)
	res_table = {}
	res_table.Bpp = _bpp
	res_table.Width = _width
	res_table.Height = _height
	return res_table
end

options_window.combobox_params["screen_resolution_combobox"] = {}

options_window.combobox_params["screen_resolution_combobox"].widget = false

local items = {}
local renderer = __const(sf.core.g_Application.GetRenderer())
local all_resolutions = sf.graphics.CRenderer.ResolutionsT()
renderer.GetAvailableResolutions(all_resolutions)
local res_i, res_e = all_resolutions.begin(), all_resolutions._end()
while res_i~=res_e do
	local res = res_i.value
	
	local res_value = 
	{
		name = string.format("%ix%i", res.Width, res.Height),
		value = options_window.form_screen_resolution_value( res.Bpp32, res.Width, res.Height)
	}
	if res_value.value.Width>=res_value.value.Height then
		local found = false
		for k,v in ipairs(items) do
			if (v.name==res_value.name) and (not v.value.Bpp) then 
				items[k] = res_value 
				found = true 
				break 
			end
		end
		if not found then items[#items + 1] = res_value end
	end
	res_i.inc()
end
io.flush()
options_window.combobox_params["screen_resolution_combobox"].Items = items

options_window.combobox_params["screen_resolution_combobox"].compare_function = function (_a, _b)  
	if (_a.Width == _b.Width) and (_a.Height == _b.Height) then
		return true
	end
		return false
end

options_window.combobox_params["screen_resolution_combobox"].get_function = function () 
	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	return options_window.form_screen_resolution_value ( screen_mode.Bpp, screen_mode.Width, screen_mode.Height)
 end
 
options_window.combobox_params["screen_resolution_combobox"].set_function = function (_value) 
	local screen_mode = s4.g_GameOptions.Instance().GetScreenMode()   
	if screen_mode.Width==_value.Width and screen_mode.Height==_value.Height then return end
	local res = s4.g_GameOptions.Instance().SetScreenMode(s4.ScreenModeInfo(screen_mode.FullScreen, screen_mode.Bpp, _value.Width, _value.Height)) 
end



--[[
back_combobox
screen_resolution_combobox
]]


function options_window.run_options_window(_owner, _hide_game_buttons)
	options_window.current_screen_mode = s4.g_GameOptions.Instance().GetScreenMode() 
	local app = sf.core.g_Application
	local window = s4.COnKeyDownWindow()
	window.Load(
		app.GetSettings().GetChild(_T("GUI"), false).GetChildByAttribute(_T("window"), _T("id"), _T("s4_options"), true), 
		app.GetSettings().GetChild(_T("GUITemplates"), false))
	window.UpdateLayout()                            
	options_window.owner_window = window
	MoveWindowToCenter(_owner, window)
	if _hide_game_buttons then
		help_button = __cast(window.GetWidget(_T("help"), true).get(), "sf.gui.CButtonWidget")
		main_menu_button = __cast(window.GetWidget(_T("main_menu"), true).get(), "sf.gui.CButtonWidget")
		continue_button = __cast(window.GetWidget(_T("continue"), true).get(), "sf.gui.CButtonWidget")
		

		continue_button.SetText(sf.misc.g_StringTable.Instance().FormatByStringsID(options_window.main_menu_options_ok_button_string_id))

		set_widget_flag_value(help_button, sf.gui.CBaseWidget.FlagHidden, true)
		set_widget_flag_value(help_button, sf.gui.CBaseWidget.FlagDisabled, true)

		set_widget_flag_value(main_menu_button, sf.gui.CBaseWidget.FlagHidden, true)
		set_widget_flag_value(main_menu_button, sf.gui.CBaseWidget.FlagDisabled, true)
	end
	
	options_window.init_options_window(window)	
	window_ptr = sf.gui.CBaseWindowPtrT(window)
	return _owner.DoModal(window_ptr)
end

function options_window.set_slider_value(_slider_id, _value)
	local current_silder = options_window.sliders_params[_slider_id]
	if current_silder then
		local diver = (current_silder.max_value - current_silder.min_value)
		if math.abs(diver) > 0.0001 then
			if current_silder.widget then
				current_silder.widget.SetValue((_value-current_silder.min_value)/diver, false)		
			end
		end
	end
end

function options_window.get_slider_value(_slider_id)
	local current_silder = options_window.sliders_params[_slider_id]
	if current_silder then
		local slider_value = 0
		if current_silder.widget then
			slider_value = current_silder.widget.GetValue()		
		end
		local res = current_silder.min_value + (current_silder.max_value - current_silder.min_value)*slider_value
		return res
	else
		return 0 
	end
end

function options_window.fill_widgets()
	for slider_id in pairs(options_window.sliders_params) do
		local current_get_fun = options_window.sliders_params[slider_id].get_function
		if current_get_fun then
			options_window.set_slider_value(slider_id, current_get_fun())
		end
	end
	for checkbox_id in pairs(options_window.checkbox_params) do
		local current_get_fun = options_window.checkbox_params[checkbox_id].get_function
		if current_get_fun then
			local current_widget = options_window.checkbox_params[checkbox_id].widget
			if current_widget then
				current_widget.Check(current_get_fun(), false)
			end
		end
	end

	for combobox_id in pairs(options_window.combobox_params) do
		local current_get_fun = options_window.combobox_params[combobox_id].get_function
		if current_get_fun then
			local current_widget = options_window.combobox_params[combobox_id].widget
			if current_widget then
				local value = current_get_fun()
				local index = -1
				local max_index = #options_window.combobox_params[combobox_id].Items
				local compare_function =  options_window.combobox_params[combobox_id].compare_function
				if compare_function then
					for item_index = 1,max_index do
						if compare_function(options_window.combobox_params[combobox_id].Items[item_index].value, value) then
							index = item_index - 1
						end
					end
				else
					for item_index = 1,max_index do
						if options_window.combobox_params[combobox_id].Items[item_index].value == value then
							index = item_index - 1
						end
					end
				
				end
				current_widget.SelectLine(index, false)
			end
		end
	end
	
end

function options_window.init_options_window(_owner)
	for slider_id in pairs(options_window.sliders_params) do
		options_window.sliders_params[slider_id].widget	= 
			__cast(_owner.GetWidget(_T(slider_id), true).get(), "sf.gui.CSliderWidget")
	end
	for checkbox_id in pairs(options_window.checkbox_params) do
		options_window.checkbox_params[checkbox_id].widget = 
			__cast(_owner.GetWidget(_T(checkbox_id), true).get(), "sf.gui.CCheckboxWidget")
	end

	for combobox_id in pairs(options_window.combobox_params) do
		options_window.combobox_params[combobox_id].widget	= 
			__cast(_owner.GetWidget(_T(combobox_id), true).get(), "sf.gui.CComboBoxWidget")
	end

	for combobox_id in pairs(options_window.combobox_params) do
		local combobox_widget = options_window.combobox_params[combobox_id].widget
		if combobox_widget then
			combobox_widget.ClearLines()
			local max_index = #options_window.combobox_params[combobox_id].Items
			for index = 1,max_index do
				combobox_widget.AddLine(_T(options_window.combobox_params[combobox_id].Items[index].name))
			end
		end
	end

	if s4.g_ProfileManager.Instance().GetCurrentProfile() then
		options_window.show_hint = s4.g_ProfileManager.Instance().GetCurrentProfile().IsShow()
	end
	
	options_window.fill_widgets()
end

function options_window.apply_changes()
	s4.g_GameOptions.Instance().UpdateLocalSettings()
	local res = s4.g_GameOptions.Instance().ApplyOptionsChanges();
	if res then
		local dialog_result =  options_window.run_resolution_apply_dialog(options_window.owner_window)
		if dialog_result == 0 then
			s4.g_GameOptions.Instance().RestoreResolution()
		end
	end
	s4.g_GameOptions.Instance().Save()
	if s4.g_ProfileManager.Instance().GetCurrentProfile() then
		s4.g_ProfileManager.Instance().GetCurrentProfile().SetShow(options_window.show_hint)
	end
end

function options_window.restore()
	s4.g_GameOptions.Instance().RestoreOptions()
	s4.g_GameOptions.Instance().Save()
end

function options_window.options_window_script(_owner, _action, _child)
	if _action==_T("mouse_right_click") then
		options_window.restore()
		_owner.EndModal(0)
	elseif T_(_action).c_str()=="key_down_escape" then
		options_window.restore()
		_owner.EndModal(0)
	elseif T_(_action).c_str()=="key_down_enter" then
		options_window.apply_changes()
		_owner.EndModal(1)
	elseif _child then
		if _child.GetId()==_T("continue") and _action==_T("pressed") then
			options_window.apply_changes()
			_owner.EndModal(1)
		elseif _child.GetId()==_T("download_music") and _action==_T("pressed") then
			s4.g_MainLua.LoadScript(_T("scripts\\game\\download_music.lua"))
			download_music.try(_owner)
		elseif _child.GetId()==_T("cancel") and _action==_T("pressed") then
			options_window.restore()
			_owner.EndModal(0)
		elseif _child.GetId()==_T("help") and _action==_T("pressed") then
			set_widget_flag_value(_owner, sf.gui.CBaseWidget.FlagHidden, true)
			help_window.run_help_window(_owner)
			set_widget_flag_value(_owner, sf.gui.CBaseWidget.FlagHidden, false)
		elseif _child.GetId()==_T("main_menu") and _action==_T("pressed") then
			options_window.apply_changes()
			_owner.EndModal(2)
		elseif _action==_T("value_changed") then
			if options_window.sliders_params[T_(_child.GetId()).c_str()] then
				local slider_value = options_window.get_slider_value(T_(_child.GetId()).c_str())
				local current_set_function = options_window.sliders_params[T_(_child.GetId()).c_str()].set_function
				if current_set_function then
					current_set_function(slider_value)
				end
			end
		elseif _action==_T("check_changed") then
			if options_window.checkbox_params[T_(_child.GetId()).c_str()] then
				local checkbox_widget = options_window.checkbox_params[T_(_child.GetId()).c_str()].widget
				if checkbox_widget then
					local checkbox_value = checkbox_widget.IsChecked()
					local set_fun = options_window.checkbox_params[T_(_child.GetId()).c_str()].set_function
					if set_fun then
						set_fun(checkbox_value)
						options_window.fill_widgets()
					end
				end
			end
		elseif _action==_T("line_selected") then
			if options_window.combobox_params[T_(_child.GetId()).c_str()] then
				local combobox_widget = options_window.combobox_params[T_(_child.GetId()).c_str()].widget
				if combobox_widget then
					local combobox_value = combobox_widget.GetSelectedLine()
					local current_item = options_window.combobox_params[T_(_child.GetId()).c_str()].Items[combobox_value + 1]
					if current_item then
						local set_fun = options_window.combobox_params[T_(_child.GetId()).c_str()].set_function
						if set_fun then
							set_fun(current_item.value)
							options_window.fill_widgets()
						end
					end
				end
			end
		end
	end
end
