-- File:        UI_LOAD_DIALOG.SCRIPT
-- Description: Load Dialog for STALKER
-- Created:     28.10.2004
-- Copyright:   2004 GSC Game World
-- Author:      Serhiy Vynnychenko (narrator@gsc-game.kiev.ua)
-- Version:     0.5

class "load_item" (CUIListItemEx)

function load_item:__init() super()
	self.file_name		= "filename"
	self:SetWndRect		(0,0,430,22)
	
	local ca = 255
	local cr = 216
	local cg = 186
	local cb = 140
	
	self.fn					= CUIStatic	()
	self.fn:SetAutoDelete	(true)
	self:AttachChild		(self.fn)
	self.fn:SetWndRect		(0,0,230,22)
	self.fn:SetText			("filename")
	self.fn:SetFont			(GetFontLetterica18Russian())
	self.fn:SetTextColor	(ca,cr,cg,cb)

	self.fage				= CUIStatic	()
	self.fage:SetAutoDelete	(true)
	self:AttachChild		(self.fage)
	self.fage:SetWndRect	(250,0,150,22)
	self.fage:SetText		("fileage")
	self.fage:SetFont		(GetFontLetterica16Russian())
	self.fage:SetTextColor	(ca,cr,cg,cb)
end


class "load_dialog" (CUIScriptWnd)

function load_dialog:__init() super()
   self:InitControls()
   self:InitCallBacks()
   self:FillList()
end

function load_dialog:__finalize()

end

function load_dialog:FillList()

	local f = getFS()
	local flist = f:file_list_open_ex("$game_saves$",bit_or(FS.FS_ListFiles,FS.FS_RootOnly),"*.sav")
	local f_cnt = flist:Size()
	
	flist:Sort(FS.FS_sort_by_modif_down)
	
	for	it=0, f_cnt-1 	do
		local file		=	flist:GetAt(it)			
		local file_name = string.sub(file:NameFull(), 0, (string.len(file:NameFull()) - 4))
		local date_time = "[" .. file:ModifDigitOnly() .. "]"
		--menu_item =  .. 
		if file_name ~= "abcmod_auto_save" then
		self:AddItemToList(file_name, date_time)
		end
	end
	
end

function load_dialog:InitControls()
	self:Init(0,0,1024,768)
	
	local xml			 = CScriptXmlInit()
	local ctrl
	xml:ParseFile("ui_mm_load_dlg.xml")
	
	xml:InitStatic("back_video", self)
	
	xml:InitStatic("background",				self)	
	xml:InitStatic("newspaper_video",			self)	

	self.form = xml:InitStatic("form",			self)
	
	xml:InitStatic("form:caption",				self.form)
	
	ctrl = xml:InitStatic("form:picture",		self.form)
	ctrl:SetWindowName("static_pict")	
	
	xml:InitStatic("form:file_info",			self.form)
	
	self.file_caption 	= xml:InitStatic("form:file_caption",	self.form)
	self.file_data		= xml:InitStatic("form:file_data",		self.form)
	
	xml:InitFrame("form:list_frame",			self.form)

	ctrl = xml:InitList("form:list", 			self.form)
	ctrl:ShowSelectedItem(true)
	self:Register(ctrl, "list_window")
		
	ctrl = xml:Init3tButton("form:btn_load",	self.form)	
	self:Register(ctrl, "button_load")
	
	ctrl = xml:Init3tButton("form:btn_delete",	self.form)	
	self:Register(ctrl, "button_del")
	
	ctrl = xml:Init3tButton("form:btn_cancel",	self.form)
	self:Register(ctrl, "button_back")	
	
	self.message_box = CUIMessageBoxEx()
	self:Register(self.message_box,"message_box")
end

function load_dialog:InitCallBacks()
	self:AddCallback("button_load",		ui_events.BUTTON_CLICKED,             self.OnButton_load_clicked,	self)
	self:AddCallback("button_back",		ui_events.BUTTON_CLICKED,             self.OnButton_back_clicked,	self)
	self:AddCallback("button_del",		ui_events.BUTTON_CLICKED,             self.OnButton_del_clicked,	self)
	self:AddCallback("message_box",		ui_events.MESSAGE_BOX_YES_CLICKED,    self.OnMsgYes,				self)
	self:AddCallback("message_box",		ui_events.MESSAGE_BOX_OK_CLICKED,	  self.OnMsgYes,				self)

	self:AddCallback("list_window", ui_events.LIST_ITEM_CLICKED, 			  self.OnListItemClicked,		self)
	self:AddCallback("list_window", ui_events.WINDOW_LBUTTON_DB_CLICK,		  self.OnListItemDbClicked,		self)
	
	
end

function file_exist(fname)
	local f = getFS();
	local flist = f:file_list_open_ex("$game_saves$",bit_or(FS.FS_ListFiles,FS.FS_RootOnly) , fname)
	local f_cnt = flist:Size()
	
	if f_cnt > 0 then
		return true
	else
		return false
	end	
end

function delete_save_game(filename)

	local save_file		= filename .. ".sav"
	local dds_file		= filename .. ".dds"
	
	local f = getFS()
	
	f:file_delete("$game_saves$",save_file)
	
	if file_exist(dds_file) then
		f:file_delete("$game_saves$", dds_file)
	end

end

function AddTimeDigit(str, dig)
	if (dig > 9) then
		str = str .. dig
	else
		str = str .. "0" .. dig
	end
	
	return str
	
end

