/*
 * cdda.c: The plugin main source file and main interface
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: cdda.c,v 1.1.1.5 2005/06/27 13:34:02 cm Exp $
 */

#include <vdr/plugin.h>
#include <getopt.h>
#include "cdda_i18n.h"
#include "cdda_menu.h"
#include "cdda_conf.h"
#include "cdda_device.h"

cCddaConfiguration oConfig;
cCddaDevice *po_Device;

static const char *VERSION        = "0.0.4";
static const char *DESCRIPTION    = "music cd player";
static const char *MAINMENUENTRY  = "CD Player";

class cPluginCdda : public cPlugin {
private:
  // Add any member variables or functions you may need here.
public:
  cPluginCdda(void);
  virtual ~cPluginCdda();
  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 const char *MainMenuEntry(void) { return oConfig.allowMenuEntry() ? tr(MAINMENUENTRY) : NULL; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  };

cPluginCdda::cPluginCdda(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!

  oConfig.SetDevice("/dev/cdrom");
}

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

const char *cPluginCdda::CommandLineHelp(void)
{
  static char *p_help = NULL;

  if (NULL == p_help)
    asprintf(&p_help, "  -d DEVICE,    --device=DEVICE (default: /dev/cdrom)\n"
                      "  -c DIRECTORY, --cddbDir=DIRECTORY");
  return p_help;
}

bool cPluginCdda::ProcessArgs(int argc, char *argv[])
{
  int c;

  static struct option long_options[] = {
    {"device", required_argument, NULL, 'd'},
    {"cddbDir", required_argument, NULL, 'c'},
    {       0,                 0,    0,   0}
  };

  while (-1 != (c = getopt_long(argc, argv, "c:d:", long_options, NULL))) {
    switch (c) {
      case 'c':
        oConfig.SetCddbDir(optarg);
        break;
      case 'd':
        oConfig.SetDevice(optarg);
        break;
      default:
        return false;
    }
  }
  return true;
}

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

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

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

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

cOsdObject *cPluginCdda::MainMenuAction(void)
{
  if (oConfig.isAutoReplay()) {
    cCddaPlaylist *po_playlist = new cCddaPlaylist();

    if (NULL == po_Device)
      po_Device = new cCddaDevice();
    po_Device->RefIncr();

    if (! po_Device->isOpen())
      po_Device->Open(oConfig.GetDevice());

    if (po_Device->isOpen() && po_Device->Disc()->isAudio()) {
      if (cpOff != oConfig.GetCddbPriority())
        if (NULL == po_Device->Disc()->Cdtext()->GetTitle() || cpAlways == oConfig.GetCddbPriority())
          po_Device->Disc()->ReadCddb(oConfig.GetCddbServerFqdn(),
                                      oConfig.GetCddbServerPort(),
                                      oConfig.allowCddbCache() ? oConfig.GetCddbDir() : NULL);

      po_playlist->SetType(ptAll);
      po_playlist->Load(1, po_Device->Disc()->Tracks()->GetFirstNum(), po_Device->Disc()->Tracks()->GetNumTracks());
      cControl::Launch(new cCddaControl(po_playlist));
    }

    po_Device->RefDecr();
    if (0 == po_Device->RefCount()) {
      po_Device->Close();
      DELETENULL(po_Device);
    }

    po_playlist = NULL;
    return NULL;
  } else
    return new cCddaMenu();
}

cMenuSetupPage *cPluginCdda::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.

  return new cCddaSetupMenu();
}

bool cPluginCdda::SetupParse(const char *Name, const char *Value)
{
  if (!strcasecmp(Name, "menuEntry"))
    oConfig.SetMenuEntry(atoi(Value));
  else if (!strcasecmp(Name, "skipSeconds"))
    oConfig.SetSkipSeconds(atoi(Value));
  else if (!strcasecmp(Name, "audioOnly"))
    oConfig.SetAudioOnly(atoi(Value));
  else if (!strcasecmp(Name, "skipBackMargin"))
    oConfig.SetSkipBackMargin(atoi(Value));
  else if (!strcasecmp(Name, "autoReplay"))
    oConfig.SetAutoReplay(atoi(Value));
  else if (!strcasecmp(Name, "cddbServerFqdn"))
    oConfig.SetCddbServerFqdn(Value);
  else if (!strcasecmp(Name, "cddbServerPort"))
    oConfig.SetCddbServerPort(atoi(Value));
  else if (!strcasecmp(Name, "cddbPriority"))
    oConfig.SetCddbPriority((eCddbPriority)atoi(Value));
  else if (!strcasecmp(Name, "cddbCache"))
    oConfig.SetCddbCache(atoi(Value));
  else if (!strcasecmp(Name, "infoView"))
    oConfig.SetDisplayInfoView((eDisplayInformationView)atoi(Value));
  else
    return false;

  return true;
}

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