-- File:        UI_SAVE_DIALOG.SCRIPT
-- Description: Save Dialog for STALKER
-- Created:     27.9.2004
-- Author:      Serhiy Vynnychenko (narrator@gsc-game.kiev.ua)
-- Copyright:   2004 GSC Game World
-- Version:     0.8

class "save_item" (CUIListItemEx)

function save_item:__init() super()
--	self:SetWndRect		(Frect():set(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		(Frect():set(0,0,280,22))
	self.fn:SetFont			(GetFontLetterica18Russian())
	self.fn:SetTextColor	(ca,cr,cg,cb)
	self.fn:SetElipsis		(2,2);

	self.fage				= CUIStatic	()
	self.fage:SetAutoDelete	(true)
	self:AttachChild		(self.fage)
--	self.fage:SetWndRect	(Frect():set(290,0,250+110,22))
	self.fage:SetFont		(GetFontLetterica16Russian())
	self.fage:SetTextColor	(ca,cr,cg,cb)
end


class "save_dialog" (CUIScriptWnd)

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

function save_dialog:__finalize()

end

function save_dialog:FillList()
	self.list_box:RemoveAll()
	local flist = getFS():file_list_open_ex("$game_saves$",FS.FS_ListFiles,"*.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 =  .. 
		self:AddItemToList(file_name, date_time)
	end
end

function save_dialog:InitControls()
	self:SetWndRect(Frect():set(0,0,1024,768))
	
	self.list_file_font = GetFontMedium()
	self.list_date_font = GetFontMedium()
	
	local xml = CScriptXmlInit()
	xml:ParseFile("ui_mm_save_dlg.xml")
	
	local ctrl
	
	xml:InitStatic					("background", self)

	ctrl							= CUIWindow()
	xml:InitWindow					("file_item:main",0,ctrl)
	
	self.file_item_main_sz			= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	xml:InitWindow					("file_item:fn",0,ctrl)
	self.file_item_fn_sz			= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())

	xml:InitWindow					("file_item:fd",0,ctrl)
	self.file_item_fd_sz			= vector2():set(ctrl:GetWidth(),ctrl:GetHeight())
	
	
		
	self.form = xml:InitStatic		("form", self)
	
	xml:InitStatic					("form:caption", self.form)
	
	ctrl = xml:InitEditBox			("form:edit", self.form)
	self:Register					(ctrl, "edit_filename")
	
	xml:InitFrame					("form:list_frame", self.form)

	self.list_box = xml:InitList	("form:list", self.form)
	self.list_box:ShowSelectedItem	(true)
	self:Register					(self.list_box, "list_window")
		
	ctrl = xml:Init3tButton			("form:btn_save", self.form)	
	self:Register					(ctrl, "button_ok")

	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_cancel")	
	
	self.message_box				= CUIMessageBoxEx()
	self:Register					(self.message_box,"message_box")

	self.mbox_mode					= 0	
end

function save_dialog:InitCallBacks()
    -- main frame buttons
	self:AddCallback("button_ok",     ui_events.BUTTON_CLICKED, 			self.OnButton_ok_clicked,		self)
	self:AddCallback("button_cancel", ui_events.BUTTON_CLICKED, 			self.OnButton_cancel_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("list_window", ui_events.LIST_ITEM_CLICKED, 			self.OnListItemClicked,			self)
end

function save_dialog:OnListItemClicked()
	if self.list_box:GetSize()==0 then return end

	local edit_box		= self:GetEditBox("edit_filename")
	local item_id		= self.list_box:GetFocusedItem()
	local item			= self.list_box:GetItem(item_id)
	
	if item==nil then return end
	
	local item_text		= item.fn:GetText()
	edit_box:SetText	(item_text)
end

function save_dialog:OnMsgYes()
	if self.mbox_mode == 1 then
		self:SaveFile(self.new_save)
		
		self:GetHolder():start_stop_menu	(self.owner, true) --new
		self:GetHolder():start_stop_menu	(self,true)
		self.owner:Show						(true)
		
	elseif self.mbox_mode == 2 then
		self:delete_selected_file()
	end	
end

function save_dialog:OnButton_del_clicked()
	if self.list_box:GetSize()==0 then return end

	local index		= self.list_box:GetSelectedItem	()
	
	if index == -1 then return end

	self.mbox_mode = 2
	self.message_box:InitMessageBox("message_box_delete_file_name")		
	self:GetHolder():start_stop_menu(self.message_box, true)
end

function save_dialog:delete_selected_file()
	if self.list_box:GetSize()==0 then return end
	local index		= self.list_box:GetSelectedItem	()
	
	if index == -1 then return end

	local item				= self.list_box:GetItem(index)
	local filename			= item.fn:GetText()
	
	ui_load_dialog.delete_save_game(filename)
	
	self.list_box:RemoveItem(index)
	self:OnListItemClicked	()
end

function save_dialog:OnButton_ok_clicked()
	-- prepare message box
	local message_box	= self.message_box
	local main_frame	= self:GetFrameWindow("main_frame")	
	
	-- Get file name
	local edit_box		= self:GetEditBox("edit_filename")
	self.new_save		= edit_box:GetText()
	
	-- check for empty name
	if string.len(self.new_save) == 0 then
		self.mbox_mode = 0
		self.message_box:InitMessageBox("message_box_empty_file_name")		
		self:GetHolder():start_stop_menu(self.message_box, true)
		return
	end
	
	-- check for match name
	local f = getFS()
	local flist = f:file_list_open("$game_saves$",FS.FS_ListFiles)
	local file_struct = f:exist("$game_saves$", self.new_save .. ".sav" )

	if file_struct ~= nil then
		self.mbox_mode = 1
		self.message_box:InitMessageBox("message_box_file_already_exist")
		self:GetHolder():start_stop_menu(self.message_box, true)
		
		flist:Free()	
		return
	end
	flist:Free()	
	self:SaveFile(self.new_save)

	self:GetHolder():start_stop_menu	(self.owner, true) --new
	self:GetHolder():start_stop_menu	(self,true)
	self.owner:Show						(true)
end

function save_dialog:OnButton_cancel_clicked()
	self:GetHolder():start_stop_menu	(self.owner, true) --new
	self:GetHolder():start_stop_menu	(self,true)
	self.owner:Show						(true)
end

function save_dialog:OnKeyboard(dik, keyboard_action)  --virtual function
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	local bind = dik_to_bind(dik)
	if bind == key_bindings.kQUIT then
		self:OnButton_cancel_clicked()
	else  

	   DIK_RETURN			= 28

		if dik == DIK_RETURN and keyboard_action == ui_events.WINDOW_KEY_PRESSED then
			self:OnButton_ok_clicked()
		end
   end
	return true
end

function save_dialog:AddItemToList(file_name, date_time)	
	
	local _itm				= save_item()

	_itm:SetWndSize			(self.file_item_main_sz)
	
	_itm.fn:SetWndPos		(vector2():set(0,0))
	_itm.fn:SetWndSize		(self.file_item_fn_sz)

	_itm.fage:SetWndPos		(vector2():set(self.file_item_fn_sz.x+4, 0))
	_itm.fage:SetWndSize	(self.file_item_fd_sz)

	_itm.fn:SetText			(file_name)
	_itm.fage:SetText		(date_time)
	self.list_box:AddItem	(_itm)	
end

function save_dialog:SaveFile(fileName)
	if nil~= fileName then
		local console = get_console()
		console:execute("save " .. fileName)
	end
end

