download_music = {}

download_music.strings = {}

download_music.tracks = {}
download_music.tracks.items = {}
download_music.tracks.count = 0
download_music.tracks.i = 0

download_music.url_music = download.app.GetSettings().GetChild(_T("settings"), false).GetValue(_T("url_new_music"))

download_music.func_prepare_dialog_download_track = function (_owner)

	local label_stage = __cast(_owner.GetWidget(_T("label_stage")), sf.gui.CLabelWidget)
	local stage_text = _T(string.format(T_(label_stage.GetText()).c_str(), download_music.tracks.i, download_music.tracks.count))
	label_stage.SetText(stage_text)

end

download_music.try = function (_owner)
	
	local params_music_info = {}
	params_music_info.dialog_main = _T("s4_download_music_info")
	params_music_info.dialog_connection_error = _T("s4_download_music_info_connection_error")
	params_music_info.dialog_transfer_error = _T("s4_download_music_info_transfer_error")
	params_music_info.dialog_cancel = _T("s4_download_music_info_cancel")
	params_music_info.url = download_music.url_music+_T("music.xml")
	params_music_info.outfile = _T("music_info.xml")
	
	local result_get_music_info = download.http_request(_owner, params_music_info)
	
	if result_get_music_info ~= download.enum.successful then return end
	
	local settings = sf.core.CSettings()
	settings.LoadXml(params_music_info.outfile)
	
	local group_music = settings.GetChild(_T("Music"), false)
	local group_track = group_music.GetFirstChild()
	
	download_music.tracks.items = {}
	download_music.tracks.count = 0
	
	while group_track.GetId().empty()==false do
		
		if group_track.GetId() == _T("track") then
		
			local name = group_track.GetValue(_T("name"))
			local campaign = group_track.GetValue(_T("campaign"))
			local path = group_track.GetValue(_T("path"))
			local size = group_track.GetValue(_T("size"))
						
			local outfile = sf.misc.AppendPath(sf.misc.AppendPath(_T("music"), campaign), name)
			
			if sf.misc.CheckExistPath(outfile) == false then
			
				download_music.tracks.items[download_music.tracks.count] = {}
				download_music.tracks.items[download_music.tracks.count].outfile = outfile
				download_music.tracks.items[download_music.tracks.count].url = download_music.url_music+path
				if not size.empty() then
					download_music.tracks.items[download_music.tracks.count].size = tonumber(T_(size).c_str())
				else
					download_music.tracks.items[download_music.tracks.count].size = nil
				end
				
				download_music.tracks.count = download_music.tracks.count+1
						
			end
						
		end

		group_track = group_track.GetNextSibling()
		
	end
	
	if download_music.tracks.count==0 then
	
		local dialog_no_tracks = sf.gui.CWindow()
		game.load_window(_owner, dialog_no_tracks, _T("s4_download_no_tracks"))
		local dialog_no_tracks_ptr = sf.gui.CBaseWindowPtrT(dialog_no_tracks)
		_owner.DoModal(dialog_no_tracks_ptr)
	
	else
	
		local successful = true
	
		for i=0,download_music.tracks.count-1 do
			
			download_music.tracks.i = i+1
			
			local params_track = {}
			params_track.dialog_main = _T("s4_download_track")
			params_track.dialog_connection_error = _T("s4_download_track_connection_error")
			params_track.dialog_transfer_error = _T("s4_download_track_transfer_error")
			params_track.dialog_cancel = _T("s4_download_track_cancel")
			params_track.url = download_music.tracks.items[i].url
			params_track.outfile = download_music.tracks.items[i].outfile
			params_track.func_prepare_dialog_main = download_music.func_prepare_dialog_download_track
			params_track.size = download_music.tracks.items[i].size
			
			if download.http_request(_owner, params_track) ~= download.enum.successful then 
			
				successful = false
				break 
			
			end
					
		end
		
		if successful then
			
			sf.misc.SetNamedFullAccess(_T("music"), true)
		
			local dialog_complete = sf.gui.CWindow()
			game.load_window(_owner, dialog_complete, _T("s4_download_track_complete"))
			local dialog_complete_ptr = sf.gui.CBaseWindowPtrT(dialog_complete)
			_owner.DoModal(dialog_complete_ptr)
		
		end
		
	end
	
	sf.misc.ShDeleteFile(params_music_info.outfile)
	
end
