require('json')

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..'\n')
if not string.match( inAdr, 'www%.youtube%.com/user/(.-)/playlists' )  and not string.match( inAdr, 'youtube%.com/channel/(.-)/playlists') then return end

require ('youtubecore')
m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress =  'error'
--------------
function trim(str)
str = string.match(str,'^%s*(.-)%s*$')
  return str
end
--------------
function ChoiseYoutubePlst(void)

 if m_simpleTV.User~=nil and m_simpleTV.User.Youtube~=nil and m_simpleTV.User.Youtube.Plst~=nil then
   
   m_simpleTV.User.Youtube.Plst.PrevIndex = m_simpleTV.Control.GetMultiAddressIndex()
   local ret,id = m_simpleTV.OSD.ShowSelect_UTF8(m_simpleTV.User.Youtube.Plst.Header,(m_simpleTV.User.Youtube.Plst.Index or 0) ,m_simpleTV.User.Youtube.Plst.Table,5000,1+4+8+16)
	if ret==1 then 
	  m_simpleTV.User.Youtube.Plst.Active = true
	  m_simpleTV.User.Youtube.Plst.IndexLevel0 = id-1
	  m_simpleTV.Control.ChangeAdress='No'
	  m_simpleTV.Control.CurrentAdress = m_simpleTV.User.Youtube.Plst.Table[id].Adress 
	  m_simpleTV.User.Youtube.Plst.Level0 = true
	  dofile(m_simpleTV.MainScriptDir .. "user\\video\\youtube_user_playlists.lua") 
	end	
 end
 
end
------------------------------------------------------------------
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
if string.match(inAdr, '^https://') then
	m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
end

if not string.match(inAdr,'%?') then inAdr = inAdr .. '?view=1&sort=dd' end

local rc,answer = m_simpleTV.WinInet.Get(session,inAdr)
m_simpleTV.WinInet.Close(session)
if rc ~= 200 then return end

local header = string.gsub(answer,'\n','')
header = string.gsub(header,'\r','')
header =  findpattern( header,'<title>(.-)</title>',1,7,8)
if header==nil then header=inAdr end
 header = header .. ' (playlists)'
if m_simpleTV.Control.CurrentTitle_UTF8 ~= nil then m_simpleTV.Control.CurrentTitle_UTF8=youtubecore.unescape_html(header) end

answer = string.gsub(answer,'&quot;','"')
answer = string.gsub(answer,'&raquo;','')
answer = string.gsub(answer,'&#39;',"'")
answer = string.gsub(answer,'&amp;',"&")

local host = findpattern(inAdr,'http://(.-)/',1,0,0) or findpattern(inAdr,'https://(.-)/',1,0,0)
local i=1
local t={}
local retAdr ='' 

for w in string.gmatch(answer,'h3 class="yt%-lockup%-title(.-)</h3>') do
 
	 local name = findpattern(w,'<a(.-)</a>',1,0,0)
	  if name~=nil then 
	    name = findpattern(name,'>(.-)<',1,1,1)
	  end
	 local adr =  findpattern(w,'href=%b""',1,7,1)
	 
	 if name~=nil and adr~=nil then
		t[i] = {}
        t[i].Id  =  i
        t[i].Name = i .. '. ' .. trim(name)
        t[i].Adress = host .. adr
        --debug_in_file(t[i].Name .. '\n' .. t[i].Adress .. '\n\n')
	    if retAdr == '' then retAdr = t[i].Adress end
		i=i+1
     end		
 end

 if i>2 then
    if m_simpleTV.User == nil then m_simpleTV.User={} end
	if m_simpleTV.User.Youtube == nil then m_simpleTV.User.Youtube={} end
	if m_simpleTV.User.Youtube.Plst == nil then m_simpleTV.User.Youtube.Plst={} end
	
	m_simpleTV.User.Youtube.Plst.Active = true
	m_simpleTV.User.Youtube.Plst.Table = t
	m_simpleTV.User.Youtube.Plst.Index = 0
	m_simpleTV.User.Youtube.Plst.Header = header
	m_simpleTV.User.Youtube.Plst.Level0 = false
	
	local ret,id = m_simpleTV.OSD.ShowSelect_UTF8(header,0,t,5000,1+4+8+16)
	
	--m_simpleTV.OSD.ShowSelect_UTF8(header,0,t,5000,0) --1+4+8+16)
	--m_simpleTV.Control.CurrentAdress = 'wait'
	--do return end
	
	if ret==1 then 
	   retAdr = t[id].Adress 
	   m_simpleTV.User.Youtube.Plst.Index = id-1
	end
 end

if retAdr==nil or retAdr=='' then return end

m_simpleTV.Control.ChangeAdress='No'
m_simpleTV.Control.CurrentAdress =  retAdr
dofile(m_simpleTV.MainScriptDir .. "user\\video\\youtube_user_playlists.lua") 


