--get address

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

local inAdr =  m_simpleTV.Control.CurrentAdress 


if inAdr:find('^$TVSSTART') then
	TVSources_var.ChangeAdress = true
	TVSources_var.CurrentSource = findpattern(inAdr,'TVSSTART=%b""',0,10,1) or 'Unknow'
	--TVSources_var.CurrentSource = inAdr:match('TVSSTART="(.+)"') or 'Unknow'
	if not tvs_core.isUTF8(TVSources_var.CurrentSource) then
       TVSources_var.CurrentSource = m_simpleTV.Common.string_toUTF8(TVSources_var.CurrentSource)
       inAdr = m_simpleTV.Common.string_toUTF8(inAdr)
	end	
	m_simpleTV.OSD.ShowMessage_UTF8('['.. TVSources_var.CurrentSource .. ']' , 0xffffff, 3)

	inAdr = inAdr:gsub('$TVSSTART=%b"" ','')
	tvs_core.tvs_debug('ChangeAdress: CurrentAdress=' .. inAdr .. ' CurrentSource=' .. (TVSources_var.CurrentSource or 'nil'))
	m_simpleTV.Control.CurrentAdress = inAdr
	return
end

if TVSources_var.ChangeAdress == true then
	m_simpleTV.Control.EventPlayingInterval = 5000 
	if  m_simpleTV.Control.RealAdress~=nil or m_simpleTV.Control.Peers==-1 then
		m_simpleTV.Control.EventTimeOutInterval = TVSources_var.ChannelWaiting * 1000 -- ожидание начала воспроизведения
	end
	TVSources_var.ChangeAdress = false	
end

--inAdr =  m_simpleTV.Control.CurrentAdress            -- string.gsub(m_simpleTV.Control.CurrentAdress,'\'','"')
TVSources_var.CurrentAdress = inAdr

----------------- IPTV -----------------------------
TVSources_var.tmp.iptv_sign = inAdr:match("&tvssrc=(.+)")
if TVSources_var.tmp.iptv_sign~=nil then 
  TVSources_var.tmp.AttemptCount = TVSources_var.tmp.AttemptCount or 1
  if TVSources_var.tmp.OldAdr ~= inAdr  then 
     TVSources_var.tmp.OldAdr = inAdr
     TVSources_var.tmp.AttemptCount = 1
  else
     TVSources_var.tmp.AttemptCount = TVSources_var.tmp.AttemptCount + 1
  end
  if TVSources_var.tmp.AttemptCount <= 3 then
     inAdr = tvs_func.ChangeIpPort(inAdr, TVSources_var.tmp.iptv_sign) or inAdr  
  end
  --inAdr = inAdr:gsub("&tvssrc=(.+)","")
  m_simpleTV.Control.CurrentAdress = inAdr  
end
----------------------------------------------------
if  not inAdr:find('TVSources &id') then 
	TVSources_var.AutoErrorBuild = false
	return 
end

TVSources_var.AutoErrorBuild = true
TVSources_var.BadSrcCount	= 0
TVSources_var.TVSAdress = inAdr

local tmpName,tmpSrc,default,Id

local CurrentTitleUTF8 = m_simpleTV.Control.CurrentTitle_UTF8 -- m_simpleTV.Common.string_toUTF8(m_simpleTV.Control.CurrentTitle,1251)

Id = findpattern( inAdr,'id=%b""',0,4,1) or tvs_core.tvs_clearname(CurrentTitleUTF8, false)
default = findpattern( inAdr,'df=%b""',0,4,1) or TVSources_var.Default or ''

--tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. (Id or 'nil') .. ', Default Source: ' .. default .. '. Address: ' .. inAdr)	

if Id == nil then return end

tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. Id  .. ', Default Source: ' .. default .. '. Address: ' .. inAdr)	

if TVSources_var.LastPlay and TVSources_var.LastPlay==true then 
	tmpSrc,tmpName = tvs_core.tvs_GetLastPlay(Id) -- тут используем только название источника, урл может быть уже устаревший
	if tmpSrc == nil then tmpSrc = '' end
	
	tmpSrc,tmpName = tvs_core.tvs_GetUrlById (Id, tmpSrc, nil)  -- тут берем свежий урл
	
    if not tvs_core.isUTF8(tmpSrc) then
       tmpSrc = m_simpleTV.Common.string_toUTF8(tmpSrc)
	end
	
	if tmpName ~= nil then
		m_simpleTV.OSD.ShowMessage_UTF8('['.. tmpSrc .. ']' , 0xffffff, 3)
		--tvs_core.tvs_ShowError('['.. tmpSrc .. ']',0xffffff,1)
		tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. Id .. '. Start Play last success source: ' .. tmpSrc .. ', URL: ' .. tmpName)
	else
		tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. Id .. '. Start Play last success source: error')
	end
end

if tmpName == nil then
	tmpSrc,tmpName = tvs_core.tvs_GetUrlById (Id, default, nil)
	m_simpleTV.Control.tmpCounter = (m_simpleTV.Control.tmpCounter or 0) + 1
    tvs_core.tvs_debug('getaddress ID: ' .. Id .. ' Count='..m_simpleTV.Control.tmpCounter.. ' ret='..(tmpSrc or ''))
    if tmpSrc == 'stop' then 
       m_simpleTV.Control.ChangeAdress = 'Yes' 
       m_simpleTV.Control.ExecuteAction(11)  -- 11   Stop (KEYSTOP)
    end
	if tmpSrc == nil then tmpSrc = '' end
    if not tvs_core.isUTF8(tmpSrc) then
       tmpSrc = m_simpleTV.Common.string_toUTF8(tmpSrc)
	end	
	
	if tmpName ~= nil then
		m_simpleTV.OSD.ShowMessage_UTF8('['.. tmpSrc .. ']' , 0xffffff, 3)
		--tvs_core.tvs_ShowError(CurrentTitleUTF8 .. ' ('.. tmpSrc .. ')',0x00CC00)
		tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. Id .. '. Source: ' .. tmpSrc .. ', Start Play: ' .. tmpName)	
	else
		tvs_core.tvs_debug(CurrentTitleUTF8 .. '. ID: ' .. Id .. '. Source: ' .. tmpSrc .. '. Start Play: error GetUrlById.')
	end
end

if tmpName==nil then return end


m_simpleTV.Control.ChangeAdress='No'
m_simpleTV.Control.CurrentAdress = tmpName


if m_simpleTV.Control.RealAdress~=nil then
    m_simpleTV.Control.EventPlayingInterval = 3000 
	m_simpleTV.Control.EventTimeOutInterval = TVSources_var.ChannelWaiting * 1000 -- ожидание начала воспроизведения
end
--m_simpleTV.Control.EventPlayingInterval = 0

TVSources_var.ChangeAdress 	= false
TVSources_var.CurrentAdress = tmpName
TVSources_var.CurrentSource = tmpSrc


