--[[-----------------------------------------------------------------------------------------------
 File       : ui_main_menu.script
 Description: Main Menu Dialog for STALKER
 Copyright  : 2004 © GSC Game World
 Author     : Serhiy Vynnychenko (narrator@gsc-game.kiev.ua)
 Editors    : barin (22.08.2010)
 Last edit  : 05.04.2013 [...]
--]]-----------------------------------------------------------------------------------------------

--/ -----------------------------------------------------------------
--/ VARIABLEs
--/ -----------------------------------------------------------------
local sModule = script_name()
--/ -----------------------------------------------------------------
local log = function(fmt,...) _G.log(sModule..":"..tostring(fmt),...) end
--/ -----------------------------------------------------------------
--/ CLASS CONSTRUCTOR
--/ -----------------------------------------------------------------------------------------------
class "main_menu" (CUIScriptWnd)
--/ -----------------------------------------------------------------------------------------------
function main_menu:__init() super()
	self:NeedCenterCursor(true)
	self.mbox_mode = 0
	self:InitControls()
	self:InitCallBacks()
	event("main_menu_on"):trigger() --/#+#>
end

function main_menu:__finalize()
end

function main_menu:InitControls()
	self:Init(0, 0, 1024, 768)
	
	local xml = CScriptXmlInit()
	xml:ParseFile("ui_mm_main.xml")
	
	local st = xml:InitStatic("back_movie", self)
	st:InitTexture("ui\\ui_vid_back_02")
	
	xml:InitStatic("background", self)
	
	self.shniaga = xml:InitMMShniaga("shniaga_wnd", self)
	
	self.message_box = CUIMessageBoxEx()
	self:Register(self.message_box, "msg_box")
	
	local mm = _G.main_menu.get_main_menu()
	local str = string.format("Lost Alpha ver. %s Developer's Cut", mm:GetGSVer())
	local wnd = xml:InitStatic("static_version", self)
	wnd:SetText( str )
	wnd:SetTextColor(255, 255, 255, 255)
	--[[
	if db.sTxtVerAdd then --/#+#
		wnd = xml:InitStatic("add_version", self)
		wnd:SetText( string.rep(" ",str:len() +2) .. db.sTxtVerAdd )
		if db.is_mixed_mode then
			wnd:SetTextColor(255, 231, 153, 022)
		else
			wnd:SetTextColor(255, 255, 255, 0)
		end
	end
	]]
	self:Show_Error(xml) --/#+#
end

function main_menu:Show_Error(xml) --/#+#
	if db.sErrTxt and db.sErrTxt ~= "" then
		local wnd = xml:InitStatic("menu_warning",self)
		wnd:SetText("-=WARNING=-\\n"..db.sErrTxt)
		wnd:SetTextColor(255, 255, 0, 0)
	end
end

function main_menu:Show(f)
	self.shniaga:SetVisibleMagnifier(f)
end

function main_menu:InitCallBacks()
	--/ title
	self:AddCallback("btn_credits",     ui_events.BUTTON_CLICKED, self.OnButton_credits_clicked,     self)
	--/ options
	self:AddCallback("btn_options",     ui_events.BUTTON_CLICKED, self.OnButton_options_clicked,     self)
	--/ message box
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_OK_CLICKED,        self.OnMsgOk,           self)
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_CANCEL_CLICKED,    self.OnMsgCancel,       self)
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_YES_CLICKED,       self.OnMsgYes,          self)
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_NO_CLICKED,        self.OnMsgNo,           self)
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_QUIT_GAME_CLICKED, self.OnMessageQuitGame, self)
	self:AddCallback("msg_box",         ui_events.MESSAGE_BOX_QUIT_WIN_CLICKED,  self.OnMessageQuitWin,  self)
	--/ quit
	self:AddCallback("btn_quit",        ui_events.BUTTON_CLICKED, self.OnButton_quit_clicked,        self)
	self:AddCallback("btn_quit_to_mm",  ui_events.BUTTON_CLICKED, self.OnButton_disconnect_clicked,  self)
	--/ new game
	self:AddCallback("btn_novice",      ui_events.BUTTON_CLICKED, self.OnButton_new_novice_game,     self)
	self:AddCallback("btn_stalker",     ui_events.BUTTON_CLICKED, self.OnButton_new_stalker_game,    self)
	self:AddCallback("btn_veteran",     ui_events.BUTTON_CLICKED, self.OnButton_new_veteran_game,    self)
	self:AddCallback("btn_master",      ui_events.BUTTON_CLICKED, self.OnButton_new_master_game,     self)
		--/ return|load|save
	self:AddCallback("btn_ret",         ui_events.BUTTON_CLICKED, self.OnButton_return_game,         self)
	self:AddCallback("btn_load",        ui_events.BUTTON_CLICKED, self.OnButton_load_clicked,        self)
	self:AddCallback("btn_save",        ui_events.BUTTON_CLICKED, self.OnButton_save_clicked,        self)
	self:AddCallback("btn_lastsave",    ui_events.BUTTON_CLICKED, self.OnButton_last_save,           self)
	--/ multiplayer
	self:AddCallback("btn_multiplayer", ui_events.BUTTON_CLICKED, self.OnButton_multiplayer_clicked, self)
	--/#?#
	self:AddCallback("btn_spawn",       ui_events.BUTTON_CLICKED, self.OnButton_load_spawn,          self)
	--/#+# add optiond
