/*
 * Least Cost Routing plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "lcr.h"

cString destinationData[7], priceData[7], pulseData[7];
const char * destinationNamesLocalized[NUMBER_ENTRIES];

#if VDRVERSNUM && VDRVERSNUM < 10321
#error "Sorry, VDR < 1.3.21 not tested - delete this line on your own risk to be a TESTER ;-)!"
#endif

char *retrievalScript =  NULL;

cPluginLcr::cPluginLcr(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!
  vLcrOsd = NULL;
}

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

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

  free(help_str);  
  asprintf(&help_str,
    "  -S CMD,   --script=CMD   data retrieval script (with absolute path)\n"
    "                           of plugin.\n"
    "                           e.g. /usr/local/bin/vdr-lcr-retrieve_data.pl\n");
  return help_str;
}

bool cPluginLcr::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  static struct option long_options[] = {
    { "script", no_argument, NULL, 'S' },
    { NULL }
  };
     
  int c, option_index = 0;
  retrievalScript = NULL;
  while ((c = getopt_long(argc, argv, "S:", long_options, &option_index)) != -1)
  {
    switch (c)
    {
      case 'S': retrievalScript = optarg; break;
      default: return false;
    }         
  }

  // assign the default value for the retrieval script if no option is given
  if ( retrievalScript == NULL )
      retrievalScript = XQUOTE(RETRIEVAL_SCRIPT);
  
  return true;
}

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

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

void cPluginLcr::Stop(void)
{
  // Stop the background activities.
}

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

cOsdObject *cPluginLcr::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  vLcrOsd = new cLcrOsd();
  return vLcrOsd;
}

bool cPluginLcr::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "ZoomIn"))                 lcrConfig.zoomIn                = atoi(Value);
  else if (!strcasecmp(Name, "PreferredEntry"))         lcrConfig.preferredEntry        = atoi(Value);
#if VDRVERSNUM >= 10330
  else if (!strcasecmp(Name, "OtherDisplays"))          lcrConfig.otherDisplays         = atoi(Value);
#endif
  else if (!strcasecmp(Name, "HighlightEntry"))         lcrConfig.highlightEntry        = atoi(Value);
  else if (!strcasecmp(Name, "Destination1"))           lcrConfig.destination[1]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination2"))           lcrConfig.destination[2]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination3"))           lcrConfig.destination[3]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination4"))           lcrConfig.destination[4]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination5"))           lcrConfig.destination[5]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination6"))           lcrConfig.destination[6]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination7"))           lcrConfig.destination[7]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination8"))           lcrConfig.destination[8]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination9"))           lcrConfig.destination[9]        = atoi(Value);
  else if (!strcasecmp(Name, "Destination0"))           lcrConfig.destination[0]        = atoi(Value);
  else if (!strcasecmp(Name, "LogoFrame"))              lcrConfig.logoFrame             = atoi(Value);
  else if (!strcasecmp(Name, "HideMenu"))               lcrConfig.hidemenu              = atoi(Value);
  else if (!strcasecmp(Name, "OSDOffsetX"))             lcrConfig.osdoffsetX            = atoi(Value);
  else if (!strcasecmp(Name, "OSDOffsetY"))             lcrConfig.osdoffsetY            = atoi(Value);
  else if (!strcasecmp(Name, "Theme"))                  lcrConfig.theme                 = atoi(Value);
  else
    return false;

  return true;
}

cMenuLcrSetup::cMenuLcrSetup(void)
{
  themes[eLcrThemeClassic]       = tr("Classic");
  themes[eLcrThemeElchi]         = tr("Elchi");
  themes[eLcrThemeDeepBlue]      = tr("DeepBlue");
  themes[eLcrThemeMoronimo]      = tr("Moronimo");

  Setup();
}

void cMenuLcrSetup::Setup(void)
{
  // store the values temporarily (enables discarding them if you leave the setup with "Back")
  zoomInNew         = lcrConfig.zoomIn;
  preferredEntryNew = lcrConfig.preferredEntry;
  otherDisplaysNew  = lcrConfig.otherDisplays;
  highlightEntryNew = lcrConfig.highlightEntry;
  logoFrameNew      = lcrConfig.logoFrame;
  hidemenuNew       = lcrConfig.hidemenu;
  themeNew          = lcrConfig.theme;
  osdoffsetXNew     = lcrConfig.osdoffsetX;
  osdoffsetYNew     = lcrConfig.osdoffsetY;
  destinationNew[1] = lcrConfig.destination[1];
  destinationNew[2] = lcrConfig.destination[2];
  destinationNew[3] = lcrConfig.destination[3];
  destinationNew[4] = lcrConfig.destination[4];
  destinationNew[5] = lcrConfig.destination[5];
  destinationNew[6] = lcrConfig.destination[6];
  destinationNew[7] = lcrConfig.destination[7];
  destinationNew[8] = lcrConfig.destination[8];
  destinationNew[9] = lcrConfig.destination[9];
  destinationNew[0] = lcrConfig.destination[0];
  
  // do the translations for the selectable strings in the setup
  int i;
  for ( i=0; i < NUMBER_ENTRIES; i++ )
  {
      destinationNamesLocalized[i] = tr(destinationNames[i]);
  }
  int current = Current();

  Clear();
  Add(new cMenuEditBoolItem(  tr("Zoom-in"),                     &zoomInNew,                 tr("no"),           tr("yes")));
  Add(new cMenuEditIntItem(   tr("Preferred entry"),             &preferredEntryNew,         0,                  7));
#if VDRVERSNUM >= 10330
  Add(new cMenuEditBoolItem(  tr("Display on lcdproc & co."),    &otherDisplaysNew,          tr("no"),           tr("yes")));
#endif
  Add(new cMenuEditBoolItem(  tr("Highlight entry"),             &highlightEntryNew,         tr("no"),           tr("yes")));
  Add(new cMenuEditStraItem(  tr("Destination 1"),               &destinationNew[1],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 2"),               &destinationNew[2],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 3"),               &destinationNew[3],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 4"),               &destinationNew[4],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 5"),               &destinationNew[5],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 6"),               &destinationNew[6],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 7"),               &destinationNew[7],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 8"),               &destinationNew[8],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 9"),               &destinationNew[9],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditStraItem(  tr("Destination 0"),               &destinationNew[0],         NUMBER_ENTRIES,     destinationNamesLocalized));
  Add(new cMenuEditBoolItem(  tr("Frame around Logo"),           &logoFrameNew,              tr("no"),           tr("yes")));
  Add(new cMenuEditBoolItem(  tr("Hide main menu entry"),        &hidemenuNew,               tr("no"),           tr("yes")));
  Add(new cMenuEditStraItem(  tr("Theme"),                       &themeNew,                  eLcrThemeMaxNumber, themes));
  Add(new cMenuEditIntItem(   tr("Horizontal offset"),           &osdoffsetXNew,             -50,                50));
  Add(new cMenuEditIntItem(   tr("Vertical offset"),             &osdoffsetYNew,             0,                  300));

  SetCurrent(Get(current));
  Display();
}

void cMenuLcrSetup::Store(void)
{
  SetupStore("ZoomIn",                  lcrConfig.zoomIn         = zoomInNew );
  SetupStore("PreferredEntry",          lcrConfig.preferredEntry = preferredEntryNew );
#if VDRVERSNUM >= 10330
  SetupStore("OtherDisplays",           lcrConfig.otherDisplays  = otherDisplaysNew);
#endif
  SetupStore("HighlightEntry",          lcrConfig.highlightEntry = highlightEntryNew );
  SetupStore("Destination1",            lcrConfig.destination[1] = destinationNew[1] );
  SetupStore("Destination2",            lcrConfig.destination[2] = destinationNew[2] );
  SetupStore("Destination3",            lcrConfig.destination[3] = destinationNew[3] );
  SetupStore("Destination4",            lcrConfig.destination[4] = destinationNew[4] );
  SetupStore("Destination5",            lcrConfig.destination[5] = destinationNew[5] );
  SetupStore("Destination6",            lcrConfig.destination[6] = destinationNew[6] );
  SetupStore("Destination7",            lcrConfig.destination[7] = destinationNew[7] );
  SetupStore("Destination8",            lcrConfig.destination[8] = destinationNew[8] );
  SetupStore("Destination9",            lcrConfig.destination[9] = destinationNew[9] );
  SetupStore("Destination0",            lcrConfig.destination[0] = destinationNew[0] );
  SetupStore("LogoFrame",               lcrConfig.logoFrame      = logoFrameNew );
  SetupStore("HideMenu",                lcrConfig.hidemenu       = hidemenuNew );
  SetupStore("Theme",                   lcrConfig.theme          = themeNew );
  SetupStore("OSDOffsetX",              lcrConfig.osdoffsetX     = osdoffsetXNew );
  SetupStore("OSDOffsetY",              lcrConfig.osdoffsetY     = osdoffsetYNew );
}


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

bool cPluginLcr::Service(const char *Id, void *Data)
{
    if (strcmp(Id,"LcrService-v1.0") == 0 && (const char*)(destinationData[0]) != NULL && strcmp(destinationData[0],"---")
      && lcrConfig.otherDisplays == 1
    )
    {
        if ( Data )
        {
            LcrService_v1_0 *data = (LcrService_v1_0*)Data;
            vLcrOsd->getServiceData( data );
        }
        return true;
    }
    return false;
}

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