/*
 * c.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: cc.c,v 1.2 2005/11/02 16:08:26 aegyssus Exp $
 */

#include <getopt.h>
#include <stdlib.h>
#include <vdr/interface.h>
#include <vdr/plugin.h>

#include "i18n.h"
#include "ccosd.h"
#include "commons.h"

static const char *VERSION        = "0.0.1pre1";
static const char *DESCRIPTION    = "Closed Caption Plugin for NTSC TV";
//static const char *MAINMENUENTRY  = "Closed Caption";

// Global variables that control the overall behaviour:
int ccType = 1; //Stores the user selection for desired type of CC
int enabled = true; //The user can enable/disable the plugin

class cPluginCC : public cPlugin {
private: 
  cCcOsd *osd; //This is the main OSD object which will handle the way the text is displayed through OSD
public:
  cPluginCC(void); //Ensures that OSD pointer is null
  virtual ~cPluginCC(); //Remove the OSD object, cleaning completely the memory that this plugin used.
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return tr(DESCRIPTION); }
  virtual const char *CommandLineHelp(void); //Shows a basic usage message
  virtual cMenuSetupPage *SetupMenu(void); //Creates the setup menu object which will be shown in OSD menu
  virtual bool SetupParse(const char *Name, const char *Value); //Parses the parameters from OSD setup menu
  virtual bool Start(void); //Initializes the OSD object
  //virtual bool ProcessArgs(int argc, char *argv[]); //Not used yet
  //virtual void Housekeeping(void); //Not used yet
  //virtual const char *MainMenuEntry(void) { return tr(MAINMENUENTRY); } //Not used yet
  //virtual cOsdObject *MainMenuAction(void); //Not used yet
  
  };

// --- cMenuSetupCC -------------------------------------------------------

class cMenuSetupCC : public cMenuSetupPage {
private:
  int newCCType; //new value for CC type variable
  int newEnabled; //new value for "enabled" flag
protected:
  virtual void Store(void); //Stores all changes made through OSD setup menu
public:
  cMenuSetupCC(void); //initializes the properties and adds them into the OSD menu
  };

cMenuSetupCC::cMenuSetupCC(void)
{
  newCCType = ccType;
  newEnabled = enabled;
  Add(new cMenuEditIntItem( tr("CC type"), &newCCType, 1, 4));
  Add(new cMenuEditBoolItem(tr("Enabled"), &newEnabled));
}

void cMenuSetupCC::Store(void)
{
  SetupStore("CC type", ccType = newCCType);
  SetupStore("Enabled", enabled = newEnabled);
}

// --- cPluginCC ----------------------------------------------------------

cPluginCC::cPluginCC(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!
  osd = NULL;  
  dprint(1, "CC: Plugin CC created.\n");
}

cPluginCC::~cPluginCC()
{
	// Clean up after yourself!
	if(osd!=NULL)
		delete osd;
  
  dprint(1, "CC: Plugin CC deleted.");
}

const char *cPluginCC::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  char *version = new char[50]; 
  sprintf(version, "CC for VDR - version %s\n\tUsage: -Pcc", Version());
  return version;
}

bool cPluginCC::Start(void)
{
  // Start any background activities the plugin shall perform.
  dprint(1, "CC: Starting...\n");
  RegisterI18n(Phrases);
  if(osd==NULL){
  	osd = new cCcOsd();
	osd->setActive(true);
	osd->setCCType(ccType);
  }
  return true;
}

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

bool cPluginCC::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "CC Type")) ccType = atoi(Value);
  else if (!strcasecmp(Name, "enabled")) enabled = atoi(Value);
  else
     return false;
  return true;
}


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