<%pre>

#include <livefeatures.h>
#include <setup.h>
#include <tools.h>
#include <epg_events.h>
#include <timers.h>
#include <recman.h>


#include <vdr/config.h>


using namespace vdrlive;



</%pre>
<%args>
  int channel = -1;
  std::string channelid;
</%args>
<%session scope="global">
bool logged_in(false);
</%session>

<# scope="page" should be enough but does not work with tntnet 3.0 #>
<%request scope="global">
  tChannelID channel_id = tChannelID();
  int channel_number = 0;
  cString prev;
  cString next;
</%request>
<%include>page_init.eh</%include>
<%cpp>
reply.setHeader (tnt::httpheader::cacheControl, "no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate", true);
if (!logged_in && LiveSetup().UseAuth()) {
  cToSvConcat<0> targetUrl = "/login.html?redirect=";
  targetUrl.appendUrlEscaped(request.getQuery());
  return reply.redirect(targetUrl.data());
}
</%cpp>
<%cpp>
  pageTitle = trVDR("Schedule");

bool display_pictures = !(LiveSetup().GetTvscraperImageDir().empty());
bool events_available = false;

{
// figure out channel_id & channel_number
  const cChannel* Channel = nullptr;
  LOCK_CHANNELS_READ;
  if ( qparam.has("channelid") ) {
    tChannelID cid = cid.FromString(channelid.c_str());
    Channel = Channels->GetByChannelID( cid );
  } else if ( channel > 0 )
    Channel = Channels->GetByNumber( channel );
  else if (cDevice::CurrentChannel())
    Channel = Channels->GetByNumber(cDevice::CurrentChannel());
  else
    Channel = Channels->Get( Channels->GetNextNormal( -1 ) );
  if ( !Channel )
    throw HtmlError( tr("Couldn't find channel or no channels available. Maybe you mistyped your request?") );
  channel_id = Channel->GetChannelID();
  channel_number = Channel->Number();
  if (channel_number > 1)
    prev = cString::sprintf("href=\"schedule.html?channel=%i\"", channel_number - 1);
  if (channel_number < Channels->MaxNumber())
    next = cString::sprintf("href=\"schedule.html?channel=%i\"", channel_number + 1);

  LOCK_SCHEDULES_READ;
  cSchedule const* Schedule = Schedules->GetSchedule(Channel);
  events_available = Schedule && Schedule->Events()->First();
}


</%cpp>
<& pageelems.doc_type &>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>VDR Live - <$ pageTitle $></title>
    <& pageelems.stylesheets &>
    <& pageelems.ajax_js &>
    <& pageelems.create_html_js &>
    <script type="text/javascript"><!--
    function KeyboardInput(ev) {
      if (ev.ctrlKey)
        switch (ev.which) {
        case 37: // left
          <?? *prev ? "window.location." << *prev ?>
          return false;
        case 39: // right
          <?? *next ? "window.location." << *next ?>
          return false;
        }
        switch (ev.key) {
        case '<':
          <?? *prev ? "window.location." << *prev ?>
          return false;
        case '>':
          <?? *next ? "window.location." << *next ?>
          return false;
        }
      return true;
    }
    //--></script>
  </head>
  <body onpagehide="saveScrollPosition('content')" onpageshow="restoreScrollPosition()" onkeydown="return KeyboardInput(event);">
    <& pageelems.logo &>
    <& menu active=("schedule") component=("schedule.channel_selection") &>
    <div id="content">
      <div class="spacebar"><# spacer with fade-out effect #></div>
<%cpp>
  if (!events_available) {
</%cpp>
      <$ tr("No schedules available for this channel") $>.
<%cpp>
  }
  else {
    int col_span = 3;
    if (display_pictures) col_span++;
    std::string current_day = "";
    time_t now = time(NULL) - ::Setup.EPGLinger * 60;
    cToSvConcat<0> epg_item;
    epg_item.reserve(10000);
    epg_item.concat('[');
    {
      LOCK_TIMERS_READ;
      LOCK_CHANNELS_READ;
      LOCK_RECORDINGS_READ;  // required because appendEpgItemWithRecItem needs also lock on recording
      LOCK_SCHEDULES_READ;
      const cChannel* Channel = Channels->GetByChannelID(channel_id);
      cSchedule const* Schedule = Schedules->GetSchedule(Channel);
      const cEvent* PresentEvent = Schedule->GetPresentEvent();
      for (const cEvent *Event = Schedule->Events()->First(); Event; Event = Schedule->Events()->Next(Event)) {
        if (Event->EndTime() <= now && Event != PresentEvent) continue;
        current_day = appendEpgItemWithRecItem(epg_item, current_day, Event, Channel, false, Timers);
      }
    }
    epg_item.concat("]]]");
    if (current_day.empty() ) {
</%cpp>
      <$ tr("No events available for this channel") $>.
%   } else {
<script>
document.write(addEventListString(<$col_span$>, <$$ epg_item.c_str() $>))
</script>
<%cpp>
    }
  }
</%cpp>
      <div class="spacebar"><# spacer with fade-out effect #></div>
    </div>
    <script>imgLoad()</script>
  </body>
</html>
<%include>page_exit.eh</%include>

<%def channel_selection>
<form action="schedule.html" method="get" id="channels">
   <span>
     <label for="channel"><$ tr("Channel") $>:&nbsp;<span class="bold"><$ cToSvInt(channel_number) $></span></label>
     <& channels_widget name=("channel") selected=(channel_id) onchange=("document.forms.channels.submit()") &>
   </span>
</form>
<span class="tools">
% if (cDevice::PrimaryDevice() ) {
  <& pageelems.ajax_action_href action="switch_channel" tip=(tr("Switch to this channel")) param=(channel_id) image="zap.png" className="icon" alt="" &>
% }
  <& pageelems.hls_channel channelId=(channel_id) className="icon" &>
  <& pageelems.m3u_playlist_channel channelId=(channel_id) className="icon" &>
</span>
<span class="sep">|</span>
<?? *prev ? "<a " << *prev << ">"?>«&nbsp;<$ tr("Prev. entry") $><?? *prev ? "</a>" ?>
<span class="sep">|</span>
<?? *next ? "<a " << *next << ">"?><$ tr("Next entry") $>&nbsp;»<?? *next ? "</a>" ?>
</%def>
