local base = _G

module ('moonwalkcore')

function MoonwalkGetAddress(inAdr)
	local session = base.m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.0; U; ru) Presto/2.10.229 Version/11.64")
	if session == nil then return nil end
	--INTERNET_OPTION_CONNECT_TIMEOUT
	base.m_simpleTV.WinInet.SetOptionInt(session, 2, 10000, 0)
	--INTERNET_OPTION_RECEIVE_TIMEOUT
	base.m_simpleTV.WinInet.SetOptionInt(session, 6, 10000, 0)
	local rc, answer = base.m_simpleTV.WinInet.Get(session, inAdr)
	if rc ~= 200 then
		base.m_simpleTV.WinInet.Close(session)
		base.m_simpleTV.OSD.ShowMessage("01) Connection error - " .. rc, 255, 10)
		return
	end
	--base.debug_in_file(answer .. '\n', nil, true)
	local d_id = base.findpattern(answer, "d_id: (.-),", 1, 6, 1)
	local video_token = base.findpattern(answer, "video_token: '(.-)'", 1, 14, 1)
	local access_key = base.findpattern(answer, "access_key: '(.-)'", 1, 13, 1)
	local content_type = base.findpattern(answer, "content_type: '(.-)'", 1, 15, 1)
	--base.debug_in_file('content_type: ' .. content_type .. '\n\n')
	if video_token ~= nil then 
		local body, host
		host = 'http://moonwalk.cc/sessions/create_session'
		body = 'partner=65&d_id=' .. d_id .. '&video_token=' .. video_token .. '&content_type=' .. content_type ..
		'&access_key=' .. access_key
		header = 
			'Host: moonwalk.cc\n' ..
			'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
			'Referer: ' .. inAdr .. '\n' ..
			'Accept: */*\n' ..
			'X-Requested-With: XMLHttpRequest\n' ..
			'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'
		local rc, answer = base.m_simpleTV.WinInet.Post(session, host, nil, header, body)
		if rc ~= 200 then
			base.m_simpleTV.WinInet.Close(session)
			base.m_simpleTV.OSD.ShowMessage("02) Connection error - " .. rc, 255, 10)
			return
		end
		base.debug_in_file(answer .. '\n', nil, true)
		local tmp = base.findpattern(answer, 'manifest_m3u8_parsed":"(.-)"', 1, 23, 1)
		if tmp == nil then
			tmp = base.findpattern(answer, 'manifest_m3u8":"(.-)"', 1, 16, 1)
			local tmpName = base.m_simpleTV.Common.DownloadFileInTmp (tmp, 'dozhd.tmp', false)
			local fhandle = base.io.open(tmpName , "r")
			local slines = ''
			if fhandle then
				fhandle:seek("set", 0)
				local line = fhandle:read("*l")
				if not line:find('#EXTM3U') then return end
				local t, i = {}, 1
				tmp = base.string.gsub(tmp, 'index%.m3u8', '')
				--base.debug_in_file('1) ' .. tmp, 'C:\\debug.txt', true)
				repeat
					line = fhandle:read("*l")
					if not line then break end
					if line:find('#EXT%-X%-STREAM%-INF:') then
						t[i] = {}
						t[i].Id     = i
						t[i].Name   = base.findpattern(line, 'RESOLUTION=(.-),', 1, 11, 1)
						line = fhandle:read("*l")
						--t[i].Adress = tmp .. line
						t[i].Adress = line
						i = i + 1
					end
					slines = slines .. '\n' .. line
				until false
				base.io.close(fhandle)
				if i > 2 then
					local _, id = base.m_simpleTV.OSD.ShowSelect('Выберите разрешение', 0, t, 0, 1)
					if id == nil then id = 1 end
					retAdr = t[id].Adress
				else
					retAdr = t[1].Adress
				end
				base.m_simpleTV.WinInet.Close(session)
				--base.debug_in_file('1) ' .. retAdr, 'C:\\debug.txt', true)
				return retAdr
			end
			answer = tmp
			--base.debug_in_file(tmp, 'C:\\debug.txt', true)
		end
		base.m_simpleTV.WinInet.Close(session)
		local retAdr = base.findpattern(answer, 'http(.-)index.m3u8', 1, 0, 0)
		if retAdr ~= nil then retAdr = base.string.gsub(retAdr, 'tracks%-%d', 'tracks%-1') end
		--base.debug_in_file('2) ' .. retAdr, 'C:\\debug.txt', true)
		return retAdr
	end
end