/*
 * cutalot.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/plugin.h>

#include "cal-config.h"
#include "cal-setup.h"
#include "cal-menurecordings.h"
#include "cal-cutter.h"
#include "cal-gapmarker.h"
#include "i18n.h"
#include "cal-service.h"

static const char *VERSION        = "0.0.3";
static const char *DESCRIPTION    = "Cuts one recording into several recordings using named cutting marks";
static const char *MAINMENUENTRY  = "Cut-a-Lot";

class cPluginCutalot : public cPlugin {
private:
  // Add any member variables or functions you may need here.
public:
  cPluginCutalot(void);
  virtual ~cPluginCutalot();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void);
  virtual bool ProcessArgs(int argc, char *argv[]);
  virtual bool Initialize(void);
  virtual bool Start(void);
  virtual void Stop(void);
  virtual void Housekeeping(void);
  virtual void MainThreadHook(void);
  virtual cString Active(void);
  virtual const char *MainMenuEntry(void) { return MAINMENUENTRY; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  virtual bool Service(const char *Id, void *Data = NULL);
  virtual const char **SVDRPHelpPages(void);
  virtual cString SVDRPCommand(const char *Command, const char *Option, int &ReplyCode);
  };

cPluginCutalot::cPluginCutalot(void)
{
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}

cPluginCutalot::~cPluginCutalot()
{
  // Clean up after yourself!
}

const char *cPluginCutalot::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return NULL;
}

bool cPluginCutalot::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  return true;
}

bool cPluginCutalot::Initialize(void)
{
  // Initialize any background activities the plugin shall perform.
  return true;
}

bool cPluginCutalot::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);
  return true;
}

void cPluginCutalot::Stop(void)
{
  // Stop any background activities the plugin shall perform.
}

void cPluginCutalot::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

void cPluginCutalot::MainThreadHook(void)
{
  // Perform actions in the context of the main program thread.
  // WARNING: Use with great care - see PLUGINS.html!
  if (!cCalCutter::Active() && cCalCutter::Ended()) {
    if (cCalCutter::Error()) {
      char *message = NULL;
      asprintf(&message, "%s: %s", MAINMENUENTRY, tr("Multi-cut process failed!"));
      Skins.QueueMessage(mtError, message, Setup.OSDMessageTime, 0);
      free(message);
    } else {
      char *message = NULL;
      asprintf(&message, "%s: %s", MAINMENUENTRY, tr("Multi-cut process finished"));
      Skins.QueueMessage(mtInfo, message, Setup.OSDMessageTime, 0);
      free(message);
    }
  }

  if (!cCalGapMarker::Active() && cCalGapMarker::Ended()) {
    char *message = NULL;
    asprintf(&message, "%s: %s", MAINMENUENTRY, tr("Gap finder process finished"));
    Skins.QueueMessage(mtInfo, message, Setup.OSDMessageTime, 0);
    free(message);
  }
}

cString cPluginCutalot::Active(void)
{
  // Return a message string if shutdown should be postponed
  if(cCalCutter::Active() && !cCalCutter::Ended()) {
    return tr("Multi-cut process active");
  } else if (cCalGapMarker::Active() && !cCalGapMarker::Ended()) {
    return tr("Gap finder process active");
  }
  return NULL;
}

cOsdObject *cPluginCutalot::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  return new cCalMenuRecordings(NULL, 0, true);
}

cMenuSetupPage *cPluginCutalot::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cCalSetup();
}

bool cPluginCutalot::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "useCuttingPrefix"))   calConfig.useCuttingPrefix   = atoi(Value);
  else if (!strcasecmp(Name, "adjustStartingTime")) calConfig.adjustStartingTime = atoi(Value);
  else
    return false;
  return true;
}

bool cPluginCutalot::Service(const char *Id, void *Data)
{
  // Handle custom service requests from other plugins
  if (strcmp(Id, "Cutalot-MultiCut-v1.0") == 0) {
    if (Data == NULL)
      return true;
    Cutalot_MultiCut_v1_0 *data = (Cutalot_MultiCut_v1_0*) Data;
    if(cCalCutter::Start(data->recordingName)) {
      data->message = NULL;
      char *message = NULL;
      asprintf(&message, "%s: %s", MAINMENUENTRY, tr("Multi-cut process started"));
      Skins.QueueMessage(mtInfo, message, Setup.OSDMessageTime, 0);
      free(message);
    } else {
      asprintf(&(data->message), "%s: %s", MAINMENUENTRY, tr("Multi-cut process already active!"));
    }
    return true;
  }
  return false;
}

const char **cPluginCutalot::SVDRPHelpPages(void)
{
  // Return help text for SVDRP commands this plugin implements
  return NULL;
}

cString cPluginCutalot::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
  // Process SVDRP commands this plugin implements
  return NULL;
}

VDRPLUGINCREATOR(cPluginCutalot); // Don't touch this!
