<%pre>

#include <stdext.h>
#include <exception.h>
#include <setup.h>
#include <tools.h>
#include <epg_events.h>
#include <recman.h>

using namespace vdrlive;

</%pre>
<%args>
  std::string epgid;
  std::string delete_rec;  // to_confirm or confirmed
  int history_num_back = 0;
  std::string async;
  std::string tab;
  std::string sort;
  std::string filter;
  std::string flat;
</%args>
<%session scope="global">
  bool logged_in(false);
</%session>
<%request scope="global">
  EpgInfo    epgEvent;
  std::string epgImage;
</%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 targetUrl = "/login.html?redirect=";
    targetUrl.appendUrlEscaped(request.getQuery());
    return reply.redirect(targetUrl.data());
  }

  pageTitle = tr("Electronic program guide information");

  bool ajaxReq = !async.empty() && (parse_int<int>(async) != 0);

  bool aboutBox = false;
  if (epgid.compare(0, 4, "del_") == 0) {
    delete_rec = "to_confirm";
    epgid.erase(0, 4);
  }

  bool isEvent = false;
  bool isRecording = false;
  epgImage.clear();
// we use objects of these types during creation of this page, so keep the locks

  if (!delete_rec.empty()) {
      pageTitle = tr("Delete recordings");
  } else if (!epgid.empty()) {

    const std::string_view recording_s("recording_");
    const std::string_view event_s("event_");
    const std::string_view aboutbox("aboutBox");

    // check for recording:
    if (epgid.compare(0, recording_s.length(), recording_s) == 0) {
      isRecording = true;
      LOCK_RECORDINGS_READ;
      const cRecording *recording = RecordingsManager::GetByHash(epgid, Recordings);
      if (recording == 0) {
        throw HtmlError(tr("Couldn't find recording or no recordings available"));
      }
      epgEvent.CreateEpgInfo(epgid, recording);
      cTvMedia image;
      EpgEvents::PosterTvscraper(image, NULL, recording);
      epgImage = image.path;
    }
    // check for event:
    else if (epgid.compare(0, event_s.length(), event_s) == 0) {
      /* Need to lock here channels also, because CreateEpgInfo will lock
       * it also and this will result in a wrong lock order */
      epgEvent.CreateEpgInfo(epgid);
      isEvent = true;
      LOCK_SCHEDULES_READ;
      const cEvent* event = EpgEvents::GetEventByEpgId(epgid, Schedules);
      cTvMedia image;
      EpgEvents::PosterTvscraper(image, event, nullptr);
      epgImage = image.path;
    }
    // check for aboutbox:
    else if (epgid.compare(0, aboutbox.length(), aboutbox) == 0) {
      aboutBox = true;
    }
  }
</%cpp>
<& pageelems.doc_type &>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>VDR-Live - <$ pageTitle $></title>
<%cpp>
  if (!ajaxReq) {
</%cpp>
    <& pageelems.stylesheets &>
    <& pageelems.create_html_js &>
    <& pageelems.ajax_js &>
<%cpp>
  }
</%cpp>
  </head>
  <body onpagehide="saveScrollPosition('content')" onpageshow="restoreScrollPosition()">
<%cpp>
  if (!ajaxReq) {
</%cpp>
    <& pageelems.logo &>
    <& menu &>
<%cpp>
  }
</%cpp>
    <div id="content">
      <div class="spacebar"><# spacer with fade-out effect #></div>
<%cpp>
  if (!epgEvent.Id().empty() && delete_rec.empty()) {
    uintptr_t i_epgEvent = (uintptr_t)&epgEvent;
    cToSvConcat start(epgEvent.StartTime("%a,"), ' ',
                      epgEvent.StartTime(tr("%b %d %y")), ' ',
                      epgEvent.StartTime(tr("%I:%M %p")) );
    if (epgEvent.isRecording() ) {
      start << tr(" - ") << epgEvent.EndTime(tr("%I:%M %p")) << ' ';
      AppendDuration(start, tr("(%d:%02d)"), epgEvent.Duration());
    }

    std::string tools_component;
    if (isRecording) {
      tools_component = epgEvent.Archived().empty() ? "recordings.rec_tools" : "recordings.archived_disc" ;
    }
    if (isEvent)
      tools_component = "epginfo.epgTools";

</%cpp>
    <& pageelems.epg_tt_box boxId=(epgEvent.Id()) caption=(epgEvent.Caption()) tools_comp=(tools_component) time=(start) title=(epgEvent.Title()) short_descr=(epgEvent.ShortDescr()) long_descr=(epgEvent.LongDescr()) archived=(epgEvent.Archived()) elapsed=(epgEvent.Elapsed()) channel=(epgEvent.ChannelName()) filename=(epgEvent.FileName()) epgImage=(epgImage) iepgEvent=(i_epgEvent) sort=(sort) filter=(filter) flat=(flat) history_num_back=(history_num_back) &>
<%cpp>
  }
  if (aboutBox) {
</%cpp>
    <& pageelems.about_tt_box &>
<%cpp>
  }
  if (!delete_rec.empty() ) {
</%cpp>
    <& recordings.todel todel=(epgid) confirmed=(delete_rec) history_num_back=(history_num_back) &>
% }
      <div class="spacebar"><# spacer with fade-out effect #></div>
    </div>
  </body>
</html>

<%include>page_exit.eh</%include>

<# ---------------------------------------------------------------------- #>

<%def epgTools>
<%args>
std::string id;
std::string title;
int detail;
std::string s_IMDB_ID;
</%args>
<& pageelems.epg_tool_box detail=(detail) epgid=(id) title=(title) startTime=(epgEvent.GetStartTime()) endTime=(epgEvent.GetEndTime()) s_IMDB_ID=(s_IMDB_ID) &>
</%def>