--	self:AddCallback("btn_options_la",  ui_events.BUTTON_CLICKED, self.OnButton_options_la,          self)
end

function main_menu:OnMsgOk()
	self.mbox_mode = 0
end

function main_menu:OnMsgCancel()
	self.mbox_mode = 0
end

function main_menu:OnMsgYes()
	if self.mbox_mode == 1 then
		self:LoadLastSave()
	end
	self.mbox_mode = 0
end

function main_menu:OnMsgNo()
	self.mbox_mode = 0
end

function main_menu:LoadLastSave()
	local flist = getFS():file_list_open_ex("$game_saves$", FS.FS_ListFiles, "*.sav")
	if flist:Size() > 0 then
		if event then event("game_load"):trigger() end --/#+#>
		local console = get_console()
		console:execute("main_menu off")
		console:execute("load_last_save")
	end
end

function main_menu:OnButton_last_save()
	if alife() then
		self.mbox_mode = 1
		self.message_box:Init("message_box_confirm_load_save")
		self:GetHolder():start_stop_menu(self.message_box, true)
	else
		self:LoadLastSave()
	end
end

function main_menu:OnButton_credits_clicked()
	game.start_tutorial("credits_seq")
end

function main_menu:OnButton_quit_clicked()
	self.message_box:Init("message_box_quit_windows")
	self:add_check_button() --/#+#
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function main_menu:OnButton_disconnect_clicked()
	self.message_box:Init("message_box_quit_game")
	if level.game_id() ~= 1 then
		self.message_box:SetText("ui_mm_disconnect_message") --/ MultiPlayer
	else
		self.message_box:SetText("ui_mm_quit_game_message") --/ SinglePlayer
		self:add_check_button() --/#+#
	end
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function main_menu:add_check_button() --/#+# added 'SaveOnQuit'
	if self:check_game() then
		self.chk_btn = CUICheckButton()
		self.chk_btn:Init(155,128,52,21) --/SHOC coordinates (region) of window button (without texture)
		self.chk_btn:SetTextureOffset(5,0) --/ offset of button texture (x,y)
		self.chk_btn:SetText("<"..game.translate_string("ui_mm_save_game")) --/ text button ("SaveOnQuit")
		self.chk_btn:SetTextAlign(CGameFont.alLeft) --/ Horizontal Align. Possible values are (alLeft[0],alRight[1],alCenter[2])
		self.chk_btn:SetTextColor(255,231,153,022)
		self.chk_btn:SetCheck(false) --/ set by default
		self.chk_btn:SetAutoDelete(true) --/ parent will delete this item on destructor
		self.message_box:AttachChild(self.chk_btn) --/ attaching button to message_box
	end
end

function main_menu:OnMessageQuitGame()
	if self:check_game() then --/#+#
		if self.chk_btn and self.chk_btn:GetCheck() then
			ui_save_dialog.save_dialog():SaveFile("quitsave")
		end
		event("disconnect"):trigger({act="quit_game"}) --/#+#>
	end
	get_console():execute("disconnect")
end

function main_menu:OnMessageQuitWin()
	if self:check_game() then --/#+#
		if self.chk_btn and self.chk_btn:GetCheck() then
			ui_save_dialog.save_dialog():SaveFile("quitsave")
		end
		event("quit"):trigger() --/#+#>
	end
	get_console():execute("quit")
end

function main_menu:OnButton_return_game()
	get_console():execute("main_menu off")
	event("main_menu_off"):trigger() --/#+#>
end

function main_menu:OnButton_new_novice_game()
	get_console():execute("g_game_difficulty gd_novice")
	self:StartGame()
end

function main_menu:OnButton_new_stalker_game()
	get_console():execute("g_game_difficulty gd_stalker")
	self:StartGame()
