require("httpTimeshift")

local dialogObject = m_simpleTV.Dialog.FindWindowById(httpTimeshift.getDialogId())
if dialogObject~=nil then   
  m_simpleTV.Dialog.SetForegroundWindow(dialogObject)
 return 
end

if m_simpleTV.User==nil then m_simpleTV.User = {}	end
if m_simpleTV.User.httpTimeshift==nil then	m_simpleTV.User.httpTimeshift = {} end
m_simpleTV.User.httpTimeshift.GUI = {}

local chInfo = m_simpleTV.Control.GetCurrentChannelInfo()
if chInfo~=nil and chInfo.Id~=nil then 
 local t = m_simpleTV.Database.GetTable('SELECT RawM3UString FROM Channels WHERE Id=' .. chInfo.Id)
 if     t ~= nil 
    and t[1]~=nil 
    and t[1].RawM3UString~=nil 
  then  
     local params =  {}
     params.channelId    = chInfo.Id
     params.address      = chInfo.Address
     params.rawM3UString = t[1].RawM3UString
     local len = httpTimeshift.handler('GetLengthByAddress', params)
     if len~=nil and len > 0 then
       m_simpleTV.User.httpTimeshift.GUI.ChannelId=chInfo.Id
       m_simpleTV.User.httpTimeshift.GUI.Len=len
     elseif ip4_TimeshiftHandler~=nil then      
             params =  {}
             params.channelId    = chInfo.Id
             params.address      = chInfo.Address
             params.rawM3UString = t[1].RawM3UString
             local len = ip4_TimeshiftHandler('GetLengthByAddress', params)
             if len~=nil and len > 0 then
                m_simpleTV.User.httpTimeshift.GUI.ChannelId=chInfo.Id
                m_simpleTV.User.httpTimeshift.GUI.Len=len
             end
          end
     end
end

if m_simpleTV.User.httpTimeshift.GUI.ChannelId==nil then
  local mess = httpTimeshift.tr('Current channel does not support archive')
  local header = 'httpTimeshift ' .. httpTimeshift.getVersion()
  if m_simpleTV.OSD.IsEnabled() then
      local t = {}
      t.message = '<html><table><tr><td><img src="simpleTVImage:./luaScr/user/httptimeshift/GUI/img/warning.png"></td>' 
                  .. '<td valign="middle" style="padding-left:20px;">' .. mess  .. '</td>'
                  .. '</tr></table></html>'
      t.richTextMode = true
      t.windowMaxSizeH = 0.6
      t.header  = header
      t.addFontHeight = 2
      --t.textColor = ARGB(255,240,240,0)
      t.showTime = 6000
      t.once = true
      --t.watchForMouse = false
      m_simpleTV.OSD.ShowMessageBox(t)
     else       
    m_simpleTV.Interface.MessageBox(mess,header,0x30)
  end
  return
end
   
 local t ={}
 t.name = httpTimeshift.tr('Archive')
 t.urlHtml = m_simpleTV.MainScriptDir_UTF8 .. 'user/httptimeshift/GUI/dialog.html'
 t.urlLua  = 'user/httptimeshift/GUI/dialog.lua'
 t.urlLogo  = m_simpleTV.MainScriptDir_UTF8 .. 'user/httptimeshift/GUI/img/timeshift.png'
 
 local scale = 1
 if m_simpleTV.Interface.GetScale then 
   scale = m_simpleTV.Interface.GetScale()
 end
 t.cx = 400 * scale
 t.cy = 180 * scale
 t.minCx = 310 * scale
 t.minCy = 170 * scale
 
 if m_simpleTV.Common.GetVersion() < 880 then  --b12.7.6
   t.flags = 1 + 4 + 512 
 else 
   t.flags = 'HAVE_SIZE | ALLOW_RESIZE | NOT_MODAL'  
 end
 
 --t.flags =  'FRAMELESS_MODE | NOT_MODAL | TRANSPARENT_BACKGROUND | INHERIT_OSD_FONT'
 --t.childPositionParams = 'typeAlign="0x202" x="0" y="0" cx="-50" cy="-50" xb="0" yb="0" mincx="50" mincy="50"'  
 
 t.id = httpTimeshift.getDialogId()
 m_simpleTV.Dialog.ShowT(t)
