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

#include <getopt.h>

#include <vdr/plugin.h>

#include "setup.h"
#include "sportosd.h"
#include "sporttools.h"
#include "i18n.h"


// ------------- cMenu Sport Setup ------------------------------ //

class cMenuSportSetup : public cMenuSetupPage {
private:
 cSportSetup data;
protected:
  virtual void Store(void);
public:
  cMenuSportSetup(void);
};

cMenuSportSetup::cMenuSportSetup(void) {
  data = SportSetup;
  Add(new cMenuEditBoolItem(tr("Update data on Plugin start"),    &data.s_update));
  Add(new cMenuEditIntItem(tr("Breite Position (def. 4)"),        &data.tab1,3,5));
  Add(new cMenuEditIntItem(tr("Ergebnisse Clubs (def. 35)"),      &data.tab2,20,35));
  Add(new cMenuEditIntItem(tr("Ergebnis (def. 8)"),               &data.tab3,5,8));
  Add(new cMenuEditIntItem(tr("Tabelle Club (def. 20)"),          &data.tab4,12,30));
  Add(new cMenuEditIntItem(tr("Tabelle Spieltag (def. 3)"),       &data.tab5,2,4));
  Add(new cMenuEditIntItem(tr("Tabelle Torverhältnis (def. 5)"),  &data.tab6,4,6));
  Add(new cMenuEditIntItem(tr("Tabelle Punkte (def. 2)"),         &data.tab7,2,4));
 // Add(new cMenuEditIntItem(tr("Leer"),                            &data.tab8,1,1));
}


void cMenuSportSetup::Store(void) {
  SportSetup = data;
  SetupStore("AutoUpdate",                                   SportSetup.s_update);
  SetupStore("Tab1",                                         SportSetup.tab1);
  SetupStore("Tab2",                                         SportSetup.tab2);
  SetupStore("Tab3",                                         SportSetup.tab3);
  SetupStore("Tab4",                                         SportSetup.tab4);
  SetupStore("Tab5",                                         SportSetup.tab5);
  SetupStore("Tab6",                                         SportSetup.tab6);
  SetupStore("Tab7",                                         SportSetup.tab7);
 // SetupStore("Tab8",                                         SportSetup.tab8);
}



// -------------- cPluginSport ----------------------------------- //

static const char *VERSION        = "0.0.1";
static const char *DESCRIPTION    = "Zeigt Sportergebnisse auf dem OSD";
static const char *MAINMENUENTRY  = "Sport";

class cPluginSport : public cPlugin {
private:

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

cPluginSport::cPluginSport(void)
{

}

cPluginSport::~cPluginSport()
{

}

const char *cPluginSport::CommandLineHelp(void) {

  static char *help_str = NULL;

  free(help_str);
  asprintf(&help_str,
                   	"  -D DIR    --data=DIR    DIR to datafiles\n"
		     	"  -S DIR    --script=DIR  DIR to downloadscript\n");

  return help_str;
}

bool cPluginSport::ProcessArgs(int argc, char *argv[]) {

  static struct option long_options[] = {
       { "data",          required_argument, NULL, 'D' },
       { "script",        required_argument, NULL, 'S' },
       { NULL }
     };

  int c, option_index = 0;
  while ((c = getopt_long(argc, argv, "D:S:", long_options, &option_index)) != -1) {
      switch (c) {
         case 'D': DataDir     = optarg; break;
         case 'S': ScriptDir   = optarg; break;
	 default: return false;
	 }
  }

  return true;
}

bool cPluginSport::Initialize(void) {

  if (DataDir==NULL) {
    DataDir = strdup(ConfigDirectory(PLUGIN_NAME_I18N));
    dsyslog("%s: Parameter '-D' not set.\n"
            " Set default value : %s\n", PLUGIN_NAME_I18N, DataDir);
  }

  if (ScriptDir==NULL) {
    ScriptDir = strdup(ConfigDirectory(PLUGIN_NAME_I18N));
    dsyslog("%s: Parameter '-S' not set.\n"
            " Set default value : %s\n", PLUGIN_NAME_I18N, ScriptDir);
  }

  return true;
}

bool cPluginSport::Start(void)
{
/*  if (SportSetup.s_update == true) {
    char *buffer;


	asprintf(&buffer, "%s%s \"%s\"", ScriptDir, "/fussball.sh", DataDir);

       dsyslog("%s: Executing as autoupdate: '%s'\n", PLUGIN_NAME_I18N, buffer);

    	system(buffer);

    free(buffer);	
  }	*/

  RegisterI18n(Phrases);
  return true;
}

void cPluginSport::Stop(void)
{

}

void cPluginSport::Housekeeping(void)
{

}

cOsdObject *cPluginSport::MainMenuAction(void)
{
	  if (SportSetup.s_update == true) {
    char *buffer;
  Skins.Message(mtStatus, tr("Daten werden gelesen. Bitte warten!"));

	asprintf(&buffer, "%s%s \"%s\"", ScriptDir, "/fussball.sh", DataDir);

       dsyslog("%s: Executing as autoupdate: '%s'\n", PLUGIN_NAME_I18N, buffer);

    	system(buffer);

    free(buffer);	
  }	
	
	
	
	
  return new cSport();
}


bool cPluginSport::Service(const char *Id, void *Data)
{
   return false;
}

const char **cPluginSport::SVDRPHelpPages(void)
{
   return NULL;
}

cString cPluginSport::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
  return NULL;
}

cMenuSetupPage *cPluginSport::SetupMenu(void)
{
  return new cMenuSportSetup;
}

bool cPluginSport::SetupParse(const char *Name, const char *Value)
{
  if    (!strcasecmp(Name, "AutoUpdate"))   SportSetup.s_update    = atoi(Value);
    else if  (!strcasecmp(Name, "Tab1"))    SportSetup.tab1        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab2"))    SportSetup.tab2        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab3"))    SportSetup.tab3        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab4"))    SportSetup.tab4        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab5"))    SportSetup.tab5        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab6"))    SportSetup.tab6        = atoi(Value);
    else if  (!strcasecmp(Name, "Tab7"))    SportSetup.tab7        = atoi(Value);
  //  else if  (!strcasecmp(Name, "Tab8"))    SportSetup.tab8        = atoi(Value);

    else
      return false;

  return true;
}




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