function file_data(fname)
	local f = getFS();
	local flist = f:file_list_open_ex("$game_saves$",bit_or(FS.FS_ListFiles,FS.FS_RootOnly) , fname .. ".sav")
	local f_cnt = flist:Size()
	
	if f_cnt > 0 then
		local file		=	flist:GetAt(0)
		local sg = CSavedGameWrapper(fname)
		
		local y,m,d,h,min,sec,ms = 0,0,0,0,0,0,0
		y,m,d,h,min,sec,ms = sg:game_time():get(y,m,d,h,min,sec,ms)
		
		
		local date_time = ""
		
		date_time = AddTimeDigit(date_time, h)
		date_time = date_time .. ":"
		date_time = AddTimeDigit(date_time, min)		
		date_time = date_time .. " "
		date_time = AddTimeDigit(date_time, m)
		date_time = date_time .. "/"
		date_time = AddTimeDigit(date_time, d)
		date_time = date_time .. "/"
		
		date_time = date_time .. y
		
		
		--string.format("[%d:%d:%d %d]",m,d,h,min,y)
		local health = string.format("\\n%s %d%s", game.translate_string("ui_inv_health"),sg:actor_health()*100,"%")
		
		return game.translate_string("st_level") .. ": " .. game.translate_string(sg:level_name()) .. "\\n" .. game.translate_string("ui_inv_time")..": " .. date_time .. health
	else
		return "no file data"
	end	
end

function load_dialog:OnListItemClicked()
	local list_box		= self:GetListWnd("list_window")
	
	if list_box:GetSize()==0 then return end
	
	local picture		= self:GetStatic("static_pict")	
	
	local itm_index		= list_box:GetSelectedItem()
	
	local item			= list_box:GetItem(itm_index)

	if item == nil then
		self.file_caption:SetText	("")
		self.file_data:SetText		("")
		picture:InitTexture			("ui\\ui_noise")
		return
	end
	
	local item_text				= item.fn:GetText()
	self.file_caption:SetText	(item_text)
	self.file_data:SetText		(file_data(item_text))
	
	if file_exist(item_text .. ".sav") ~= true then
		list_box:RemoveItem(itm_index)
		return
	end
	
	if file_exist(item_text .. ".dds") then
		picture:InitTexture(item_text)
	else
		picture:InitTexture("ui\\ui_noise")
	end
end

function load_dialog:OnListItemDbClicked()
	self:OnButton_load_clicked()
end

function load_dialog:OnMsgYes()
	local list = self:GetListWnd("list_window")	
	local index = list:GetSelectedItem()
	
	if index == -1 then return end

	if self.msgbox_id == 1 then
		local item  = list:GetItem(index)
		local fname = item.fn:GetText()
		
		delete_save_game	(fname)

		list:RemoveItem		(index)
		
		self:OnListItemClicked()
	elseif self.msgbox_id == 2 then
		self:load_game_internal()
	end
	
	self.msgbox_id = 0
end

function load_dialog:load_game_internal()	
	local console = get_console()
	local list = self:GetListWnd("list_window")	

	if list:GetSize()==0 then return end

	local index = list:GetSelectedItem()
	
	if index == -1 then return end
		
	local item  = list:GetItem(index)
	local fname = item.fn:GetText()
	
	if (alife() == nil) then
		-- alterei
		if fname ~= "abcmod_auto_save.sav" then
		console:execute	("disconnect")	
		console:execute	("start server(" .. fname .. "/single/alife/load) client(localhost)")
		end
		db.loadedfmenu = "yup"
	else
		if fname ~= "abcmod_auto_save.sav" then
		console:execute	("load " .. fname)	
		end
		db.loadedfmenu = "yup"
	end
end

function load_dialog:OnButton_load_clicked()	
	local console = get_console()
	local list = self:GetListWnd("list_window")	

	if list:GetSize()==0 then return end

	local index = list:GetSelectedItem()
	
	if index == -1 then return end

	local item		= list:GetItem(index)
	local fname		= item.fn:GetText()

	if (alife() == nil) then

		if valid_saved_game(fname) then
			self:load_game_internal()
		else
			self.msgbox_id			= 0
			self.message_box:Init			("message_box_invalid_saved_game")		
			self:GetHolder():start_stop_menu(self.message_box, true)
		end

		return
	end

	if valid_saved_game(fname) then
		self.msgbox_id = 2
		self.message_box:Init("message_box_confirm_load_save")		
		self:GetHolder():start_stop_menu(self.message_box, true)
	else
		self.msgbox_id			= 0
		self.message_box:Init			("message_box_invalid_saved_game")		
		self:GetHolder():start_stop_menu(self.message_box, true)
	end
	
end

function load_dialog:OnButton_back_clicked()
	self:GetHolder():start_stop_menu	(self.owner, true) --new(show main window)
	self:GetHolder():start_stop_menu	(self,true)
	self.owner:Show						(true)
end

function load_dialog:OnButton_del_clicked()
	local list = self:GetListWnd("list_window")	

	if list:GetSize()==0 then return end
	local index = list:GetSelectedItem()
	
	if index == -1 then return end
	
	self.msgbox_id = 1
	
	self.message_box:Init("message_box_delete_file_name")		
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function load_dialog:OnKeyboard(dik, keyboard_action)  --virtual function
   ui_base_dialog.base_dialog.OnKeyboard(self,dik,keyboard_action)
	
   DIK_RETURN = 28
   WINDOW_KEY_PRESSED = 6

	if dik == DIK_RETURN and keyboard_action == WINDOW_KEY_PRESSED then
		self:GetHolder():start_stop_menu	(self.owner, true) --new
		self:GetHolder():start_stop_menu	(self,true)
		self.owner:Show						(true)
	end
   
	return true
end

function load_dialog:AddItemToList(file_name, date_time)
	local _itm				= load_item()
	_itm.fn:SetText			(file_name)
	_itm.fage:SetText		(date_time)
	
	local list_box			= self:GetListWnd("list_window")
	list_box:AddItem		(_itm)
end



