/*
 * autosort.c: A channel autosort plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <vdr/plugin.h>
//#include <vdr/sources.h>
//#include <vdr/channels.h>
//#include <vdr/interface.h>

#include "autosort_menu.h"
#include "autosort_main.h"
//#include "autogroup.h"
//#include "autogroups.h"



static const char *VERSION        = "0.0.10";
static const char *DESCRIPTION    = "Channel Autosort";
static const char *MAINMENUENTRY  = "AutoSort Config Test";

// -----------------------------------------------------
// -----------------------------------------------------

class cPluginAutoSort : public cPlugin {
private:
  // Add any member variables or functions you may need here.
  cNewAutoSort *autoSort;
public:
  cPluginAutoSort(void);
  virtual ~cPluginAutoSort();
  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 Start(void);
  virtual void Housekeeping(void);
  virtual const char *MainMenuEntry(void) { if (!hideMenu) return MAINMENUENTRY; else return NULL; }
  virtual cOsdObject *MainMenuAction(void);
  virtual cMenuSetupPage *SetupMenu(void);
  virtual bool SetupParse(const char *Name, const char *Value);
  };

cPluginAutoSort::cPluginAutoSort(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!
  autoSort = NULL;
}

cPluginAutoSort::~cPluginAutoSort()
{
  // Clean up after yourself!
  delete autoSort;
}

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

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

bool cPluginAutoSort::Start(void)
{
  // Start any background activities the plugin shall perform.
  autoSort = new cNewAutoSort;
  return true;
}

void cPluginAutoSort::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
  if (housekeepingRun) autoSort->CheckChannels();
  if (saveSetup) {
    //dsyslog("save here!");
    SetupStore("LastTimeStamp", lastTimeStampSet);
    saveSetup = false;
    }
}

cOsdObject *cPluginAutoSort::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  delete autoSort;
  autoSort = new cNewAutoSort;
  dsyslog("AutoSort: Started complete run");  
  autoSort->CheckChannels(true);
  return NULL;
}
cMenuSetupPage *cPluginAutoSort::SetupMenu(void)
{
  // Return a setup menu in case the plugin supports one.
  return new cMenuAutoSortSetup;
}

bool cPluginAutoSort::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if      (!strcasecmp(Name, "MaxMoves"))         maxMoves          = atoi(Value);
  else if (!strcasecmp(Name, "MessageLevel"))     messageLevel      = atoi(Value);
  else if (!strcasecmp(Name, "MessageTime"))      messageTime       = atoi(Value);
  else if (!strcasecmp(Name, "SeperateNew"))      seperateNew       = atoi(Value);
  else if (!strcasecmp(Name, "SortNew"))          sortNew           = atoi(Value);
  else if (!strcasecmp(Name, "SortUnsorted"))     sortUnsorted      = atoi(Value);
  else if (!strcasecmp(Name, "HousekeepingRun"))  housekeepingRun   = atoi(Value);
  else if (!strcasecmp(Name, "HideMenu"))         hideMenu          = atoi(Value);
  else if (!strcasecmp(Name, "VersionSort"))      versionSorting    = atoi(Value);  
  else if (!strcasecmp(Name, "LastTimeStamp"))    lastTimeStampSet  = atoi(Value);  
  else if (!strcasecmp(Name, "SetTimeStamps"))    writeTimeStamp    = atoi(Value);  
  else if (!strcasecmp(Name, "TimeStampDelta"))   timeStampSetDelta = atoi(Value);  
  else if (!strcasecmp(Name, "UseFixedAuto"))     useFixedAuto      = atoi(Value);
  else if (!strcasecmp(Name, "UseFixedUnsorted")) useFixedUnsorted  = atoi(Value);
  else if (!strcasecmp(Name, "UseFixedNew"))      useFixedNew       = atoi(Value);
  else if (!strcasecmp(Name, "FixedAuto"))        fixedAuto         = atoi(Value);
  else if (!strcasecmp(Name, "FixedUnsorted"))    fixedUnsorted     = atoi(Value);
  else if (!strcasecmp(Name, "FixedNew"))         fixedNew          = atoi(Value);
  else
     return false;
  return true;
}

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