-------------------------------------------------------------
-- Init
-------------------------------------------------------------
if m_simpleTV.User==nil then m_simpleTV.User={} end
if m_simpleTV.User.ip4==nil then m_simpleTV.User.ip4={} end
local ip4_table = m_simpleTV.User.ip4
ip4_table.DefaultLen =  3*24*3600*1000
-------------------------------------------------------------
-------------------------------------------------------------
--Timer handler
-------------------------------------------------------------
function ip4_TimeshiftSetDelayed()
 ip4_TimeshiftStopTimer()		
  
 local state = m_simpleTV.Control.GetState()
 if state < 1 or state > 5 then return end
 
 local adr = ip4_TimeshiftGetNakedAdr(ip4_table.CurAdr)
 if adr == nil then return end
 if not string.match(adr,'%?$') then adr = adr .. '?' end
  
 if ip4_table.TimeShiftPos==1.0 then 
				ip4_table.AdrToSet =  adr
  else 
   local offset = math.floor(ip4_table.TimeShiftLen * (1- ip4_table.TimeShiftPos ) / 1000 )
   ip4_table.AdrToSet =  adr .. 'offset=' .. -offset
  end
		
 --debug_in_file ('state:' .. m_simpleTV.Control.GetState() .. '  new adr:' .. ip4_table.AdrToSet ..  '\n')
 --m_simpleTV.Control.SetNewAddress(ip4_table.AdrToSet)
 
 m_simpleTV.Common.PostMessageToMainFrame(0x420,63,0);
end
-------------------------------------------------------------
---Main handler 
-------------------------------------------------------------
function ip4_TimeshiftHandler(Type,Data,Data1)
 
 --debug_in_file ('Type:' .. Type )
 --if Data~=nil then  debug_in_file ('   Data:' .. Data) end
 --if Data1~=nil then  debug_in_file ('   Data1:' .. Data1) end
 --debug_in_file ('\n')
 
 -------------------------------------------------------------    
  if Type=='GetLengthByAddress' then 
   if not ip4_TimeshiftTestAddress(Data) then return 0 end
   return ip4_table.DefaultLen
  end
-------------------------------------------------------------
 if Type=='TestAddress' then 
   ip4_TimeshiftStopTimer()   
   if ip4_table.AdrToSet~=nil then 
     if not ip4_TimeshiftCompareAdr(Data,ip4_table.AdrToSet) then ip4_table.AdrToSet=nil end
   end
   
   return ip4_TimeshiftTestAddress(Data) 
 end
-------------------------------------------------------------
 if Type=='Start' then 
    
	ip4_TimeshiftStopTimer()
	ip4_table.TimeShiftPos = 1.0
    ip4_table.TimeShiftLen = ip4_table.DefaultLen
    ip4_table.CurAdr = Data
	--debug_in_file   ('data:' .. Data ..  '\ntoset:' ..  (ip4_table.AdrToSet or '') .. '\n\n')
		
	if Data1==0 and ip4_TimeshiftCompareAdr(ip4_table.CurAdr,ip4_table.AdrToSet) then ip4_table.CurAdr = ip4_table.AdrToSet end
        
	ip4_table.AdrToSet = nil
	
	local offset 
	local setadr = false
	if Data1~=0 then 
	   offset = -Data1/1000
	   setadr = true
	 else 
	   offset = findpattern(ip4_table.CurAdr,'offset=[%-0-9]+',1,7,0)
	end
	
	if offset~=nil then
	   --debug_in_file	   ('offset = ' .. offset .. '\n')
	   offset = tonumber(offset)
	   if offset~=nil and offset<0 then
	        ip4_table.TimeShiftPos =  1 - ((-offset*1000)/ip4_table.TimeShiftLen)
			if ip4_table.TimeShiftPos<0 or ip4_table.TimeShiftPos>1 then
				ip4_table.TimeShiftPos=1.0
			end
			if  setadr then 
			  ip4_table.CurAdr = ip4_TimeshiftGetNakedAdr(ip4_table.CurAdr) .. '?offset=' .. offset
			end
			m_simpleTV.OSD.ShowMessage_UTF8('Setting timeshift ' .. math.floor(offset/60) .. ' min')
	    end		  
	end	
	
	--debug_in_file   ('retAdr = ' .. ip4_table.CurAdr .. '\n')
	return ip4_table.CurAdr
 end
-------------------------------------------------------------
  if Type=='IsSeekable' then
   return true
 end
-------------------------------------------------------------
  if Type=='GetPosition' then  
   return ip4_table.TimeShiftPos
 end
-------------------------------------------------------------
 if Type=='GetLength' then
	return ip4_table.TimeShiftLen
 end
-------------------------------------------------------------
 if Type=='SetPostion' then
	
	ip4_TimeshiftStopTimer()
	
	if ip4_table.CurAdr==nil then return false end
	ip4_table.TimeShiftPos = Data
	ip4_table.TimerId = m_simpleTV.Timer.SetTimer(1500,'ip4_TimeshiftSetDelayed()')
		
	return true
 end
-------------------------------------------------------------  
 if Type=='OnStop' then
    ip4_TimeshiftStopTimer()
	return
 end
 -------------------------------------------------------------  
 if Type=='OnPause' then
 	return
 end
 -------------------------------------------------------------  
 if Type=='OnPlaying' then
 	return
 end
  -------------------------------------------------------------
end --ip4_TimeshiftHandler
-------------------------------------------------------------
-- Helpers
-------------------------------------------------------------
function ip4_TimeshiftTestAddress(Adr)
 if string.match(Adr, 'svc%.iptv%.rt%.ru/hls') then return true end
 if string.match(Adr, 'ip42%-htlive') then return true end
 return false
end
-------------------------------------------------------------
function  ip4_TimeshiftStopTimer()
  if ip4_table.TimerId then 
	 m_simpleTV.Timer.DeleteTimer (ip4_table.TimerId)
	 ip4_table.TimerId = nil		
  end  
end
-------------------------------------------------------------
function  ip4_TimeshiftGetNakedAdr(Adr)
  if Adr == nil then return nil end
  return string.gsub(Adr,'?(.+)','') 
end
-------------------------------------------------------------
function  ip4_TimeshiftCompareAdr(Adr1,Adr2)
  if not Adr1 or not Adr2 then return false end
  return (string.gsub(Adr1,'?(.+)','')	== string.gsub(Adr2,'?(.+)',''))
end
-------------------------------------------------------------
-------------------------------------------------------------
---- Post init 
-------------------------------------------------------------
m_simpleTV.Timeshift.AddHandler('ip4_TimeshiftHandler',5000)
-------------------------------------------------------------
-------------------------------------------------------------
