#ifndef __ALCD_CPP
#define __ALCD_CPP

#include <getopt.h>
#include <stdlib.h>
// Statische Variablen
static const char *VERSION        = "1.0.0";
static const char *DESCRIPTION    = "Activy300 LCD-Plugin";
static const char *MAINMENUENTRY  = NULL;

#include "alcd.h"
#include "i18n.h"

#define SETUP_SLEEPTIME           "Sleeptime"
#define SETUP_SCROLLSPEED         "ScrollSpeed"
#define SETUP_SCROLLING           "Scrolling"
#define SETUP_UMLAUTS             "Umlauts"
#define SETUP_LEDRECORD           "LEDRecord"
#define SETUP_LCDBRIGHTNESS       "LCDBrightness"
#define SETUP_LCDAVAILABLE        "LCDavailable"
#define SETUP_LCDBRIGHTNESSONEXIT "LCDBrightnessOnExit"
#define SETUP_PROGRESS            "ProgressSign"
#define SETUP_MSGTIME             "MessageTime"
#define PROGRESS_SIGNS            "*#>|oO0X"

char *resetScript = "/etc/init.d/activy.sh";

// sonstige Variablen
unsigned int ScrollSpeed = 3;
bool Umlauts = false;              // Show Umlauts
int Scrolling = SCROLL_BLOCK;      // Kind of scrolling
int LEDRecord = LEDREC_GREEN;      // Message LED
int LCDBrightness = BRIGHTEST;     // Brightness 0 == bright, 4 == dark...
int LCDBrightnessOnExit = DARKEST;
bool LCDavailable = true;
int MsgTime = 5;
int SleepTime = 10;
char ProgChar = '#';

///////////////////////////////////////////////////
// activy300 LCD plugin for VDR
//
// Original written by:
//
// Meinrad Sauter <meinrad.sauter@gmx.de>
// andreas 'randy' weinberger <vdr@smue.org>
//
// rearranged, beautified and rewritten by:
//
// Markus Geisler <geisler@primusnetz.de>
//
// optimized by:
// Sascha Quint <sascha.quint@gmx.net>

///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
// Class cPluginAlcd Member Implementations

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

cPluginAlcd::~cPluginAlcd()
{
   // Clean up after yourself!
   delete aLcd;
}

const char *cPluginAlcd::CommandLineHelp(void)
{
   // Return a string that describes all known command line options.
   static char *help_str=0;

   free( help_str );
   asprintf( &help_str, "  -r <file>,  --resetscript=<file>  script to execute after pic reset\n"
             "            (usually something like /etc/init.d/activy.sh)\n" );
   return help_str;
}

bool cPluginAlcd::ProcessArgs(int argc, char *argv[])
{
   // Implement command line argument processing here if applicable.
   static struct option long_options[] = {
      { "resetscript", required_argument, NULL, 'r'},
      { NULL}};

   int c, option_index = 0;

   while( ( c = getopt_long( argc, argv, "r:", long_options, &option_index ) ) != -1 )
   {
      switch( c )
      {
         case 'r':  resetScript = optarg; break;
         default:  return false;
      }
   }

   return true;
}

bool cPluginAlcd::Start(void)
{
//     	isyslog("Calling cPluginAlcd::Start");
   // Start any background activities the plugin shall perform.
   RegisterI18n(Phrases);
   aLcd = new cLCD();
   aLcd->Start();
   return true;
}

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

cOsdMenu *cPluginAlcd::MainMenuAction(void)
{
   // Perform the action when selected from the main VDR menu.
   return NULL;
}


///////////////////////////////////////////////////////////
// Class cMenuSetupAlcd Member Implementations

