<%pre>

#include <setup.h>
#include <tools.h>
#include <epg_events.h>
#include <recman.h>
#include <timerconflict.h>

#include <vdr/menu.h>

using namespace vdrlive;

</%pre>
<%args>
  int update;
</%args>
<%session scope="global">
bool logged_in(false);
int update_status(1);
TimerConflictNotifier timerNotifier();
</%session>
<%cpp>
  if (!logged_in && LiveSetup().UseAuth()) {
    cToSvConcat targetUrl = "/login.html?redirect=";
    targetUrl.appendUrlEscaped(request.getQuery());
    return reply.redirect(targetUrl.data());
  }

  tChannelID prev_chan;
  tChannelID next_chan;
  reply.setContentType( "application/xml" );

  if (update_status != update) {
    update_status = update;
  }

  std::string infoMsg;
  cSv infoUrl;

  if (timerNotifier.ShouldNotify()) {
    infoMsg = timerNotifier.Message();
    infoUrl = "timerconflicts.html";
  }

  const char* NowReplaying = cReplayControl::NowReplaying();

  EpgInfo epgEvent;

  if (NowReplaying) {
    // is is OK to lock here, because CreateEpgInfo will *not* lock other lists
    LOCK_RECORDINGS_READ;
    const cRecording *recording = Recordings->GetByName(NowReplaying);
    if (recording) {
      epgEvent.CreateEpgInfo(
        concat("recording_", cToSvXxHash128(XXH3_128bits(NowReplaying, strlen(NowReplaying))) ),
        recording,
        tr("Playing recording"));
    }
  }
  else {
    cSv CHANNEL_STR("channel");
    if (cDevice::CurrentChannel()) {
      const int SKIP_GAP = 1;
      LOCK_CHANNELS_READ;
      const cChannel* Channel = Channels->GetByNumber(cDevice::CurrentChannel());
      const cChannel* tmp = Channels->GetByNumber(Channels->GetPrevNormal(cDevice::CurrentChannel()), -SKIP_GAP);
#if VDRVERSNUM >= 20606
      if (tmp)
#else
      if (tmp && cDevice::PrimaryDevice() && cDevice::PrimaryDevice()->HasDecoder() )
#endif
        prev_chan = tmp->GetChannelID();
      tmp = Channels->GetByNumber(Channels->GetNextNormal(cDevice::CurrentChannel()), SKIP_GAP);
#if VDRVERSNUM >= 20606
      if (tmp)
#else
      if (tmp && cDevice::PrimaryDevice() && cDevice::PrimaryDevice()->HasDecoder() )
#endif
        next_chan = tmp->GetChannelID();

      LOCK_SCHEDULES_READ;
      const cSchedule *Schedule = Schedules->GetSchedule(Channel);

      if (Schedule) {
        const cEvent *Event = Schedule->GetPresentEvent();
        if (Event) {
          epgEvent.CreateEpgInfo(Channel, Event, CHANNEL_STR);
        } else {
          epgEvent.CreateEpgInfo(CHANNEL_STR, Channel->Name(),
                            tr("No EPG info for current event!"));
        }
      }
      else {
        epgEvent.CreateEpgInfo(CHANNEL_STR, Channel->Name(),
                          tr("No EPG info for current channel!"));
      }
    }
    else {
      cSv chanName(tr("no current channel!"));
      epgEvent.CreateEpgInfo(CHANNEL_STR, chanName, chanName);
    }
  }
  if (epgEvent.Id().empty() ) {
    epgEvent.CreateEpgInfo("error", "no current channel!", "error retrieving status info!");
  }
  else {
    if (prev_chan.Valid() && next_chan.Valid())
    {
</%cpp>
<& xmlresponse.ibox update=(update_status) type=(epgEvent.Id()) caption=(epgEvent.Caption()) currentTime=(epgEvent.CurrentTime(tr("%I:%M:%S %p"))) duration=(epgEvent.StartTime(tr("%I:%M %p")) + std::string(tr(" - ")) + epgEvent.EndTime(tr("%I:%M %p"))) title=(epgEvent.Title()) elapsed=(epgEvent.Elapsed()) prev_chan=(prev_chan) next_chan=(next_chan) infoMsg=(infoMsg) infoUrl=(infoUrl) &>
<%cpp>
    }
    else if (prev_chan.Valid()) {
</%cpp>
<& xmlresponse.ibox update=(update_status) type=(epgEvent.Id()) caption=(epgEvent.Caption()) currentTime=(epgEvent.CurrentTime(tr("%I:%M:%S %p"))) duration=(epgEvent.StartTime(tr("%I:%M %p")) + std::string(tr(" - ")) + epgEvent.EndTime(tr("%I:%M %p"))) title=(epgEvent.Title()) elapsed=(epgEvent.Elapsed()) prev_chan=(prev_chan) infoMsg=(infoMsg) infoUrl=(infoUrl) &>
<%cpp>
    }
    else if (next_chan.Valid()) {
</%cpp>
<& xmlresponse.ibox update=(update_status) type=(epgEvent.Id()) caption=(epgEvent.Caption()) currentTime=(epgEvent.CurrentTime(tr("%I:%M:%S %p"))) duration=(epgEvent.StartTime(tr("%I:%M %p")) + std::string(tr(" - ")) + epgEvent.EndTime(tr("%I:%M %p"))) title=(epgEvent.Title()) elapsed=(epgEvent.Elapsed()) next_chan=(next_chan) infoMsg=(infoMsg) infoUrl=(infoUrl) &>
<%cpp>
    }
    else {
</%cpp>
<& xmlresponse.ibox update=(update_status) type=(epgEvent.Id()) caption=(epgEvent.Caption()) currentTime=(epgEvent.CurrentTime(tr("%I:%M:%S %p"))) duration=(epgEvent.StartTime(tr("%I:%M %p")) + std::string(tr(" - ")) + epgEvent.EndTime(tr("%I:%M %p"))) title=(epgEvent.Title()) elapsed=(epgEvent.Elapsed()) infoMsg=(infoMsg) infoUrl=(infoUrl) &>
<%cpp>
    }
  }
</%cpp>

