-- script for kinokong.net (18/07/2015)

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, 'kinokong%.net') then return end

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

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, 8000, 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

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 header =  findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
header = string.gsub(header, '&raquo;', '')
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:\\_TEMP\\kinokong.txt', true)

local vars = string.match(answer, 'var flashvars = %{(.-)%};')

if vars == nil then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Ошибка - Видео не найдено", 255, 2)
	return
end
local i, t = 1, {}
local retAdr
local s = string.match(vars, '"file":"(.-)"')
if s == nil then
	s = string.match(vars, 'pl:"(.-)"') 
	if s == nil then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Ошибка - Видео не найдено", 255, 2)
		return
	end
	rc, answer = m_simpleTV.WinInet.Get(session, s)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	m_simpleTV.WinInet.Close(session)
	--debug_in_file(answer .. '\n', 'C:\\_TEMP\\kinokong.txt', true)
	--"comment":"1 Серия","file":"http://cdn2.nowfilms.ru/video/6lQ0bvXoHYnNoWAAsSSpkw/Groznaya.semeika.e1.mp4"
	for name, link in string.gmatch(answer, '"comment":"(.-)","file":"(.-)"') do
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = RuStr(name, true)
		t[i].Adress = link
		i = i + 1
	end
	--[[
	require('json')
	local tab = json.decode(answer)
	if (tab == nil) or (tab.playlist == nil) or (tab.playlist[0] == nil) then
		m_simpleTV.OSD.ShowMessage("Ошибка - Видео не найдено", 255, 10)
		return
	end
	for i = 1, #tab.playlist, 1 do
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = tab.playlist[i].comment
		t[i].Adress = tab.playlist[i].file
	end
	]]
else
	s = string.gsub(s, ',', '\\') .. '\\'
	retAdr = s
	for adr in string.gmatch(s, '(.-)\\') do
		local name = string.match(adr, '.+/(.+)')
		--debug_in_file(adr .. '\n', 'C:\\_TEMP\\kinokong.txt')
		t[i]        = {}
		t[i].Id     = i
		t[i].Name   = name
		t[i].Adress = adr
		i = i + 1
	end
	m_simpleTV.WinInet.Close(session)
end
--debug_in_file(s, 'C:\\_TEMP\\kinokong.txt', true)
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
if retAdr == nil then
	m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено", 255, 10)
	return
end
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.PlayAddress(retAdr)