var gSBRec = null;

/**
 * List showtypes (series, movies etc.), and go to the other submenus.
 */
function recordingsMenuStart()
{
    try {
        gSBRec = getStringBundle("recordings");
        var menu = new Menu(gSBRec.GetStringFromName("recordings.title"));
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsSeries.item"), null, recordingsSeriesListStart);
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsMovies.item"), null, recordingsMoviesStart);
        menu.load();
    } catch (e) { errorCritical(e); }
}


///////////////////////////////////////////////////////////////
// Series

/**
 * List series (just the titles of each series which has at least one recording),
 * but not the individual recordings.
 */
function recordingsSeriesListStart()
{
    try {
        var menu = new Menu(gSBRec.GetStringFromName("recordingsSeries.title"));
        menu.load();
        askServer({
          filter: "showtype-eq-series",
          onlyOnePer: "title",
          sortBy: "orderrecorded",
          sortDir: "desc"
        }, showSeriesResults);
    } catch (e) { errorCritical(e); }
}

/**
 * show titles only, then go to recordingsEpisodesStart(title)
 */
function showSeriesResults(recordings)
{
    try {
        for (let i = 0; i < recordings.length; i++)
        {
            let title = recordings[i].title;
            gCurrentMenu.createMenuItem(title, title, recordingsEpisodesStart);
        }
    } catch (e) { errorCritical(e); }
}

/**
 * All recordings (episodes) of a certain series
 */
function recordingsEpisodesStart(seriesTitle)
{
    try {
        var menu = new Menu(gSBRec.formatStringFromName("recordingsEpisodes.title",
            [ seriesTitle ], 1));
        menu.load();
        askServer({
          filter: "showtype-eq-series-and-title-eq-" + seriesTitle,
          sortBy: "orderrecorded",
          sortDir: "asc"
        }, showEpisodesResults);
    } catch (e) { errorCritical(e); }
}

function showEpisodesResults(recordings)
{
    showResults(recordings, function(r) { return r.subtitle; });
}


///////////////////////////////////////////////////////////////
// Movies

function recordingsMoviesStart()
{
    try {
        gSBRec = getStringBundle("recordings");
        var menu = new Menu(gSBRec.GetStringFromName("recordingsMovies.title"));
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsMovieGenres.item"), null, recordingsMovieGenreListStart);
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsAllMoviesByRecording.item"), null, recordingsAllMoviesByRecordingStart);
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsAllMoviesByProduction.item"), null, recordingsAllMoviesByProductionStart);
        menu.createMenuItem(gSBRec.GetStringFromName("recordingsAllMoviesByName.item"), null, recordingsAllMoviesByNameStart);
        menu.load();
    } catch (e) { errorCritical(e); }
}

/**
 * List all genres (their names) that we have movies in.
 */
function recordingsMovieGenreListStart()
{
    try {
        var menu = new Menu(gSBRec.GetStringFromName("recordingsMovieGenres.title"));
        menu.load();
        askServer({
          filter: "showtype-eq-movie",
          onlyOnePer: "genre",
          sortBy: "genre",
          sortDir: "asc"
        }, showMovieGenreListResults);
    } catch (e) { errorCritical(e); }
}

/**
 * Show genre name only.
 * On selection, show all movies in that genre using
 * recordingsMoviesOfGenreStart(genre).
 */
function showMovieGenreListResults(recordings)
{
    try {
        for (let i = 0; i < recordings.length; i++)
        {
            let genre = recordings[i].genre;
            gCurrentMenu.createMenuItem(genre, genre, recordingsMoviesOfGenreStart);
        }
    } catch (e) { errorCritical(e); }
}

/**
 * All movies of a certain genre
 */
function recordingsMoviesOfGenreStart(genreName)
{
    try {
        var menu = new Menu(gSBRec.formatStringFromName("recordingsMoviesOfGenre.title",
            [ genreName ], 1));
        menu.load();
        askServer({
          filter: "showtype-eq-movie-and-genre-eq-" + genreName,
          sortBy: "orderrecorded",
          sortDir: "desc"
        }, showMovieResults);
    } catch (e) { errorCritical(e); }
}

/**
 * All movies (flat list), sorted by name
 */
function recordingsAllMoviesByNameStart()
{
    try {
        var menu = new Menu(gSBRec.GetStringFromName("recordingsAllMoviesByName.title"));
        menu.load();
        askServer({
          filter: "showtype-eq-movie",
          sortBy: "title",
          sortDir: "asc"
        }, showMovieResults);
    } catch (e) { errorCritical(e); }
}

/**
 * All movies (flat list), sorted by recording time, newest first
 */
function recordingsAllMoviesByRecordingStart()
{
    try {
        var menu = new Menu(gSBRec.GetStringFromName("recordingsAllMoviesByRecording.title"));
        menu.load();
        askServer({
          filter: "showtype-eq-movie",
          sortBy: "orderrecorded",
          sortDir: "desc"
        }, showMovieResults);
    } catch (e) { errorCritical(e); }
}

/**
 * All movies (flat list), sorted by production time, newest first
 */
function recordingsAllMoviesByProductionStart()
{
    try {
        var menu = new Menu(gSBRec.GetStringFromName("recordingsAllMoviesByProduction.title"));
        menu.load();
        askServer({
          filter: "showtype-eq-movie",
          sortBy: "firstshown",
          sortDir: "desc"
        }, showMovieResultsWithYear);
    } catch (e) { errorCritical(e); }
}

function showMovieResults(recordings)
{
    showResults(recordings, function(r) { return r.fullTitle; });
}

function showMovieResultsWithYear(recordings)
{
    showResults(recordings, function(r) { return r.fullTitle + " (" + r.firstshown + ")"; });
}


////////////////////////////////////////////////////////////
// General functions

var gParamsAll = {
  details: "false",
  filter: "seen-eq-false"
}

function askServer(params, resultFunction)
{
    var server = getCoreServer();
    if (params.filter && gParamsAll.filter)
      params.filter += "-and-" + gParamsAll.filter;
    for (var prop in gParamsAll)
      if (params[prop] == undefined)
        params[prop] = gParamsAll[prop];
    new RecordingsList(params, server, resultFunction, serverError);
}

function showResults(recordings, titleFunction)
{
     try {
        for (var i = 0; i < recordings.length; i++)
        {
            let r = recordings[i];
            try {
              gCurrentMenu.createMenuItem(titleFunction(r), r, recordingContextMenu);
            } catch (e) { errorNonCritical(e); }
        }
    } catch (e) { errorCritical(e); }
}

function serverError(msg)
{
    errorCritical(msg);
}
