require 'json'

if m_LinkToPlay.Control.ChangeAddress ~= 'No' then return end

local inAdr =  m_LinkToPlay.Control.CurrentAddress
if inAdr == nil then return end

if not string.match(inAdr, 'ivi%.ru/watch/') then return end

m_LinkToPlay.Control.ChangeAddress = 'Yes'
m_LinkToPlay.Control.CurrentAddress = ''

local url = 'www.ivi.ru'

local index = findpattern(inAdr, '/watch/(.+)', 1, 0, 0)
if index == nil then return end

local session = m_LinkToPlay.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

--INTERNET_OPTION_CONNECT_TIMEOUT
m_LinkToPlay.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_LinkToPlay.WinInet.SetOptionInt(session, 6, 5000, 0)

local rc, answer = m_LinkToPlay.WinInet.Get(session, url, index)
if rc ~= 200 then
	m_LinkToPlay.WinInet.Close(session)
	return
end
answer = m_LinkToPlay.Common.string_fromUTF8(answer)

local header = findpattern(answer, '<meta name="title" content="(.-)"/>', 1, 28, 3)
if header == nil then header = inAdr end
if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = header end

local videoid = string.match(index, '/(%d+)' )
local i, t = 1, {}
local retAdr, serial = '', false

--Сериал--------------------------------------------------------------------------------------------------------
if not videoid then
	local iviAdr
	local s = findpattern(answer, 'episodeListTitle(.+)', 1, 0, 0)
	if s == nil then return end
	while true do
		iviAdr = findpattern(s, '<strong><a href="(.-)</a></strong>', 1, 11, 0)
		if iviAdr == nil then break end
		s = findpattern(s, '<strong><a href="/watch/(.+)', 1, 24, 0)
		iviAdr = 'http://www.ivi.ru' .. string.match(iviAdr, 'href="(.-)"')
		t[i]         = {}
		t[i].Id      = i
		t[i].Name    = 'Cерия ' .. i
		t[i].Address = iviAdr
		--debug_in_file(iviAdr .. '\n\n', 'C:\\_TEMP\\debug.txt')
		i = i + 1
	end
	if i > 2 then local ret = m_LinkToPlay.Taskbar.AddList(header, 0, t, 7000, 1) end
	return
end
----------------------------------------------------------------------------------------------------------------

--Фильм---------------------------------------------------------------------------------------------------------
index = 'api/json'
url   = 'api.digitalaccess.ru'
local header = 'Content-Type: application/json'
local body = '{"method":"da.content.get","params":["'.. videoid .. '",{"site":"1"}]}'

rc, answer = m_LinkToPlay.WinInet.Post(session, url, index, header, body)
if rc ~= 200 then
	--m_simpleTV.OSD.ShowMessage("Error ivi.ru - " .. rc, 255, 10)
	m_LinkToPlay.WinInet.Close(session)
	return
end

answer = m_LinkToPlay.Common.string_fromUTF8(answer)
answer = string.gsub(answer, '(%[%])', '"nil"')
--debug_in_file(answer .. '\n\n', 'C:\\_TEMP\\debug.txt', true)

local tab = json.decode(answer)
if tab == nil or tab.result == nil or tab.result.files == nil then
	--m_simpleTV.OSD.ShowMessage("Error ivi.ru - can't decode json",255,10)
	m_LinkToPlay.WinInet.Close(session)
	return
end

local retAdr
i, t = 1, {}
while true do
	if tab.result.files[i] == nil then break end
	t[i]         = {}
	t[i].Id      = i
	t[i].Name    = 'Качество ' .. tab.result.files[i].content_format
	t[i].Address = tab.result.files[i].url
	retAdr = t[1].Address
	i = i + 1
end

if i > 2 then
	local ret = m_LinkToPlay.Taskbar.AddList(header, 0, t, 7000, 1)
	m_LinkToPlay.WinInet.Close(session)
	return
end
----------------------------------------------------------------------------------------------------------------

if (retAdr == nil) then
	--m_simpleTV.OSD.ShowMessage("Error ivi.ru - can't find adress",255,10)
	m_LinkToPlay.WinInet.Close(session)
	return
end

if string.match(retAdr, '%.f4m$') then
	--m_simpleTV.OSD.ShowMessage("Error ivi.ru - f4m not supported now",255,10)
	do
		m_LinkToPlay.WinInet.Close(session)
		m_LinkToPlay.Control.CurrentAddress = retAdr
		return
	end
	url = findpattern(retAdr, 'http://(.-)/', 1, 0, 0)
	index = retAdr:sub(url:len() + 1)
	url = findpattern(retAdr, 'http://(.-)/', 1, 7, 1)
	rc, answer = m_LinkToPlay.WinInet.Get(session, url, index)
	m_LinkToPlay.WinInet.Close(session)
	if rc ~= 200 then
		--m_simpleTV.OSD.ShowMessage("Error ivi.ru - can't find adress",255,10)
		return
	end
	retAdr = decode64(retAdr)
end

m_LinkToPlay.Control.CurrentAddress = retAdr