class "options_dialog" (CUIScriptWnd)

function options_dialog:__init() super()

	self.m_preconditions	= {}
	self.m_deffer_ex	= {}
    self:InitControls()
    self:InitCallBacks()
	self.tab:SetActiveTab			("video")
end

function options_dialog:__finalize()

end

function options_dialog:InitControls()
	self:SetWndRect				(Frect():set(0,0,1024,768))
	self:Enable					(true)

	local xml					= CScriptXmlInit()
	xml:ParseFile				("ui_mm_opt.xml")

	xml:InitStatic				("background", self)
	self.dialog					= xml:InitStatic("main_dialog:dialog", self)

--	xml:InitStatic				("main_dialog:cap_options", self.dialog)

	self.dlg_video				= ui_mm_opt_video.opt_video()
	self.dlg_video:InitControls	(0,0, xml, self)
	self.dlg_video:Show			(false)
	self.dialog:AttachChild		(self.dlg_video)
	xml:InitWindow				("tab_size", 0, self.dlg_video)

	self.dlg_sound				= ui_mm_opt_sound.opt_sound()
	self.dlg_sound:InitControls	(0,0, xml, self)
	self.dlg_sound:Show			(false)
	self.dialog:AttachChild		(self.dlg_sound)
	xml:InitWindow				("tab_size", 0, self.dlg_sound)

	self.dlg_gameplay			= ui_mm_opt_gameplay.opt_gameplay()
	self.dlg_gameplay:InitControls(0,0, xml, self)
	self.dlg_gameplay:Show		(false)
	self.dialog:AttachChild		(self.dlg_gameplay)
	xml:InitWindow				("tab_size", 0, self.dlg_gameplay)

	self.dlg_controls			= ui_mm_opt_controls.opt_controls()
	self.dlg_controls:InitControls(0,0, xml, self)
	self.dlg_controls:Show		(false)
	self.dialog:AttachChild		(self.dlg_controls)
	xml:InitWindow				("tab_size", 0, self.dlg_controls)
--[[
	self.dlg_con_cmd			= ui_mm_opt_con_cmd.opt_con_cmd()
	self.dlg_con_cmd:InitControls(0,0, xml, self)
	self.dlg_con_cmd:Show		(false)
	self.dialog:AttachChild		(self.dlg_con_cmd)
	xml:InitWindow				("tab_size", 0, self.dlg_con_cmd)
]]
	self.dlg_video_adv			= ui_mm_opt_video_adv.opt_video_adv()
	self.dlg_video_adv:InitControls(0,0, xml, self)
	self.dlg_video_adv:Show		(false)
	self.dialog:AttachChild		(self.dlg_video_adv)
	xml:InitWindow				("tab_size", 0, self.dlg_video_adv)

	local btn
	btn = xml:Init3tButton		("main_dialog:btn_accept", self.dialog)
	self:Register				(btn, "btn_accept")
	btn = xml:Init3tButton		("main_dialog:btn_cancel", self.dialog)
	self:Register				(btn, "btn_cancel")

	self.tab					= xml:InitTab("main_dialog:tab", self.dialog)
	self:Register				(self.tab, "tab")


	self.message_box			= CUIMessageBoxEx()
	self:Register				(self.message_box, "mb")

	self.b_restart_system_shown	= false
	self.timer					= nil

	self.cap_download			= xml:InitStatic		("download_static",self)
	self.text_download			= xml:InitStatic		("download_text",self)
	self.download_progress		= xml:InitProgressBar	("progress_download", self)
	self.btn_cancel_download	= xml:Init3tButton		("btn_cancel_download", self)
	self:Register				(self.btn_cancel_download, "btn_cancel_download")

end

function options_dialog:SetCurrentValues()
	local opt = COptionsManager()

	opt:SetCurrentValues("mm_opt_video_preset")
	opt:SaveBackupValues("mm_opt_video_preset")

	opt:SetCurrentValues("mm_opt_video")
	opt:SaveBackupValues("mm_opt_video")

	opt:SetCurrentValues("mm_opt_video_adv")
	opt:SaveBackupValues("mm_opt_video_adv")

	opt:SetCurrentValues("mm_opt_gameplay")
	opt:SaveBackupValues("mm_opt_gameplay")

	opt:SetCurrentValues("mm_opt_sound")
	opt:SaveBackupValues("mm_opt_sound")

	opt:SetCurrentValues("mm_opt_controls")
	opt:SaveBackupValues("mm_opt_controls")

	opt:SetCurrentValues("key_binding")
	opt:SaveBackupValues("key_binding")

	self:UpdateDependControls()
