------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- функции для работы с диалгоами
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function base_dialog_generic_handler(window, action, widget)
	standard_generic_handler(window, action, widget)
	if action=="pressed" then 
		if widget:GetId()=="left" then window:ReturnResult(-1) 
		elseif widget:GetId()=="right" then window:ReturnResult(1) 
		elseif widget:GetId()=="center" then window:ReturnResult(0) end
		window:Close()
	end
end

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function create_dialog(base, dialog_template)
	local dialog = CLuaWindow(g_Loader:GetTemplate(dialog_template), g_Lua)

	local delta = 20
	local xdelta = 40
	local y = delta

	local width = dialog:GetRect().mWidth

	-- подстраиваем ширину окна под кнопки
	local left = ConvertToCButtonWidget(dialog:GetWidget("left"))
	local right = ConvertToCButtonWidget(dialog:GetWidget("right"))
	local center = ConvertToCButtonWidget(dialog:GetWidget("center"))

	if (not left and right 
	or left and not right 
	or not left and not right and not center) then
		error("Dialog "..dialog_template.." have wrong set of buttons!")
	end

	local need_width = width
	local button_width = -1
	if center then
		button_width = center:GetRect().mWidth
		need_width = button_width + 2*xdelta
	end
	if left and right then
		if button_width==-1 or left:GetRect().mWidth > button_width then button_width = left:GetRect().mWidth end
		if right:GetRect().mWidth > button_width then button_width = right:GetRect().mWidth end
		need_width = 2*button_width + 3*xdelta
	end
	if width<need_width then
		width = need_width
	end

	-- подстраиваем ширину окна под заголовок
	local w = dialog:GetWidget("title")
	if w then
		local title_width = w:GetRect().mWidth + xdelta*2
		if title_width > width then
			width = title_width
		end
	end
	dialog:Resize(width, dialog:GetRect().mHeight)

	-- расставляем виджеты в диалоге
	local w = dialog:GetWidget("title")
	if w then
		w:MoveTo(xdelta, y)
		w:Resize(dialog:GetRect().mWidth - 2*xdelta, w:GetRect().mHeight)
		y = y + w:GetRect().mHeight + delta
	end
	local w = dialog:GetWidget("text")
	if w then
		w:MoveTo(xdelta, y)
		w:Resize(dialog:GetRect().mWidth - 2*xdelta, w:GetRect().mHeight)
		y = y + w:GetRect().mHeight + delta
	end
	
	if left then
		left:Resize(button_width, left:GetRect().mHeight)
		left:MoveTo(xdelta, y)
	end
	
	if right then
		right:Resize(button_width, right:GetRect().mHeight)
		right:MoveTo(dialog:GetRect().mWidth - xdelta - button_width, y)
		y = y + right:GetRect().mHeight
		if center then y = y + delta end
	end

	if center then
		center:Resize(button_width, center:GetRect().mHeight)
		center:MoveTo((dialog:GetRect().mWidth - button_width)/2, y)
		y = y + center:GetRect().mHeight
	end
	y = y + xdelta
	dialog:Resize(dialog:GetRect().mWidth, y)

	dialog:MoveTo( (base:GetRect().mWidth - dialog:GetRect().mWidth)/2, (base:GetRect().mHeight - dialog:GetRect().mHeight)/2 )

	base:AddWidget(dialog)
	return dialog
end

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function dialog_do_modal(base, dialog_template)
	local dialog = create_dialog(base, dialog_template)
	return dialog:WaitForResult(false)
end

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

function connecting_dialog_update(window, dt)
	local res =records_manager():IsUpdateComplete()
	if res~=CRecords.not_completed then
		if res==CRecords.complete_success then 
			window:ReturnResult(1) 
			window:Close()
		else 
			window:ReturnResult(-1) 
			window:Close()
		end
		
	end
end