download = {}

download.app = sf.core.g_Application
	
download.strings = {}
download.strings[0] = sf.misc.g_StringTable.Instance().FormatByStringsID(_T("$559"))
download.strings[1] = _T("%d%% - %s/%s")

download.enum = {}

download.enum.connection_failed = 0
download.enum.transfer_failed = 1
download.enum.paused = 2
download.enum.cancelled = 3
download.enum.successful = 4

download.size = 0
download.size_postfix = ""

download.size_get_string = function(_size, _c)

	local div = 1
	
	if _c == "M" then
		div = 1024*1024
	elseif _c == "K" then
		div = 1024
	end
	
	return (string.format("%.2f%s", _size/div, _c))
end

download.http_request = function(_owner, _params)

	--[[

	_params = 
	{
		dialog_main,
		dialog_connection_error,
		dialog_transfer_error,
		dialog_cancel,
		url,
		outfile,
		func_prepare_dialog_main,
		func_prepare_dialog_cancel
	}

	]]
	
	if _params.size ~= nil then 
		download.size = _params.size 
		if download.size > 1024 then download.size_postfix="K" end
		if download.size > 1024*1024 then download.size_postfix="M" end
	end
	
	local http_client = sf.gui.CreateHTTPClient(_T("http_client"), s4.g_GameOptions.Instance().GetUseIESettings())
	local http_client_ptr1 = sf.gui.CWidgetPtrT(http_client)

	local result = download.enum.connection_failed
	
	while result ~= download.enum.successful and result ~= download.enum.cancelled do
	
		http_client.Get(_params.url, _params.outfile, true)	
		result = download.enum.paused
		
		while result == download.enum.paused do

			local dialog_main = sf.gui.CWindow()
			game.load_window(_owner, dialog_main, _params.dialog_main)
			local dialog_main_ptr = sf.gui.CBaseWindowPtrT(dialog_main)	
			
			if _params.func_prepare_dialog_main then 
				_params.func_prepare_dialog_main(dialog_main) 
				_owner.UpdateLayout()
			end
			
			http_client.RemFlags(sf.gui.CBaseWidget.FlagDead)
			local http_client_ptr2 = sf.gui.CWidgetPtrT(http_client)
			dialog_main.AddWidget(http_client_ptr2)	
			
			result = _owner.DoModal(dialog_main_ptr)
			
			if result == download.enum.paused then
								
				local dialog_cancel = sf.gui.CWindow()
				game.load_window(_owner, dialog_cancel, _params.dialog_cancel)
				local dialog_cancel_ptr = sf.gui.CBaseWindowPtrT(dialog_cancel)
				
				if _params.func_prepare_dialog_cancel then 
					_params.func_prepare_dialog_cancel(dialog_cancel) 
					_owner.UpdateLayout()
				end
				
				if _owner.DoModal(dialog_cancel) == 1 then
					
					http_client.Abort()
					result = download.enum.cancelled				
				
				else
					http_client.Pause(false)
				end
			end
		end
		
		if result == download.enum.connection_failed then
			
			local dialog_connection_error = sf.gui.CWindow()
			game.load_window(_owner, dialog_connection_error, _params.dialog_connection_error)
			local dialog_connection_error_ptr = sf.gui.CBaseWindowPtrT(dialog_connection_error)
			
			if _owner.DoModal(dialog_connection_error_ptr) == 1 then break end
	
		elseif result == download.enum.transfer_failed then
		
			local dialog_transfer_error = sf.gui.CWindow()
			game.load_window(_owner, dialog_transfer_error, _params.dialog_transfer_error)
			local dialog_transfer_error_ptr = sf.gui.CBaseWindowPtrT(dialog_transfer_error)
			
			if _owner.DoModal(dialog_transfer_error_ptr) == 1 then break end
			
		end
		
	end
	
	return result

end

download.on_child_action = {}

download.on_child_action.error = function(_owner, _action, _child)

	if _action == _T("pressed") then
		if _child.GetId() == _T("cancel") then _owner.EndModal(1) end
		if _child.GetId() == _T("continue") then _owner.EndModal(0) end
	end

end

download.on_child_action.cancel = function(_owner, _action, _child)

	if _action == _T("pressed") then
		if _child.GetId() == _T("no") then _owner.EndModal(0) end
		if _child.GetId() == _T("yes") then _owner.EndModal(1) end
	end

end

download.on_child_action.complete = function(_owner, _action, _child)

	if _action == _T("pressed") then
		if _child.GetId() == _T("ok") then _owner.EndModal(1) end
	end

end

download.on_child_action.dialog_download = function(_owner, _action, _child)

	if _action==_T("pressed") and _child.GetId()==_T("cancel") then
				
		__cast(_owner.GetWidget(_T("http_client")), sf.gui.CBaseHTTPClient).Pause(true)	
		_owner.EndModal(download.enum.paused)

	elseif _action==_T("changed") and _child.GetId()==_T("http_client") then
		
		local label_progress = __cast(_owner.GetWidget(_T("label_progress")), sf.gui.CLabelWidget);
		local progress_bar = __cast(_owner.GetWidget(_T("progress")), sf.gui.CProgressBarWidget);
		local http_client = __cast(_child, sf.gui.CBaseHTTPClient)
		local state = http_client.GetState()
		
		local progress_text = _T("")
		
		if download.size == 0 then
			progress_text = _T(string.format(T_(download.strings[0]).c_str(), 100 * http_client.GetProgress()))
		else
			local current_size = http_client.GetProgress()*download.size
			progress_text = _T(string.format(T_(download.strings[1]).c_str(), 100 * http_client.GetProgress(),
					download.size_get_string(current_size, download.size_postfix),
					download.size_get_string(download.size, download.size_postfix)))
		end
		
		label_progress.SetText(progress_text)
		progress_bar.SetValue(http_client.GetProgress())
		
		_owner.UpdateLayout()
		
		if state == sf.gui.CBaseHTTPClient.StateConnectingFailed then
		
			_owner.EndModal(download.enum.connection_failed)
		
		elseif state == sf.gui.CBaseHTTPClient.StateTransferringFailed then
		
			_owner.EndModal(download.enum.transfer_failed)
		
		elseif state == sf.gui.CBaseHTTPClient.StateTransferringSuccessful then
			
			_owner.EndModal(download.enum.successful)
			
		end
	end
end
