--------------------------------------------------------------------------------
--  File     : _chapter.lua
--  Author(s): Yakutin Sergey, Prokopovich Larisa
--
--  Summary  : Скрипт управления главой.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
--dofile("_load.lua")

--------------------------------------------------------------------------------
-- Текстовые константы диалогов
--------------------------------------------------------------------------------
DLGID_JOURNAL	= "Journal"
DLGID_INGMENU	= "InGameMenu"
DLGID_PERSCMNT	= "PersComment"
DLGID_LOADING	= "LoadingFast"
DLGID_GAMEPANEL	= "GamePanel"
DLGID_OPTIONS	= "DialogOptions"

--------------------------------------------------------------------------------
function OnAutoSave()		--вызов из CGameChapter
	qe:SaveGameToProfile()
end
--------------------------------------------------------------------------------
function SetAmbientsVolMultiply(val)
	local dlg_options	= ui:GetDialog(DLGID_OPTIONS)
	dlg_options:ChangeAmbientVolMult(val)
end
--------------------------------------------------------------------------------
function RestoreAmbientsVol()
	local dlg_options	= ui:GetDialog(DLGID_OPTIONS)
	dlg_options:RestoreAmbientVol()
end
--------------------------------------------------------------------------------
function OnStart()
	if gstore["IntroVideo"] then
		chapter:JumpRoom("Trunk")
	else
		chapter:AddRoomToLoadTask("Trunk")
		chapter:StartLoadingRooms()
	end

	--Инициализация глобальных переменных
	cstore:SetKey("Soldier1", 0)
	cstore:SetKey("Soldier2", 0)
	cstore:SetKey("Skeletons", 0)
	
	cstore:SetKey("Shelf_HO", 0)
	cstore:SetKey("Hollow_HO", 0)
	cstore:SetKey("Boat_HO", 0)
	cstore:SetKey("Garbage_HO", 0)
	cstore:SetKey("Cupboard_HO", 0)
	cstore:SetKey("Tools_HO", 0)
	cstore:SetKey("Cache_HO", 0)
	
	ui:GetDialog(DLGID_OPTIONS):ResetAmbientVol()
end

--------------------------------------------------------------------------------
function Update(elapsedTime)
end

--------------------------------------------------------------------------------
-- Событие старта и окончания загрузки локаций
--------------------------------------------------------------------------------
function OnStartLoadRooms()
	chapter:CompletePickAnimations()
	ui:CloseDialog("PersSpeakTop")
--	ui:CloseDialog(DLGID_GAMEPANEL)
--	ui:CloseDialog(DLGID_PERSCMNT)
	ui:ShowDialog(DLGID_LOADING)
end

function OnEndLoadRooms()
	ui:CloseDialog(DLGID_LOADING)

	if gstore["IntroVideo"] == nil then
		ambient:Stop()
		music:Stop(true)
		
		dialog = ui:GetDialog("VideoPlayer")
		dialog:SetUserTag("")
		dialog:PlayVideo("cut_scene_1", 0, 0);
		ui:CloseDialog(DLGID_GAMEPANEL)
		gstore["IntroVideo"] = true
	else
		ui:ShowDialog(DLGID_GAMEPANEL)
	end
end
-- Переход после интро видео
function OnDialogClose_VideoPlayer(dialog)
	ui:ShowDialog(DLGID_GAMEPANEL)
	chapter:JumpRoom("Trunk")
end

--------------------------------------------------------------------------------
-- Событие модификации ключа в хранилище данных главы
--------------------------------------------------------------------------------
function OnChapterStoreEvent(key, value)
end

--------------------------------------------------------------------------------
-- События диалогов
--------------------------------------------------------------------------------

function OnDialogShow(dialog)
	-- Установка паузы
	local dlg_name = dialog:GetName();
	if dlg_name == DLGID_JOURNAL or dlg_name == DLGID_INGMENU then
		chapter:CompletePickAnimations()
		chapter:SetScenePause(true)
		SetAmbientsVolMultiply(0.5)
	end
end

--------------------------------------------------------------------------------
function OnDialogClose(dialog)
	-- Снятие паузы
	local dlg_name = dialog:GetName();
	if dlg_name == DLGID_JOURNAL or dlg_name == DLGID_INGMENU then
		chapter:SetScenePause(false)
		RestoreAmbientsVol()
	end
end

function OnDialogClose_IntroText(dialog)
	if (dialog:GetUserTag() == "TheEnd") then
		dialog:SetUserTag("")
		ui:CloseDialog("GamePanel")
		chapter:MarkChapterCompleted()
		--ui:ShowDialog("TheEnd")
	end
end

--------------------------------------------------------------------------------
function OnJumpCloseup(name)
	SetAmbientsVolMultiply(0.5)
--	local dialog = ui:GetDialog(DLGID_PERSCMNT)
--	dialog:ClearLastTextID()
--	dialog:Close()
end

--------------------------------------------------------------------------------
function OnReturnFromCloseup(closeupName)
	RestoreAmbientsVol()
end

--------------------------------------------------------------------------------
function OnExitRoom()
--	local dialog = ui:GetDialog(DLGID_PERSCMNT)
--	dialog:ClearLastTextID()
--	dialog:Close()
end
--------------------------------------------------------------------------------