end

function main_menu:OnButton_new_veteran_game()
	get_console():execute("g_game_difficulty gd_veteran")
	self:StartGame()
end

function main_menu:OnButton_new_master_game()
	get_console():execute("g_game_difficulty gd_master")
	self:StartGame()
end

function main_menu:StartGame()
	local console = get_console()
	if alife() then
		event("disconnect"):trigger({act="start_game"}) --/#+#>
		console:execute	("disconnect")
	else
		event("game_load"):trigger({sav="new_game"}) --/#+#>
	end
	console:execute("start server(all/single/alife/new) client(localhost)")
	console:execute("main_menu off")
end

function main_menu:DisplayWnd(child)
	self:GetHolder():start_stop_menu(child, true)
	self:GetHolder():start_stop_menu(self, true)
	self:Show(false)
end

function main_menu:OnButton_options_clicked()
	if not self.opt_dlg then
		self.opt_dlg = ui_mm_opt_main.options_dialog()
		self.opt_dlg.owner = self
	end
	
	self.opt_dlg:UpdateControls()
	self:DisplayWnd(self.opt_dlg)
end

function main_menu:OnButton_save_clicked()
	if not self.save_dlg then
		self.save_dlg = ui_save_dialog.save_dialog()
		self.save_dlg.owner = self
	end
	
	self.save_dlg:reset()
	self:DisplayWnd(self.save_dlg)
end

function main_menu:OnButton_load_clicked()
	if not self.load_dlg then
		self.load_dlg = ui_load_dialog.load_dialog()
		self.load_dlg.owner = self
	end
	
	self.load_dlg:reset()
	self:DisplayWnd(self.load_dlg)
end

function main_menu:OnButton_multiplayer_clicked()
	if not self.mp_dlg then
		self.mp_dlg = ui_mp_main.mp_main()
		self.mp_dlg.owner = self
	end
	
	self.mp_dlg:UpdateControls()
	self:DisplayWnd(self.mp_dlg)
	
	--self.mp_dlg:OnBtn_Refresh()
	get_console():execute("check_for_updates 0")
	self.mp_dlg:OnRadio_NetChanged()
end

function main_menu:Dispatch(cmd, param)
	if cmd == 2 then
		self:OnButton_multiplayer_clicked()
	end
	return true
end

function main_menu:OnKeyboard(dik, keyboard_action)
	CUIScriptWnd.OnKeyboard(self, dik, keyboard_action)
	--local bind = dik_to_bind(dik)
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		local console = get_console()
		if dik == DIK_keys.DIK_Q then
			self:OnMessageQuitWin()
		elseif self:check_game() then --/#+# (only in game)
			if dik == DIK_keys.DIK_ESCAPE then
				self:OnButton_return_game()
			end
			--/< ----------------------------
		end
	end
	return true
end

function main_menu:check_game() --/#+#
	if level.present() and db.actor and db.actor:alive() then
		return true --/>
	end
	return false --/>
end

function main_menu:OnButton_options_la() --/#+# (доп.опции)
--[[
	if (ui_mm_opt_la) then
		if not self.opt_la_dlg then
			 self.opt_la_dlg = ui_mm_opt_la.get_ui_menu()
			 self.opt_la_dlg.owner = self
		end
		self:DisplayWnd(self.opt_la_dlg)
	end
]]
end

--/------------------------------------------------------------------
--/ For Tests&Debug
--/------------------------------------------------------------------
function main_menu:OnButton_load_spawn()
	if not self.spawn_dlg then
		self.spawn_dlg = ui_spawn_dialog.spawn_dialog() --/#?#
		self.spawn_dlg.owner = self
	end
	self:DisplayWnd(self.spawn_dlg)
end

function main_menu:OnButton_show_toolskit_menu() --/#+# (debug tools)
	if db.is_debug and (ui_hud_toolskit) then --/ and self:check_game()
		if not self.toolskit_dlg then
			self.toolskit_dlg = ui_hud_toolskit.get_ui_menu()
			self.toolskit_dlg.owner = self
		end
		self:DisplayWnd(self.toolskit_dlg)
	end
end

function main_menu:OnButton_show_spawn_menu() --/#+# (debug spawn)
	if db.is_debug and (ui_spawn_menu) then --/ and self:check_game()
		if not self.spaw_dlg then
			self.spawn_dlg = ui_spawn_menu.get_ui_menu()
			self.spawn_dlg.owner = self
		end
		self:DisplayWnd(self.spawn_dlg)
	end
end
--/ -----------------------------------------------------------------------------------------------