end

function options_dialog:UpdateDependControls()
	self.scroll_v:Clear()
	
	local current_id = self.combo_renderer:CurrentID()
	local value = self.combo_renderer:GetValueOf(current_id)

	for _, j in pairs(self.m_preconditions) do
		if j.func(value) then
			self.scroll_v:AddWindow(j.control, true)
			j.control:SetAutoDelete(false)
		end
	end

    for _, t in pairs(self.m_deffer_ex) do
        t.func(t.arg)
    end
end

function options_dialog:InitCallBacks()
	self:AddCallback("tab",					ui_events.TAB_CHANGED,		self.OnTabChange,			self)
	self:AddCallback("btn_advanced_graphic",ui_events.BUTTON_CLICKED,	self.OnBtnAdvGraphic,		self)
	self:AddCallback("btn_accept",			ui_events.BUTTON_CLICKED,	self.OnBtnAccept,			self)
	self:AddCallback("btn_cancel",			ui_events.BUTTON_CLICKED,	self.OnBtnCancel,			self)
	self:AddCallback("btn_default_graphic",	ui_events.BUTTON_CLICKED,	self.OnBtnDefGraph,			self)
	self:AddCallback("btn_default_sound",	ui_events.BUTTON_CLICKED,	self.OnBtnDefSound,			self)
	self:AddCallback("combo_preset",		ui_events.LIST_ITEM_SELECT,	self.OnPresetChanged,		self)
	self:AddCallback("btn_simply_graphic",	ui_events.BUTTON_CLICKED,	self.OnSimplyGraphic,		self)
	self:AddCallback("btn_keyb_default",	ui_events.BUTTON_CLICKED,	self.OnBtnKeybDefault,		self)
	self:AddCallback("combo_renderer",		ui_events.LIST_ITEM_SELECT,	self.UpdateDependControls,	self)
	self:AddCallback("btn_cancel_download",	ui_events.BUTTON_CLICKED,	self.OnBtn_CancelDownload,	self)

	self:AddCallback("mb", ui_events.MESSAGE_BOX_YES_CLICKED, self.OnApplyChanges, self)
	self:AddCallback("mb", ui_events.MESSAGE_BOX_NO_CLICKED, self.OnDiscardChanges,self)
end

function options_dialog:OnBtnCheckUpdates()
	local console			= get_console()
	console:execute			("check_for_updates 1")
end

function options_dialog:OnBtnKeybDefault()
	local console			= get_console()
	console:execute			("default_controls")

	local opt				= COptionsManager()
	opt:SetCurrentValues	("mm_opt_controls")
	opt:SetCurrentValues	("key_binding")
end

function options_dialog:OnPresetChanged()
	local opt					= COptionsManager()
	opt:SetCurrentValues		("mm_opt_video_adv")
end


function options_dialog:OnBtnDefGraph()
	local opt = COptionsManager()
	opt:SendMessage2Group("mm_opt_video","set_default_value")
end

function options_dialog:OnBtnDefSound()
	local opt = COptionsManager()
	opt:SendMessage2Group("mm_opt_sound","set_default_value")
end

function options_dialog:OnBtnAccept()

	local opt		= COptionsManager()
	local console	= get_console()

   	console:execute("cfg_save tmp")

	opt:SaveValues("mm_opt_video_preset")
	opt:SaveValues("key_binding")
	opt:SaveValues("mm_opt_video")
	opt:SaveValues("mm_opt_video_adv")
	opt:SaveValues("mm_opt_gameplay")
	opt:SaveValues("mm_opt_sound")
	opt:SaveValues("mm_opt_controls")

	if opt:NeedVidRestart() then
		_G.b_discard_settings_shown = true
		self.message_box:InitMessageBox("message_box_yes_no")
		self.message_box:SetText(string.format("%s %d% s", game.translate_string("ui_mm_confirm_changes"), 15, game.translate_string("mp_si_sec")))
		self.message_box:ShowDialog(true)
	else
		self:OnApplyChanges()
	end

	opt:OptionsPostAccept()

	console:execute("cfg_save")
end