cMenuSetupAlcd::cMenuSetupAlcd(void)
{
   static const char *LEDRec[2];
   LEDRec[LEDREC_RED] = tr("red flashlight");
   LEDRec[LEDREC_GREEN] = tr("green");
   static const char *LCDBright[5];
   LCDBright[BRIGHTEST] = tr("superbright");
   LCDBright[BRIGHT] = tr("bright");
   LCDBright[MEDIUM] = tr("semibright");
   LCDBright[DARK] = tr("dark");
   LCDBright[DARKEST] = tr("off");
   newScrollSpeed = ScrollSpeed;
   newSleepTime = SleepTime;
   newScrolling = Scrolling;
   newMsgTime = MsgTime;
   static const char *Scroll[2];
   Scroll[SCROLL_BLOCK] = tr("By block");
   Scroll[SCROLL_CHAR] = tr("By character");
   newUmlauts = Umlauts;
   static const char *OnOff[2];
   OnOff[0] = tr("off");
   OnOff[1] = tr("on");
   newLEDRecord = LEDRecord;
   newLCDBrightness = LCDBrightness;
   newLCDBrightnessOnExit = LCDBrightnessOnExit;
   newProgChar = ProgChar;
   Add(new cMenuEditStraItem( tr("Record LED"),  &newLEDRecord, 2, LEDRec));
   Add(new cMenuEditBoolItem( tr("Display available"), (int *)&LCDavailable ));
   Add(new cMenuEditIntItem( tr("Sleeptime in deciseconds"), &newSleepTime, 1, 100));
   Add(new cMenuEditIntItem( tr("Message display (in seconds)"), &newMsgTime, 1, 9999));
   Add(new cMenuEditIntItem( tr("Scroll speed (factor of Sleeptime)"), &newScrollSpeed, 0, 100));
   Add(new cMenuEditStraItem( tr("Scroll behaviour"),  &newScrolling, 2, Scroll));
   Add(new cMenuEditStraItem( tr("Umlauts"),  &newUmlauts, 2, OnOff));
   Add(new cMenuEditStraItem( tr("Display Brightness"), &newLCDBrightness, 5, LCDBright));
   Add(new cMenuEditStraItem( tr("Display Brightness on exit"), &newLCDBrightnessOnExit, 5, LCDBright));
   Add(new cMenuEditChrItem( tr("Progress Sign"), &newProgChar, (const char *)PROGRESS_SIGNS));
}

void cMenuSetupAlcd::Store(void)
{
   SetupStore(SETUP_SLEEPTIME, SleepTime = newSleepTime);
   SetupStore(SETUP_SCROLLSPEED, ScrollSpeed = newScrollSpeed);
   SetupStore(SETUP_SCROLLING, Scrolling = newScrolling);
   SetupStore(SETUP_UMLAUTS, Umlauts = newUmlauts);
   SetupStore(SETUP_LEDRECORD, LEDRecord = newLEDRecord);
   SetupStore(SETUP_LCDBRIGHTNESS, LCDBrightness = newLCDBrightness);
   SetupStore(SETUP_LCDAVAILABLE, LCDavailable);
   SetupStore(SETUP_LCDBRIGHTNESSONEXIT, LCDBrightnessOnExit = newLCDBrightnessOnExit);
   SetupStore(SETUP_PROGRESS, ProgChar = newProgChar);
   SetupStore(SETUP_MSGTIME, MsgTime = newMsgTime);
}

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

bool cPluginAlcd::SetupParse(const char *Name, const char *Value)
{
   // Parse your own setup parameters and store their values.
   if( !strcasecmp(Name, SETUP_SCROLLSPEED) )
      ScrollSpeed = atoi(Value);
   else if( !strcasecmp(Name, SETUP_SLEEPTIME) )
      SleepTime = atoi(Value);
   else if( !strcasecmp(Name, SETUP_MSGTIME) )
      MsgTime = atoi(Value);
   else if( !strcasecmp(Name, SETUP_LEDRECORD) )
      LEDRecord = atoi(Value);
   else if( !strcasecmp(Name, SETUP_UMLAUTS) )
      Umlauts = atoi(Value);
   else if( !strcasecmp(Name, SETUP_SCROLLING) )
      Scrolling = atoi(Value);
   else if( !strcasecmp(Name, SETUP_LCDBRIGHTNESS) )
      LCDBrightness = atoi(Value);
   else if( !strcasecmp(Name, SETUP_LCDAVAILABLE) )
      LCDavailable = atoi(Value);
   else if( !strcasecmp(Name, SETUP_LCDBRIGHTNESSONEXIT) )
      LCDBrightnessOnExit = atoi(Value);
   else if( !strcasecmp(Name, SETUP_PROGRESS) )
    	ProgChar = (char)atoi(Value);
   else
      return false;

   return true;
}


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

#endif
