require 'vkcore'
require 'moonwalkcore'
require('jsdecode')

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, 'http://gidonline%.club') then return end
m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''

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

---------------------------------------------------------------------------
local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.17")
 --Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0")
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)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
	return
end

answer = RuStr(answer, true)

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

local retAdr = string.match(answer, '<iframe.-src="(.-)"')
if retAdr ~= nil then
	--http://www.youtube.com/embed/vAu5Zo_zobA
	if string.match(retAdr, 'youtube%.com') then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.Control.PlayAddress(retAdr)
		return
	end
	--http://moonwalk.cc/video/f2364ddd8247d4ee/iframe
	if string.match(retAdr, 'moonwalk%.cc') then
		retAdr = moonwalkcore.MoonwalkGetAddress(retAdr)
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.Control.CurrentAdress = retAdr
		return
	end
end

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

local vid = findpattern(answer, 'id="meta" content="(%d-)"', 1, 19, 1)
if vid == nil then return end
--local js = 'http://gidonlinekino.com/wp-content/themes/gidonline/js/jss1.js'
local js = 'http://gidonline.club/wp-content/themes/gidonline/js/trailer.js'
rc, answer = m_simpleTV.WinInet.Get(session, js)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
	return
end

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

local p = findpattern(answer, '%b""', 1, 1, 1)
local id_post = findpattern(answer, ':(.-)}', 1, 1, 1)
local host = 'http://gidonline.club'  .. p
local body = id_post .. '=' .. vid
--debug_in_file(body .. ' = ' .. host .. '\n\n', 'C:\\tmp.txt', true)
local header =
		'Host: gidonline.club\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'
rc, answer = m_simpleTV.WinInet.Post(session, host, nil, header, body)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
	return
end

answer = RuStr(answer, true)

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

--debug_in_file(answer .. '\n\n', 'C:\\gidonlinekino.txt', true)
local t, i = {}, 1
for fr in string.gmatch(answer, '<li(.-)</li>') do
	local address = string.match(fr, "%.src='(.-)';")
	--debug_in_file(address .. '\n', 'C:\\gidonlinekino.txt')
	t[i]        = {}
	t[i].Id     = i
	t[i].Name   = 'Плейер ' .. i 
	t[i].Adress = address
	i = i + 1
end
if i > 2 then
	local _, id = m_simpleTV.OSD.ShowSelect(title, 0, t, 0, 1)
	if id == nil then id = 1 end
	retAdr = t[id].Adress
else 
	if t[1] ~= nil then 
		retAdr = t[1].Adress
	else
		retAdr = string.match(answer, '<iframe.-src="(.-)"')
	end
end
--do return end
--debug_in_file(retAdr .. '\n\n', 'C:\\gidonlinekino.txt')
-----------------------------------------------------------------------------------------------------------------------------------------
--http://moonwalk.cc/video/84f4017bf7d2922e/iframe
if string.match(retAdr, 'moonwalk%.cc') then
	retAdr = moonwalkcore.MoonwalkGetAddress(retAdr)
	m_simpleTV.WinInet.Close(session)
	if retAdr == nil then
		m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено",255,10)
		return
	end
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end
-----------------------------------------------------------------------------------------------------------------------------------------
--http://kinolove.tv/?video=ODlaMzovKjcwKDM0TDtBTiFXSCMzWEUoSVFHW0pQZD0tNnNndWp6aXVfOTE9MXVlbmF1Z3FnK1I3Mmg9Nlc2VW5kNmR0ZWVxN1YzZCtSZD0z 
--http://plyy.co/?video=ODlaMzovKjcwKDM0TDtBTiFXSCMzWEUoSVFHW0pQZD0tN3Vnb2ZlcnVbOTE9MXZvc2F2bG9kK1I3Mmg9OGZyZ3RUMk40Y2IxcmFwZitSZD0z
if string.match(retAdr, 'kinolove%.tv') or string.match(retAdr, 'plyy%.co') then
	header = 'Referer: ' .. inAdr .. '\n'
	rc, answer = m_simpleTV.WinInet.Get(session, retAdr, nil, 0, header)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	--debug_in_file(answer .. '\n\n', 'C:\\gidonlinekino.txt', true)
	--answer = findpattern(answer, "<script type='text/javascript'>\neval(.-)</script>", 1, 36, 9)
	--if answer == nil then return end
	--answer = string.gsub(answer, 'eval', '')
	answer = findpattern(answer, "<script type='text/javascript'>(.-)</script>", 1, 32, 9)
	--local answer = jsdecode.DoDecode(answer)
	if answer == nil then return end
	--debug_in_file(answer .. '\n\n', 'C:\\gidonlinekino.txt', true)
	--local vid, oid, hash = string.match(answer, "var type='(.-)';.-var d='(.-)';.-var ver='(.-)';")
	local vid = string.match(answer, "var type='(.-)';")
	local oid = string.match(answer, "var d='(.-)';")
	local hash = string.match(answer, "var ver='(.-)';")
	local url = string.match(answer, "%{url:'(.-)'")
	if url == nil then return end
	retAdr = string.format(url .. '?oid=%s&video_id=%s&embed_hash=%s', oid, vid, hash)
	--debug_in_file(retAdr .. '\n\n', 'C:\\gidonlinekino.txt', true)
	if string.match(retAdr, '^https://') then 
		m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
	end
	rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	--debug_in_file(answer .. '\n\n', 'C:\\gidonlinekino.txt', true)
	--do return end
	t, i = {}, 1
	for name, address in string.gmatch(answer, '"url(%d-)":"(.-)"') do
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = name
		t[i].Adress = string.gsub(address, '\\', '')
		--debug_in_file(name .. '=' .. t[i].Adress .. '\n', 'C:\\_TEMP\\gidonlinekino.txt')
		i = i + 1
	end
	if i > 2 then
		local _, id = m_simpleTV.OSD.ShowSelect(title, 0, t, 0, 1)
		if id == nil then id = 1 end
		retAdr = t[id].Adress
	else
		if t[1] ~= nil then retAdr = t[1].Adress end
	end
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end

if string.match(retAdr, 'http://%d-.%d-.%d-.%d-/video') then
	retAdr = moonwalkcore.MoonwalkGetAddress(retAdr)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.CurrentAdress = retAdr
	return
	--debug_in_file(retAdr .. '\n\n', 'C:\\gidonlinekino.txt', true)
end
