local base = _G
module ('inetcomcore')

---------------------------------------------------------------------------
function InetcomLogOut()

 if base.m_simpleTV.user~=nil and base.m_simpleTV.user.inetcom~=nil and base.m_simpleTV.user.inetcom.session~=nil then 
    base.m_simpleTV.WinInet.Get(base.m_simpleTV.user.inetcom.session,'http://inetcom.tv/auth/logout')
	base.m_simpleTV.WinInet.Close(base.m_simpleTV.user.inetcom.session) 		  
	base.m_simpleTV.user.inetcom.session=nil		   
 end		
 
end
--------------------------------------------------------------------------- 
function InetcomTryLogin(DontLogin)
 
 if base.m_simpleTV.user.inetcom.session==nil then
   if base.m_simpleTV.user.inetcom.session ~= nil then InetcomLogOut() end
   
   base.m_simpleTV.user.inetcom.session = base.m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
   if base.m_simpleTV.user.inetcom.session == nil then return false end
 
  else 
   return true
 end

if DontLogin==true or base.m_simpleTV.user.inetcom.login == nil or base.m_simpleTV.user.inetcom.pass==nil then return true end

base.string.gsub(base.m_simpleTV.user.inetcom.login,'%@','%%40')
 
local header = 'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\r\nReferer: http://inetcom.tv/\r\nAccept: application/json, text/javascript, */*; q=0.01\r\nX-Requested-With: XMLHttpRequest\r\nContent-Type: application/x-www-form-urlencoded\r\n'
local body ='LoginForm%5Busername%5D='.. base.m_simpleTV.user.inetcom.login .. '&LoginForm%5Bpassword%5D=' .. base.m_simpleTV.user.inetcom.pass

local rc,answer=base.m_simpleTV.WinInet.Post(base.m_simpleTV.user.inetcom.session,'http://inetcom.tv/auth/login',nil,header,body)

if rc~=200 then 
  InetcomError("Can't login")
  return false
end

--{"result":false,"errorCode":1}'
--base.debug_in_file( answer)
if base.string.match( answer, '"result":true' ) then return true end

 InetcomError("Can't login")
 return false
end
------------------------------------------------------------------------------------------------------------
function InetcomError(Message)
 
 base.m_simpleTV.OSD.ShowMessage('Inetcom ошибка - ' .. Message,0xFF,5)
 InetcomLogOut()
 
end
------------------------------------------------------------------------------------------------------------
function InetcomGetUrl(PageAddress,Dontlogin)

 if InetcomTryLogin(Dontlogin)~=true then return nil end
        
 local rc,answer = base.m_simpleTV.WinInet.Get(base.m_simpleTV.user.inetcom.session,PageAddress)
 if rc ~= 200 then return nil end

 --base.debug_in_file(answer)

 local retAdr = base.findpattern(answer,"lnks = %[%'http(.-)%'%]",1,9,2)
 if retAdr == nil then return nil end
 
 return retAdr
end
------------------------------------------------------------------------------------------------------------
function LoadPlstM3U(getIP)

 if InetcomTryLogin(false)~=true then return false end
 
 local rc,answer = base.m_simpleTV.WinInet.Get(base.m_simpleTV.user.inetcom.session,'http://inetcom.tv/channel/all')
 if rc ~= 200 then return false end
 --base.debug_in_file(answer)
 answer = base.m_simpleTV.Common.string_fromUTF8(answer)
 
local groupname='' 
local name,address,tmp
local outstr = '#EXTM3U  $ExtFilter="Inetcom"\n'

 for w in base.string.gmatch(answer,'<div class="cat%-div(.-)</div>') do
   
   groupname = base.findpattern(w,"<h4(.-)/h4>",1,0,0)
   if groupname ~= nil then groupname = base.findpattern(groupname,"%b><",1,1,1) end
   if groupname == nil then groupname='' end
   
   for w1 in base.string.gmatch(w,'<td class="col02">(.-)</td>') do
       
      name = base.findpattern(w1,'%b><',1,1,1)
	  address = base.findpattern(w1,'%b""',1,1,1)
	  
	  if name~=nil and address~=nil then
		address  = 'http://inetcom.tv' .. address 
		if getIP==true then
		   tmp = InetcomGetUrl(address,false)
		   if tmp~=nil then
		     address = base.string.gsub(tmp,'sid=(.+)','sid=InetcomFreeSid')
		   end
		end
		
		outstr = outstr .. '#EXTINF:-1,'.. groupname .. ':' .. name .. '\n' .. address .. '\n'
	  end
	  
   end
  
 end
 
 --base.debug_in_file(outstr)

 local tmpName = base.m_simpleTV.Common.GetTmpName()
 if tmpName == nil then return false end
 
 local file=base.io.open(tmpName,'w+')
 if file == nil then return false end  
 file:write(outstr)
 file:close()
 
 p={}
 p.TypeSourse = 1
 p.DeleteBeforeLoad = 0
 p.TypeFind =   0
 p.AutoSearch = 1
 p.NumberM3U =  0
 p.Find_Group = 1
 p.TypeCoding = 0
 p.TypeSkip   = 1
 p.BorpasFileFormat=0
 p.ExtFilter = ''
 p.AutoNumber = 0
 p.UpdateID='InetComm01'

 base.m_simpleTV.Common.LoadPlayList (tmpName,p,0,true,false)

 base.os.remove(tmpName)
 
 return true
end
------------------------------------------------------------------------------------------------------------

