require 'json'
--require('uppod')
require 'vkcore'

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

local inAdr = m_simpleTV.Control.CurrentAdress

if inAdr == nil then return end

if not string.match(inAdr, 'baskino%.com') then return end

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''
--local hash11 = {"8","m","9","k","o","J","s","R","V","p","G","i","H","z","e","M","d","Z","c","l","1","v","W","T","Q","="}
--local hash12 = {"a","x","4","N","D","b","B","I","5","Y","w","0","L","t","y","u","g","X","6","7","3","f","2","n","U","C"}
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, 5000, 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
answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer .. '\n\n\n', 'C:\\baskino.txt', true)
local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
--header = m_simpleTV.Common.string_fromUTF8(header)
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
header = string.gsub(header, '&quot;', '"')
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end
local t, i = {}, 1
--SERIAL----------------------------------------------------------------------------------------------------------
if string.match(inAdr, 'baskino%.com/serial/') then
	--local content = findpattern(answer, 'var tvs_codes = %{(.-)%}', 1, 17, 1)
	--debug_in_file(content .. '\n\n', 'C:\\baskino.txt', true)
	--<div id="episodes-2" 
	for id, w in string.gmatch(answer, '<div id="episodes%-(%d+)"(.-)</ul>') do
		debug_in_file(id .. '\n' .. w .. '\n\n', 'C:\\baskino.txt')
		t[i]        = {}
		t[i].Id     = id
		t[i].Name   = 'Сезон ' .. id
		t[i].Adress = id
		i = i + 1
	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
	else
		retAdr = t[1].Adress
	end
	local content = findpattern(answer, '<div id="episodes%-' .. retAdr .. '"(.-)</div>', 1, 0, 0)
	if content == nil then return end
	t, i = {}, 1
	--debug_in_file(content .. '\n\n', 'C:\\baskino.txt', true)
	for id, name in string.gmatch(content, '<span onclick="showCode%((%d+),this%);">(.-)</span>') do
		--debug_in_file(id .. '\n' .. name .. '\n\n', 'C:\\baskino.txt')
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = name
		t[i].Adress = id
		i = i + 1
	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
	else
		retAdr = t[1].Adress
	end
	content = findpattern(answer, 'var tvs_codes = %{(.-)</script>', 1, 17, 9)
	content = string.gsub(content, "\\", "")
	--debug_in_file(content .. '\n\n', 'C:\\baskino.txt', true)
	local adr = findpattern(content, '"' .. retAdr .. '":"(.-)","', 1, 0, 3)
	if adr == nil then adr = findpattern(content, '"' .. retAdr .. '":"(.-)"};', 1, 0, 3) end
	if adr == nil then return end
	retAdr = adr
	--debug_in_file(retAdr .. '\n\n', 'C:\\baskino.txt')
	adr = findpattern(retAdr, 'src="(.-)"', 1, 5, 1)
	if adr == nil then adr = findpattern(retAdr, 'file:"(.-)"', 1, 6, 1) end
	retAdr = adr
	--debug_in_file(retAdr .. '\n\n', 'C:\\baskino.txt', true)
end
--FILM------------------------------------------------------------------------------------------------------------
if string.match(inAdr, 'baskino%.com/films/') then
	local menu = findpattern(answer, '<ul id="player_tabs" class="tabs clearfix">(.-)<li class="favorites">', 1, 43, 22)
	if menu == nil then return end
	--debug_in_file(menu .. '\n', 'C:\\baskino.txt')
	t, i = {}, 1
	for id, name in string.gmatch(menu, '<li rel="(.-)">(.-)</li>') do
		--debug_in_file(id .. '\n' .. name .. '\n', 'C:\\baskino.txt')
		local item = findpattern(answer, '<div align="center" id="basplayer_' .. id .. '" class="player_code"(.-)</div>', 1, 0, 0)
		local link
		if item == nil then break end
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = name
		if (id == 'hd') or (id == 'flash') then
			link = findpattern(item, 'src="(.-)"', 1, 5, 1)
		elseif (id == 'html5') then
				link = findpattern(answer, 'file: "(.-)"', 1, 7, 1)
				--debug_in_file(link .. '\n', 'C:\\baskino.txt', true)
		end
		t[i].Adress = link
		i = i + 1
	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
	else
		retAdr = t[1].Adress
	end
end
------------------------------------------------------------------------------------------------------------------
if retAdr == nil then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Error baskino.net - can't find address", 255, 10)
	return
end
--YOUTUBE.COM-----------------------------------------------------------------------------------------------------
--http://www.youtube.com/embed/Y5ndARH_0cA
if string.match(retAdr, 'youtube%.com') or (string.match(retAdr, 'allserials%.tv')) then
	--debug_in_file(retAdr .. '\n\n', 'C:\\baskino.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.PlayAddress(retAdr)
	return
end
--VK.COM----------------------------------------------------------------------------------------------------------
--http://vk.com/video_ext.php?oid=184699600&id=164071411&hash=ca9d3588ac039e97&hd=3
if string.match(retAdr, 'vk%.com') then
	retAdr = vkcore.vk_getadress(retAdr)
	--debug_in_file(retAdr, 'C:\\baskino.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end
--GIDTV.CC--------------------------------------------------------------------------------------------------------
--http://gidtv.cc/s/6738/
if string.match(retAdr, 'gidtv%.cc') then
	rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
	m_simpleTV.WinInet.Close(session)
	if rc ~= 200 then return end
	answer = findpattern(answer, 'setFlash%((.-)%)', 1, 9, 1)
	if answer == nil then return end
	--debug_in_file(answer .. '\n\n\n', 'C:\\baskino.txt', true)
	local hd, sd, id = string.match(answer, "'(.-)', '(.-)', (%d+)")
	local t = {}
	t[1]        = {}
	t[1].Id     = 1
	t[1].Name   = 'HD 1'
	t[1].Adress = string.match(hd, "^(.-) or ")
	t[2]        = {}
	t[2].Id     = 2
	t[2].Name   = 'HD 2'
	t[2].Adress = string.match(hd, " or (.+)")
	t[3]        = {}
	t[3].Id     = 3
	t[3].Name   = 'SD 1'
	t[3].Adress = string.match(sd, "^(.-) or ")
	t[4]        = {}
	t[4].Id     = 4
	t[4].Name   = 'SD 2'
	t[4].Adress = string.match(sd, " or (.+)")
	local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
	if id == nil then id = 1 end
	retAdr = t[id].Adress
	--debug_in_file(retAdr, 'C:\\baskino.txt', true)
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end
if string.match(retAdr, 'allserials%.tv') then

end
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr