--/amk_rel_1/
-- File:        UI_MAIN_MENU.SCRIPT
-- Description: Load Dialog for STALKER
-- Created:     28.10.2004
-- Lasd edit:	18.01.2006
-- Copyright:   2004 GSC Game World
-- Author:      Serhiy Vynnychenko (narrator@gsc-game.kiev.ua)
-- Version:     0.9

class "main_menu" (CUIScriptWnd)

function main_menu:__init() super()
	self.mbox_mode = 0
   self:InitControls()
   self:InitCallBacks()
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")
     
    xml:InitStatic("back_movie", self)
    xml:InitStatic("background", self)
   -- xml:InitStatic("fire_movie", self)
    self.shniaga = xml:InitMMShniaga("shniaga_wnd",self);
	
	self.message_box = CUIMessageBoxEx()
	self:Register(self.message_box, "msg_box")	
	
	local _ver = xml:InitStatic		("static_version",self)
	local mm						= _G.main_menu.get_main_menu()
	_ver:SetText					("ver. " .. mm:GetGSVer())
	_ver = xml:InitStatic("mod_version",self)
	local txt = meceniy_utils.read_mod_ver("txt")
	_ver:SetText(txt)
	_ver:SetTextColor(170,100,160,50)
--	local amk_version    	= amk_ver.amk_version
--	local amk_revision	= amk_ver.amk_revision
--	local debug_mode = ""
--	_ver:SetText("Версия игры - " .. mm:GetGSVer() .. "  " .. amk_title .. " " .. amk_version .. " " .. amk_revision)
end

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


