<%
// This file is used SERVER SIDE only, it MUST comply to ECMA Script 5.1 and not
// use jQuery or any other client side extensions.
//
// e.g. ECMA Script 5.1 cannot parse ISO DateTime strings

"use strict";

import "/js/utility.js"

var myth = new Myth();

function recStatusToClass(statusInt)
{
    var dvr = new Dvr();
    var statusStr = dvr.RecStatusToString(statusInt);
    var statusClass = "program" + statusStr.replace(/ /g, '');
    return statusClass;
}

function scheduleSimpleRecording(chanID, startTime, type)
{
    var dvr = new Dvr();

    var recRule = dvr.GetRecordSchedule(0, "", 0, chanID, startTime, false);

    // Prevent duplicate recording rules being created
    if (recRule.Id > 0 && recRule.Type == type)
        return chanID + "#" + startTime.toISOString() + "#" + recRule.Id;

    if (type == "Record All (Channel)")
    {
        type = "Record All";
        recRule.Filter |= 1024;
    }

    recRule.Type = type;

    var recRuleId = dvr.AddRecordSchedule(recRule);

    return chanID + "#" + startTime.toISOString() + "#" + recRuleId;
}

function checkRecordingStatus(chanID, startTime)
{
    var guide = new Guide();
    var program = guide.GetProgramDetails(chanID, startTime);
    return chanID + "#" + startTime.toISOString()  + "#" + recStatusToClass(program.Recording.Status);
}

function deleteRecRule(recRuleID, chanID, startTime)
{
    var dvr = new Dvr();
    dvr.RemoveRecordSchedule(Number(recRuleID));
    return chanID + "#" + startTime.toISOString()  + "#" + recRuleID;
}

function dontRecord(chanID, startTime)
{
    var dvr = new Dvr();
    dvr.AddDontRecordSchedule(chanID, startTime, false);
    return chanID + "#" + startTime.toISOString();
}

function neverRecord(chanID, startTime)
{
    var dvr = new Dvr();
    dvr.AddDontRecordSchedule(chanID, startTime, true);
    return chanID + "#" + startTime.toISOString();
}

function deleteRecording(recordedId, startTime, allowReRecord, forceDelete)
{
    var dvr = new Dvr();
    dvr.DeleteRecording(recordedId, forceDelete, allowReRecord);
    return recordedId;
}

function unDeleteRecording(recordedId)
{
    var dvr = new Dvr();
    dvr.UnDeleteRecording(recordedId);
    return recordedId;
}

function saveRecordingRule(recRuleId, chanId, startTime)
{
    var dvr = new Dvr();

    var recRule = dvr.GetRecordSchedule(recRuleId, "", 0,
                                        chanId,
                                        startTime,
                                        false);

    // Program Data
    if (recRule.Id == 0)
    {
        recRule.Title       = getArg("Title");
        recRule.SubTitle    = getArg("SubTitle");
        recRule.Description = getArg("Description");
        recRule.Category    = getArg("Category");
        recRule.ProgramId   = getArg("ProgramId");
        recRule.SeriesId    = getArg("SeriesId");
        recRule.ChanId      = getIntegerArg("ChanId");
        recRule.CallSign    = getArg("CallSign");
        recRule.StartTime   = getArg("StartTime");
        recRule.EndTime     = getArg("EndTime");
    }

    // Schedule Options
    recRule.Type            = getArg("Type");
    recRule.RecPriority     = getIntegerArg("RecPriority");
    recRule.StartOffset     = getIntegerArg("StartOffset") * -1; // StartOffset treats a negative X value as start 'X minutes late', which is confusing from a UI perspective
    recRule.EndOffset       = getIntegerArg("EndOffset");
    recRule.DupMethod       = getArg("DupMethod");
    recRule.DupIn           = getArg("DupIn");
    recRule.PreferredInput  = getIntegerArg("PreferredInput");
    recRule.Inactive        = Boolean(getIntegerArg("Inactive"));

    // Filters
    var filters = getArg("Filter");
    for (var filterkey in filters)
    {
        var filterValue = Number(filterkey.slice(1));
        if (filters[filterkey] > 0)
        {
            recRule.Filter |= (1 << filterValue);
        }
        else
        {
            recRule.Filter &= ~(1 << filterValue);
        }
    }

    // Storage Options
    recRule.RecGroup        = getArg("RecGroup");
    recRule.StorageGroup    = getArg("StorageGroup");
    recRule.PlayGroup       = getArg("PlayGroup");
    recRule.MaxEpisodes     = getIntegerArg("MaxEpisodes");
    recRule.MaxNewest       = Boolean(getIntegerArg("MaxNewest"));
    recRule.AutoExpire      = Boolean(getIntegerArg("AutoExpire"));

    // Post-Processing
    recRule.AutoMetaLookup  = Boolean(getIntegerArg("AutoMetaLookup"));
    recRule.AutoCommflag    = Boolean(getIntegerArg("AutoCommflag"));
    recRule.AutoTranscode   = Boolean(getIntegerArg("AutoTranscode"));
    recRule.Transcoder      = getArg("Transcoder");
    recRule.AutoUserJob1    = Boolean(getIntegerArg("AutoUserJob1"));
    recRule.AutoUserJob2    = Boolean(Number( getArg("AutoUserJob2")));
    recRule.AutoUserJob3    = Boolean(getIntegerArg("AutoUserJob3"));
    recRule.AutoUserJob4    = Boolean(getIntegerArg("AutoUserJob4"));

    // Metadata Options
    recRule.Inetref         = getArg("Inetref");
    recRule.Season          = getIntegerArg("Season");
    recRule.Episode         = getIntegerArg("Episode");

    var ruleId = 0;
    if (recRule.Id == 0)
    {
        print("Add Record Schedule");
        ruleId = dvr.AddRecordSchedule(recRule);
    }
    else
    {
        ruleId = recRule.Id;
        print("Update Record Schedule: " + ruleId);
        dvr.UpdateRecordSchedule(recRule);
    }

    return ruleId;
}

var ret = "ERROR";
switch (getArg("_action")) // _action to avoid conflicts with http form's "action"
{
    case "simpleRecord":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = scheduleSimpleRecording(getArg("ChanId"), validDate, getArg("Type"));
        break;
    case "checkRecStatus":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = checkRecordingStatus(getArg("ChanId"), validDate);
        break;
    case "deleteRecRule":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = deleteRecRule(getArg("RecRuleId"), getArg("ChanId"), validDate);
        break;
    case "dontRecord":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = dontRecord(getArg("ChanId"), validDate);
        break;
    case "neverRecord":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = neverRecord(getArg("ChanId"), validDate);
        break;
    case "deleteRecording":
        ret = deleteRecording(getArg("RecordedId"),
                              getArg("AllowRerecord") == 1,
                              getArg("ForceDelete") == 1);
        break;
    case "unDeleteRecording":
        ret = unDeleteRecording(getArg("RecordedId"));
        break;
    case "saveRecordingRule":
        var validDate = myth.ParseISODateString(getArg("StartTime"));
        ret = saveRecordingRule(getArg("id"), getArg("ChanId"), validDate);
        break;
    default:
        break;
}
%>
<%=ret%>