function options_dialog:OnBtnCancel()
	local opt				= COptionsManager()
	opt:UndoGroup			("mm_opt_video_preset")
	opt:UndoGroup			("mm_opt_video")
	opt:UndoGroup			("mm_opt_video_adv")
	opt:UndoGroup			("mm_opt_sound")
	opt:OptionsPostAccept	()

	self.owner:ShowDialog(true)
	self:HideDialog()
	self.owner:Show(true)
end

function options_dialog:OnTabChange()
	self.dlg_video:Show		(false)
	self.dlg_sound:Show		(false)
	self.dlg_gameplay:Show	(false)
	self.dlg_controls:Show	(false)
	self.dlg_video_adv:Show	(false)

	local ids = self.tab:GetActiveId()
	if ids == "video" then
		self.dlg_video:Show(true)
	elseif ids == "sound" then
		self.dlg_sound:Show(true)
	elseif ids == "gameplay" then
		self.dlg_gameplay:Show(true)
	elseif ids == "controls" then
		self.dlg_controls:Show(true)
	end

end

function options_dialog:OnBtnAdvGraphic()
	self.dlg_video:Show		(false)
	self.dlg_video_adv:Show	(true)
end

function options_dialog:OnSimplyGraphic()
	self.dlg_video:Show		(true)
	self.dlg_video_adv:Show	(false)
end

function options_dialog:OnKeyboard(dik, keyboard_action)
	local res = CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)

	if res==false then

		local bind = dik_to_bind(dik)

		if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
			if dik == DIK_keys.DIK_ESCAPE then
				if self.dlg_video_adv:IsShown() then
					self.dlg_video_adv:Show	(false)
					self.dlg_video:Show		(true)
				else
					self.owner:ShowDialog(true)
					self:HideDialog()
					self.owner:Show(true)
				end
			end

		end
	end

	return res
end

function options_dialog:Update()
	CUIScriptWnd.Update(self)

	local mm = main_menu.get_main_menu()
	local sss = mm:GetPatchProgress()

	if sss:GetInProgress() then
		self.text_download:Show					(true)
		self.cap_download:Show					(true)
		self.download_progress:Show				(true)
		local _progr							= sss:GetProgress()
		self.download_progress:SetProgressPos	(_progr)
		local str								= string.format("%.0f%%(%s)",_progr,sss:GetFlieName())
		self.text_download:TextControl():SetText				(str)
		self.btn_cancel_download:Show			(true)
	else
		self.text_download:Show					(false)
		self.cap_download:Show					(false)
		self.download_progress:Show				(false)
		self.btn_cancel_download:Show			(false)
	end
	if(_G.b_discard_settings_shown) then
		local tg = math.floor(time_global_async()/1000)
		if(self.timer==nil) then
			self.timer = tg
		end
		self.message_box:SetText(string.format("%s %d %s", game.translate_string("ui_mm_confirm_changes"), 15-(tg-self.timer), game.translate_string("mp_si_sec")))
		if(tg-self.timer>=15) then
			self.message_box:HideDialog()
			self:OnDiscardChanges()
		end
	end
end

function options_dialog:OnBtn_CancelDownload()
	local mm = main_menu.get_main_menu()
	mm:CancelDownload()
end

function options_dialog:OnApplyChanges()
    self.timer = nil
    _G.b_discard_settings_shown = false

	self.owner:ShowDialog(true)
	self:HideDialog()
	self.owner:Show(true)

	if false==self.b_restart_system_shown then
		local opt = COptionsManager()
		if opt:NeedSystemRestart() then
			self.b_restart_system_shown = true
			self.message_box:InitMessageBox("message_box_restart_game")
			self.message_box:ShowDialog(true)
		end
	end
end

function options_dialog:OnDiscardChanges()
    self.timer = nil
    _G.b_discard_settings_shown = false

	local c = get_console()
   	c:execute("cfg_load tmp")

	self:SetCurrentValues()

	c:execute("vid_restart")
   	c:execute("cfg_save")
end

function options_dialog:OnMenuReloaded()
	if(_G.b_discard_settings_shown) then
	    self.message_box:InitMessageBox("message_box_yes_no")
	    self.message_box:SetText(string.format("%s %d% s", game.translate_string("ui_mm_confirm_changes"), 15, game.translate_string("mp_si_sec")))
	    self.message_box:ShowDialog(true)
	end
end