function main_menu:InitCallBacks()
	-- 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)
	self:AddCallback("btn_spawn",		ui_events.BUTTON_CLICKED,				self.OnButton_load_spawn,			self)
	-- options																											
	self:AddCallback("btn_options",     ui_events.BUTTON_CLICKED,				self.OnButton_options_clicked,		self)
	-- load																												
	self:AddCallback("btn_load",        ui_events.BUTTON_CLICKED,				self.OnButton_load_clicked,			self)
	-- save																												
	self:AddCallback("btn_save",		ui_events.BUTTON_CLICKED,				self.OnButton_save_clicked,			self)
	-- multiplayer																										
	self:AddCallback("btn_multiplayer", ui_events.BUTTON_CLICKED,				self.OnButton_multiplayer_clicked,	self)
	-- spawns																										
	-- self:AddCallback("btn_spawns",		 ui_events.BUTTON_CLICKED,				self.OnButton_spawns_clicked,		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)
	self:AddCallback("btn_ret",			ui_events.BUTTON_CLICKED,				self.OnButton_return_game,			self)
	self:AddCallback("btn_lastsave",	ui_events.BUTTON_CLICKED,				self.OnButton_last_save,			self)
	self:AddCallback("btn_credits",		ui_events.BUTTON_CLICKED,				self.OnButton_credits_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)
	--amk
	self:AddCallback("btn_amk",		ui_events.BUTTON_CLICKED,				self.amk_options,		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					console = get_console()
	console:execute			("main_menu off")
	console:execute			("load_last_save")
end

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

function main_menu:OnButton_credits_clicked()
--	local console = get_console()
--	console:execute("main_menu off")
	game.start_tutorial("credits_seq")
end

function main_menu:OnButton_quit_clicked()
	self.message_box:Init("message_box_quit_windows")
	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
	end
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function main_menu:OnMessageQuitGame()
	local console = get_console()
	console:execute("disconnect")
end

function main_menu:OnMessageQuitWin()
	local console = get_console()
	console:execute("quit")
end

function main_menu:OnButton_return_game()
	local console = get_console()
	console:execute("main_menu off")
end

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

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

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

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

function main_menu:StartGame()
	local console = get_console()
	
	if (alife() ~= nil) then
		console:execute	("disconnect")	
	end
	
	console:execute("start server(all/single/alife/new) client(localhost)")
	console:execute("main_menu off")
end

function main_menu:OnButton_load_spawn()
	if self.spawn_dlg == nil then
		self.spawn_dlg = ui_spawn_dialog.spawn_dialog()
		self.spawn_dlg.owner = self
	end
	
	self:GetHolder():start_stop_menu(self.spawn_dlg, true)
	self:GetHolder():start_stop_menu(self, true) --new
	self:Show(false)
end

function main_menu:OnButton_save_clicked()
	if self.save_dlg == nil then
		self.save_dlg = ui_save_dialog.save_dialog()
		self.save_dlg.owner = self
	end
	
	self:GetHolder():start_stop_menu(self.save_dlg, true)
	self:GetHolder():start_stop_menu(self, true) --new
	self:Show(false)
end

function main_menu:OnButton_options_clicked()
	if self.opt_dlg == nil then
		self.opt_dlg = ui_mm_opt_main.options_dialog()
		self.opt_dlg.owner = self
	end
	
	self.opt_dlg:UpdateControls()
	self:GetHolder():start_stop_menu(self.opt_dlg, true)
	self:GetHolder():start_stop_menu(self, true) --new
	self:Show(false)
  amk_mod.hud_update=true
end

function main_menu:OnButton_load_clicked()
	if self.load_dlg ==nil then
		self.load_dlg = ui_load_dialog.load_dialog()
		self.load_dlg.owner = self
	end
	
	self:GetHolder():start_stop_menu(self.load_dlg, true)
	self:GetHolder():start_stop_menu(self, true) --new
	self:Show(false)
end

function main_menu:OnButton_multiplayer_clicked()
	if self.mp_dlg ==nil then
		self.mp_dlg = ui_mp_main.mp_main()
		self.mp_dlg.owner = self
	end
	self.mp_dlg:UpdateControls()
	self:GetHolder():start_stop_menu(self.mp_dlg, true)
	self:GetHolder():start_stop_menu(self, true) --new
	self:Show(false)
	
--	self.mp_dlg:OnBtn_Refresh()
	
	local console			= get_console()
	console:execute			("check_for_updates 0")
	
	self.mp_dlg:OnRadio_NetChanged()
end

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

function main_menu:OnButton_spawns_clicked()
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		-- get_console():execute("main_menu off")
		if self.spawn_dlg == nil then
			self.spawn_dlg = ui_si.spawn_item_dialog()
			self.spawn_dlg.owner = self
		end

		self:GetHolder():start_stop_menu(self.spawn_dlg, true)

		self:Show(false)
	end
end

function main_menu:OnButton_trans_spawns_clicked()
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		-- get_console():execute("main_menu off")
		if self.spawn_dlg == nil then
			self.spawn_dlg = trans_ui_si.spawn_item_dialog()
			self.spawn_dlg.owner = self
		end

		self:GetHolder():start_stop_menu(self.spawn_dlg, true)

		self:Show(false)
	end
end
function main_menu:cheat_spawner(action)
  if level.present() and (db.actor ~= nil) and db.actor:alive() then
    get_console():execute("main_menu off")
    local spwn = ui_cheat_spawner.spawner(get_hud(),action)
    level.start_stop_menu(spwn,true)
  end
end

function main_menu:tele()
  if level.present() and (db.actor ~= nil) and db.actor:alive() then
    get_console():execute("main_menu off")
    local spwn = ui_teleport.ui_ms_teleport(get_hud())
    level.start_stop_menu(spwn,true)
  end

end

function main_menu:to_community()

  if level.present() and (db.actor ~= nil) and db.actor:alive() then
    get_console():execute("main_menu off")
    local spwn = ui_comm.ui_ms_community(get_hud(),action)
    level.start_stop_menu(spwn,true)
  end

end

function main_menu:setweather()

  if level.present() and (db.actor ~= nil) and db.actor:alive() then
    get_console():execute("main_menu off")
    local spwn = ui_weather.ui_ms_weather(get_hud(),action)
    level.start_stop_menu(spwn,true)
  end

end


function main_menu:OnKeyboard(dik, keyboard_action)  --virtual function
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	local bind = dik_to_bind(dik)
	local console = get_console()
	
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		if dik == DIK_keys.DIK_ESCAPE then
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
				console:execute("main_menu off")
			end
		end
		
		if dik == DIK_keys.DIK_U then
			self.tele_dlg = ui_teleport1.dots()
			self.tele_dlg.owner = self
       		self:GetHolder():start_stop_menu(self.tele_dlg, true)
			self:Show(false)
		end
		
		-- раскоментируем и заоодно включим убранное спавн меню 
		-- if	dik == DIK_keys.DIK_S then
		--	self:OnButton_load_spawn()
		-- end
		if	dik == DIK_keys.DIK_S then
			self:OnButton_spawns_clicked()
		end
		if	dik == DIK_keys.DIK_O then
			self:OnButton_trans_spawns_clicked()
		end
		if 	dik == DIK_keys.DIK_Q then
			self:OnMessageQuitWin()
		--elseif dik == DIK_keys.DIK_M then self:matants()	
		--elseif dik == DIK_keys.DIK_Z then self:to_community()
		end
		if 	dik == DIK_keys.DIK_A then
			if amk.check_game() then 
				self:amk_options()
			end
		end
		if 	dik == DIK_keys.DIK_F1 then
			ui_cheat_spawner.show_actor_position()
		end
		if 	dik == DIK_keys.DIK_F2 then
			-- i_cheat_spawner.deadman_to_life()
			self:cheat_spawner("find")
		end
		if 	dik == DIK_keys.DIK_F3 then
			-- self:cheat_spawner("info")
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
				anom_remover.remove_anoms()
			end
		end
		if 	dik == DIK_keys.DIK_F4 then
			self:cheat_spawner("spawn")
		end
		if 	dik == DIK_keys.DIK_F5 then
			self:cheat_spawner("remove")
		end
		if 	dik == DIK_keys.DIK_F9 then
			self:cheat_spawner("spawn2inv")
		end
		if 	dik == DIK_keys.DIK_F10 then
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
				ui_cheat_spawner.deadman_to_life()
			end
		end
		if 	dik == DIK_keys.DIK_F11 then
			self:cheat_spawner("set_npc_goodwill")
		end
		if dik == DIK_keys.DIK_F6 then
			local res, err = pcall(my_proxy.start_chunk_from_file)
			get_console():execute(res and "Succesfull!" or string.gsub(err, " ", "_")) --имитация тернарного оператора
		end
		if dik == DIK_keys.DIK_P then
			self:start_atlas("spawn")
		end
		if dik == DIK_keys.DIK_F7 then
			self:start_atlas2("spawn")
		end
		if dik == DIK_keys.DIK_F8 then
			self:start_atlas3("spawn")
		end
		if dik ==DIK_keys.DIK_W then
			self:setweather()			
		end
		if dik ==DIK_keys.DIK_R then
			self:to_community()			
		end
		if dik ==DIK_keys.DIK_T then
			self:tele()			
		end
        god.key_binder(dik, keyboard_action, self)	
	end
   
	return true
end

function main_menu:amk_options()
-- если еще ни разу не вызывали, то обозначим наш новый скрипт
	if self.amk_options_dlg == nil then
		self.amk_options_dlg = ui_amk_options.amk_options()
		self.amk_options_dlg.owner = self
	end
-- останавливаем родительский элемент, запускаем свой, но родителя показываем.
	self:GetHolder():start_stop_menu(self.amk_options_dlg, true)
	self:GetHolder():start_stop_menu(self, true)
	self:Show(false)

end

function main_menu:matants()
mutant_manager.effector_spa()
end
--[[function main_menu:to_community()
db.actor:give_info_portion("val_bandit_talk") 
db.actor:set_character_community("stranger", 0, 0)
end]]
function main_menu:start_atlas(action)
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		get_console():execute("main_menu off")
    	local target = ui_atlas.atlas(get_hud(),action)
    	level.start_stop_menu(target, true)
	end
end

function main_menu:start_atlas2(action)
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		get_console():execute("main_menu off")
    	local target = ui_atlas2.atlas(get_hud(),action)
    	level.start_stop_menu(target, true)
	end
end

function main_menu:start_atlas3(action)
	if level.present() and (db.actor ~= nil) and db.actor:alive() then
		get_console():execute("main_menu off")
		local target = ui_atlas3.atlas(get_hud(),action)
		level.start_stop_menu(target, true)
	end
end
