--get adress
local base = _G

require('json')
require('uppod')

if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr = m_simpleTV.Control.CurrentAdress

if inAdr == nil then return end

--debug_in_file(inAdr, 'C:\\2.txt', true)

if not string.match(inAdr, 'http%://allserials%.tv') then return end

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''

local hash = '0123456789WGXMHRUZID=NQVBLihbzaclmepsJxdftioYkngryTwuvihv7ec41D6GpBtXx3QJRiN5WwMf=ihngU08IuldVHosTmZz9kYL2bayE'

local session = m_simpleTV.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_simpleTV.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 5000, 0)

local rc, answer = m_simpleTV.WinInet.Get(session, inAdr)

if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	return
end

function DecodeUppod(param, hash1, hash2)
	-- decode
	local i = 1
	for i = 1, string.len(hash1), 1 do
		local re1 = hash1:sub(i, i)
		local re2 = hash2:sub(i, i)
		param = param:gsub(re1, '___')
		param = param:gsub(re2, re1)
		param = param:gsub('___', re2)
	end -- for
	return base.decode64(param)
end

function ExtractWord(num, str, delim)
	local i, k = 1, 1
	while k ~= num do
		if str:sub(i, i) == delim then k = k + 1 end
		i = i + 1
	end
	local j = i + 1;
	while (j < string.len(str) + 1) and (str:sub(j, j) ~= delim) do j = j + 1 end
	return str:sub(i, j - 1)
end;

function DecodeUppod2(param)
	if hash == nil then return end
	hash = hash:gsub('ih', '*')
	local hash1 = ExtractWord(2, hash, '*')
	local hash2 = ExtractWord(1, hash, '*')
	return DecodeUppod(param, hash1, hash2)
end

function RuStr(Str, isFrom)
	if Str == nil then return end
	if isFrom == true then
		return m_simpleTV.Common.string_fromUTF8(Str, 1251)
	else
		return m_simpleTV.Common.string_toUTF8(Str, 1251)
	end
end

answer = RuStr(answer, true)

local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")
header = string.gsub(header, '&amp;', "&")
header = string.gsub(header, '&#8230;', "")
if header == nil then header = inAdr end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

--debug_in_file(answer .. '\n', 'C:\\tmp.txt', true)

local retAdr = findpattern(answer, 'name="flashvars" value="(.-)"', 1, 24, 1)
if retAdr == nil then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Error allserials.tv - can't find address", 255, 10)
	return
end

retAdr = findpattern(retAdr, 'pl=(.-)&', 1, 3, 1)
retAdr = unescape(retAdr)
retAdr = DecodeUppod2(retAdr)
retAdr = m_simpleTV.Common.string_fromUTF8(retAdr)
rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
if rc~=200 then
	m_simpleTV.WinInet.Close(session)
	return
end
m_simpleTV.WinInet.Close(session)

answer = DecodeUppod2(answer)

answer = RuStr(answer, true)
--debug_in_file(answer .. '\n', 'C:\\tmp.txt', true)

local tab = json.decode(answer)
if tab == nil or tab.playlist == nil then
	m_simpleTV.OSD.ShowMessage("Error allserials.tv - can't find address", 255, 10)
	return
end

------------------------------------------------------------------------------
local t, i, n = {}, 1, 0
retAdr = ''
while true do
	if tab.playlist[i] == nil then break end
	if tab.playlist[i].playlist == nil then 
		t[i] = {}
		t[i].Id = i
		t[i].Name = tab.playlist[i].comment
		t[i].Adress = tab.playlist[i].file
		--debug_in_file(t[i].Adress .. '\n')
		if retAdr == '' then retAdr = t[i].Adress end
		i = i + 1
	end
end
if i > 2 then
	local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
	if id == nil then id = 1 end
	retAdr = t[id].Adress
end

m_simpleTV.Control.CurrentAdress = retAdr