/*
 * deluxe.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 <getopt.h>
#include "recstatus.h"
#include "log.h"
#include "tracks.h"
#include "menu_recs.h"
#include "menu_setup.h"
#include "i18n.h"
#include "deluxe_config.h"
#include "audio_thread.h"
#include "blacklist.h"

static const char *VERSION        = "0.0.3";
static const char *DESCRIPTION    = trNOOP("a grabber for DeluxeMusic");
static const char *MAINMENUENTRY  = trNOOP("Deluxe");

// LogFile declaration and statics
cLogFile LogFile;
char *cLogFile::LogFileName = NULL;
int cLogFile::loglevellimit = 0;

class cPluginDeluxe : public cPlugin {
private:
  char mainmenuentry[15];
public:
  cPluginDeluxe(void);
  virtual ~cPluginDeluxe();
  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);
  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);
  };

cPluginDeluxe::cPluginDeluxe(void)
{
    gl_recStatusMonitor = NULL;
}

cPluginDeluxe::~cPluginDeluxe()
{
    delete gl_recStatusMonitor;
}

const char *cPluginDeluxe::CommandLineHelp(void)
{
  return "  -l file,  --logfile=file to specify a specific logfile for epgsearch\n"
         "  -v n,  --verbose=n       verbose level for logfile\n";
}

bool cPluginDeluxe::ProcessArgs(int argc, char *argv[])
{
  static const struct option long_options[] = {
    { "logfile",        required_argument, NULL, 'l' },
    { "verbose",        required_argument, NULL, 'v' },
    { NULL }
  };

  int c=0,i=0;

  while ((c = getopt_long(argc, argv, "l:v:", long_options, &i)) != -1) 
  {
      switch (c) 
      {
          case 'l': 
	      cLogFile::LogFileName = optarg;
	      break;
          case 'v': 
	      cLogFile::loglevellimit = atoi(optarg);
	      break;
          default:  return false;
      }
  }
  
  return true;
}

bool cPluginDeluxe::Initialize(void)
{
#if APIVERSNUM < 10507
  RegisterI18n(Phrases);
#endif
  return true;
}

bool cPluginDeluxe::Start(void)
{
    if (cLogFile::LogFileName)
	LogFile.Open(cLogFile::LogFileName, Version());
    else
	LogFile.Open(AddDirectory(ConfigDirectory("deluxe"), "deluxe.log"), Version());

    Tracks.Load(AddDirectory(ConfigDirectory("deluxe"), "tracks.conf"), true);
    Blacklist.Load(AddDirectory(ConfigDirectory("deluxe"), "blacklist.conf"), true);

    gl_recStatusMonitor = new cRecStatusMonitor;
    return true;
}

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

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

void cPluginDeluxe::MainThreadHook(void)
{
  // Perform actions in the context of the main program thread.
  // WARNING: Use with great care - see PLUGINS.html!
}

cString cPluginDeluxe::Active(void)
{
  if (cConvertToAudioThread::m_Instance)
     return tr("Deluxe audio conversion still running!");
  return NULL;
}

cOsdObject *cPluginDeluxe::MainMenuAction(void)
{
  return new cMenuRecs();
}

const char *cPluginDeluxe::MainMenuEntry(void) 
{ 
    
    if (DeluxeConfig.hideMainMenuEntry)
	return NULL;
    if (cConvertToAudioThread::m_Instance)
    {
	sprintf(mainmenuentry, "%s (%d%%)", tr(MAINMENUENTRY), cConvertToAudioThread::progress);
	return mainmenuentry;
    }
    else
	return tr(MAINMENUENTRY); 
}

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

bool cPluginDeluxe::SetupParse(const char *Name, const char *Value)
{
  if (!strcasecmp(Name, "hidemainmenuentry")) DeluxeConfig.hideMainMenuEntry = atoi(Value);
  if (!strcasecmp(Name, "offset")) DeluxeConfig.offset = atoi(Value);
  if (!strcasecmp(Name, "format")) DeluxeConfig.format = Value;
  if (!strcasecmp(Name, "avoidrepeats")) DeluxeConfig.avoidRepeats = atoi(Value);
  if (!strcasecmp(Name, "audioformat")) DeluxeConfig.audioFormat = atoi(Value);
  return true;
}

bool cPluginDeluxe::Service(const char *Id, void *Data)
{
  // Handle custom service requests from other plugins
  return false;
}

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

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

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