/*
 * lcdcontrol.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 <stdio.h>
#include "setup.h"
#include "serial.h"
#include "menu.h"

static const char *VERSION        = "0.0.4";
static const char *DESCRIPTION    = "A plugin to control your TV via serial port";
static const char *MAINMENUENTRY  = "LCD Control";

#define MANUALSTART       600
static const char allowed[] = { "abcdefghijklmnopqrstuvwxyz0123456789/" };


// ------------------------------------- MenuLcdControlSetup ------------------------------------------------------

class cMenuLcdControlSetup : public cMenuSetupPage {
private:
protected:
  virtual void Store(void);
public:
  cMenuLcdControlSetup();
};


cMenuLcdControlSetup::cMenuLcdControlSetup()
{
  Add(new cMenuEditStrItem("Serial Port", LcdControlSetup.serialPort, 11 , allowed));
  Add(new cMenuEditBoolItem("Switch TV on at Startup", &LcdControlSetup.autoOn, "no", "yes"));    
}

void cMenuLcdControlSetup::Store(void)
{
    SetupStore("serialPort", LcdControlSetup.serialPort);
    SetupStore("autoOn", LcdControlSetup.autoOn);
}

// ------------------------------------- MenuLcdControlSetup ------------------------------------------------------

class cPluginLcdcontrol : public cPlugin {
private:
public:
  cPluginLcdcontrol(void);
  virtual ~cPluginLcdcontrol();
  virtual const char *Version(void) { return VERSION; }
  virtual const char *Description(void) { return 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);
  };

cPluginLcdcontrol::cPluginLcdcontrol(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!
}

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

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

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

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

bool cPluginLcdcontrol::Start(void)
{
  // Start any background activities the plugin shall perform.
  LcdControlSetup.configDir = cPlugin::ConfigDirectory(NULL);
  LcdControlSetup.ParseConfigFile();
  time_t Now = time(NULL);
  cTimer *timer = Timers.GetNextActiveTimer();
  time_t Next  = timer ? timer->StartTime() : 0;
  time_t Delta = timer ? Next - Now : 0;
  if ( (!timer || Delta > MANUALSTART) && LcdControlSetup.autoOn == 1) {
    SerialPort.openPort(LcdControlSetup.serialPort);
    SerialPort.writePort(LcdControlSetup.cmdPoweron);
    SerialPort.closePort();
  }
  return true;
}

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

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

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

cString cPluginLcdcontrol::Active(void)
{
  // Return a message string if shutdown should be postponed
  return NULL;
}

cOsdObject *cPluginLcdcontrol::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  //return NULL;
  if ( LcdControlSetup.MenuUpDown != NULL){
  	return LcdControlSetup.MenuUpDown;
  }
  else if ( LcdControlSetup.MenuMainMenu != NULL){
  	return LcdControlSetup.MenuMainMenu;
  } 
  return new cMenuLcdControl();
}

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

bool cPluginLcdcontrol::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values
  if ( Name == "serialPort")  strcpy(LcdControlSetup.serialPort,Value);
  if ( Name == "autoOn") LcdControlSetup.autoOn = (int)Value;   
  return LcdControlSetup.SetupParse(Name, Value);
}

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

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

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

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