require("httpTimeshift")

----------------------------------------------------------------------
local function escapeJSString(str)
 if str == nil then return nil end
 str = string.gsub(str,'\\','\\\\')
 str = string.gsub(str,'"','\\"')
 str = string.gsub(str,'\n','\\n')
 str = string.gsub(str,'\r','\\r')
 return str
end
----------------------------------------------------------------------
local function translateHtml(Object)
  m_simpleTV.Dialog.ExecScript(Object,'doTranslate();')
end
----------------------------------------------------------------------
function baseInit(Object)
 
 local c = os.time();
 local currentTime = os.date('%Y-%m-%dT%H:%M',c)
 local minTime = os.date('%Y-%m-%dT%H:%M',c - math.floor(m_simpleTV.User.httpTimeshift.GUI.Len/1000))
 local maxTime = currentTime
  
 local scr = 'setCalendar(' 
                          .. '"' .. escapeJSString(currentTime) .. '",'
                          .. '"' .. escapeJSString(minTime) .. '",'
                          .. '"' .. escapeJSString(maxTime) .. '")'
 
 m_simpleTV.Dialog.ExecScript(Object,scr)
end
----------------------------------------------------------------------
function OnNavigateComplete(Object)
  translateHtml(Object)
  baseInit(Object)
  httpTimeshiftDialogStartWatchTimer()
end
----------------------------------------------------------------------
function httpTimeshiftDialogRequestCancel(Object)
m_simpleTV.User.httpTimeshift.GUI = {}
m_simpleTV.Dialog.Close(Object) 
end
----------------------------------------------------------------------
function httpTimeshiftDialogDoPlay(Object,Val)

local chInfo = m_simpleTV.Control.GetCurrentChannelInfo()
 if      chInfo~=nil 
    and chInfo.Id~=nil 
    and chInfo.Id == m_simpleTV.User.httpTimeshift.GUI.ChannelId
    and type(Val) == 'string'
   then   
  
  local ryear, rmonth, rday, rhour, rmin = Val:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+)')
  local c = os.time({year = ryear, month = rmonth, day = rday, hour = rhour, min = rmin, sec = 0})
  local offset = os.time() - c
  if offset>60 then
    offset = offset * 1000
    m_simpleTV.Control.SetNewAddressT({ address=chInfo.Address
                                       ,timeshiftOffset = offset})  
  end
 end
httpTimeshiftDialogRequestCancel(Object)
end
----------------------------------------------------------------------
function httpTimeshiftDialogStartWatchTimer() 
 m_simpleTV.Timer.SetTimer(500, 'httpTimeshiftDialogOnWatchTimer()',nil,true)
end
----------------------------------------------------------------------
function httpTimeshiftDialogOnWatchTimer()
 
 if m_simpleTV.User.httpTimeshift.GUI == nil then return end
 
 local dialogObject = m_simpleTV.Dialog.FindWindowById(httpTimeshift.getDialogId())
 if dialogObject~=nil then        
   local chInfo = m_simpleTV.Control.GetCurrentChannelInfo()
   if  chInfo == nil 
       or chInfo.Id~=m_simpleTV.User.httpTimeshift.GUI.ChannelId
     then
      httpTimeshiftDialogRequestCancel(dialogObject)
   else 
    httpTimeshiftDialogStartWatchTimer()
   end
  end
 
end
----------------------------------------------------------------------