
#include <string>
#include <fstream>
#include <vdr/menu.h>

#include "vdr/osd.h"
#include "vdr/menuitems.h"

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

/* ########################################################
   -------------- H A U P T M E N Ü -----------------------
/  ######################################################## */

cSport::cSport(void)
:cOsdMenu(tr("Hauptmenü"), GetTab(1), GetTab(4))

{

  std::string sz_file;
  sz_file = DataDir;
  sz_file = sz_file + "/menu.txt";

  SetHelp(NULL,NULL,tr("Refresh"),tr("Exit"));

  if(loadUebersicht(sz_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, sz_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, sz_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
    Add(new cOsdItem(hk("HELP:")));
    Add(new cOsdItem(hk("Press 'Yellow' to refresh the data.")));
    Add(new cOsdItem(hk("Check your systemlog for errormessages.")));
    Add(new cOsdItem(hk("Turn on 'Autoupdate' (not recommened).")));
    Add(new cOsdItem(hk("Try other paths with commandline flags.")));
    Add(new cOsdItem(hk("If nothing helps, get an internetconnection ;).")));
  }

  Display();
}
//##################################################################
cSport::~cSport()
{
}
int cSport::GetTab(int Tab) {
  if(Tab == 1) return SportSetup.tab1;
  if(Tab == 4) return SportSetup.tab2;

  return 0;
}


//##################################################################
int cSport::loadUebersicht(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(0, 20);


      snprintf(ranking,sizeof(ranking),"%02d.\t%s",count +1, Verein.c_str());

      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}
//###########################################################

void cSport::reloadData() {
  std::string file;
  char *buffer;

  Skins.Message(mtStatus, tr("Reloading data..."));

  file = DataDir;
  file = file + "/menu.txt";


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

int reload = system(buffer);


  if(reload != 0) {
    Skins.Message(mtError, tr("Error while reloading data"));
    esyslog("%s: Can't reload data !  '%s'\n", PLUGIN_NAME_I18N, buffer);
    }
  else {
    dsyslog("%s: Reloaded data  '%s'\n", PLUGIN_NAME_I18N, buffer);
    Skins.Message(mtStatus, NULL);
    Clear();
    Skins.Flush();
    loadUebersicht(file.c_str());

    Display();

    }
  free(buffer);
}

//########################################################

eOSState cSport::Execute(void) {
  cOsdItem *item = (cOsdItem*)Get(Current());
  if(item) {
//    return AddSubMenu(new cMenuText("Info zu Verein", item->Text()));

// oder :
// Moeglichkeit zu einzelnen Vereinen was anzuzeigen.
// Besser waere ne eigene Klasse (z.B. cSportInfos) dafuer zu erstellen und hier nur den "Index" auswerten.
// Anstatt 'return AdSubMenu...blahblah' dann hier 'return new cSportInfos(Index);

 ///     return AddSubMenu(new cMenuText(tr("Erste Bundesliga"),"cHrIsZ"));

    if(strstr(item->Text(),"Erste Bundesliga"))  // > Zeile202
    return AddSubMenu ( new cErsteBundesliga()); //ehemals cChamp

    else if (strstr(item->Text(),"Zweite Bundesliga"))
    return AddSubMenu (new cZweiteBundesliga());

    else if (strstr(item->Text(),"Champions Leage"))
    return AddSubMenu (new cChampionsLeage());

    else if (strstr(item->Text(),"DFB Pokal"))
    return AddSubMenu (new cDfbPokal());

    else if (strstr(item->Text(),"UEFA Pokal"))
    return AddSubMenu (new cUEFAPokal());

    }
  return osContinue;

}
//#############################################################
eOSState cSport::ProcessKey(eKeys Key)
{

  bool hadSubmenu = HasSubMenu();

  eOSState state = cOsdMenu::ProcessKey(Key);

  if(hadSubmenu && !HasSubMenu())
    return osContinue;

  if(state==osUnknown) {
    switch (Key) {


        case kYellow:
	   reloadData();
	   break;

        case kBlue:
	   return osEnd;
	   break;

	case kOk:
	   return Execute();
	   break;

        default:
	   state = osContinue;
    }
  }

 return state;
}

//################################################################################
// ---------- E R S T E    B U N D E S L I G A    E R G E B N I S S E ------------
//################################################################################

cErsteBundesliga::cErsteBundesliga(void)
:cOsdMenu(tr("1. Bundesliga LIVE Ergebnisse"), GetTab(2), GetTab(3))
{

  std::string eble_file;
  eble_file = DataDir;
  eble_file = eble_file + "/1BL_Ergebnisse.txt";

  SetHelp(tr("Tabelle"),NULL,NULL,NULL);

  if(load1BLE(eble_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, eble_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, eble_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//##################################################################
cErsteBundesliga::~cErsteBundesliga()
{
}
int cErsteBundesliga::GetTab(int Tab) {
  if(Tab == 2) return SportSetup.tab2;
  if(Tab == 3) return SportSetup.tab3;

  return 0;
}


//##################################################################
int cErsteBundesliga::load1BLE(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(1, 32);

     // Games  = line.substr(len -14, 8);
     Games  = line.substr(len -9, 9);



  //    snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s",count +1, Verein.c_str(), Games.c_str());
     snprintf(ranking,sizeof(ranking),"%s\t%s", Verein.c_str(), Games.c_str());

 //     snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}

//#############################################################################
eOSState cErsteBundesliga::ProcessKey(eKeys Key)
{

bool hadSubmenu = HasSubMenu();

    eOSState state = cOsdMenu::ProcessKey(Key);

if(hadSubmenu && !HasSubMenu())
return osContinue;



  if(state==osUnknown) {
    switch (Key) {


    case kRed:
	    return AddSubMenu ( new cErsteBundesligaTabelle());


     	   break;


    case kOk:
	    return osBack;
	   break;


        default:
	   state=osContinue;
    }
  }

 return state;
}

//################################################################################
// ---------- E R S T E    B U N D E S L I G A    T A B E L L E ------------------
//################################################################################


cErsteBundesligaTabelle::cErsteBundesligaTabelle(void)

:cOsdMenu(tr("1. Bundesliga Live Tabelle"),GetTab(1), GetTab(4), GetTab(5), GetTab(6), GetTab(7))
{

  std::string eblt_file;
  eblt_file = DataDir;
  eblt_file = eblt_file + "/1BL_Tabelle.txt";

  SetHelp(NULL,NULL,NULL,NULL);

  if(loadEBLTabelle(eblt_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, eblt_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, eblt_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//####################################################################
cErsteBundesligaTabelle::~cErsteBundesligaTabelle()
{
}

int cErsteBundesligaTabelle::GetTab(int Tab) {

  if(Tab == 1) return SportSetup.tab1;
  if(Tab == 4) return SportSetup.tab4;
  if(Tab == 5) return SportSetup.tab5;
  if(Tab == 6) return SportSetup.tab6;
  if(Tab == 7) return SportSetup.tab7;

  return 0;
}

//####################################################################


int cErsteBundesligaTabelle::loadEBLTabelle(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;
  std::string Goals;
  std::string Points;
  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      //Verein
      Verein = line.substr(5, 20);
      //Games
      Games  = line.substr(len -14, 3);
      //Goals
      Goals  = line.substr(len -10, 6);
      //Points
      Points = line.substr(len -2, 2);

      snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());

      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}
//###################################################
eOSState cErsteBundesligaTabelle::ProcessKey(eKeys Key)
{

  bool hadSubmenu = HasSubMenu();

  eOSState state = cOsdMenu::ProcessKey(Key);

  if(hadSubmenu && !HasSubMenu())
    return osContinue;

  if(state==osUnknown) {
    switch (Key) {

case kOk:
return osBack;
break;



        default:
	   state = osContinue;
    }
  }

 return state;
}

//################################################################################
// ---------- C H A M P I O N S   L E A G E    E R G E B N I S S E ---------------
//################################################################################

 cChampionsLeage::cChampionsLeage(void)
:cOsdMenu(tr("Champions Leage LIVE Ergebnisse"), GetTab(2), GetTab(3))
{

  std::string cle_file;
  cle_file = DataDir;
  cle_file = cle_file + "/CL_Ergebnisse.txt";

  //SetHelp(tr("Tabelle"),NULL,NULL,NULL);
  SetHelp(NULL,NULL,NULL,NULL);

  if(loadCLE(cle_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, cle_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, cle_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//##################################################################
cChampionsLeage::~cChampionsLeage()
{
}
int cChampionsLeage::GetTab(int Tab) {
  if(Tab == 2) return SportSetup.tab2;
  if(Tab == 3) return SportSetup.tab3;

  return 0;
}


//##################################################################
int cChampionsLeage::loadCLE(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(1, 32);

     // Games  = line.substr(len -14, 8);
     Games  = line.substr(len -9, 9);



  //    snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s",count +1, Verein.c_str(), Games.c_str());
     snprintf(ranking,sizeof(ranking),"%s\t%s", Verein.c_str(), Games.c_str());

 //     snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}

//#############################################################################
eOSState cChampionsLeage::ProcessKey(eKeys Key)
{

bool hadSubmenu = HasSubMenu();

    eOSState state = cOsdMenu::ProcessKey(Key);

if(hadSubmenu && !HasSubMenu())
return osContinue;



  if(state==osUnknown) {
    switch (Key) {


    case kOk:
	    return osBack;
	   break;


        default:
	   state=osContinue;
    }
  }

 return state;
}



//################################################################################
// ---------- 2   B U N D E S L I G A    E R G E B N I S S E ---------------
//################################################################################

 cZweiteBundesliga::cZweiteBundesliga(void)
:cOsdMenu(tr("2. Bundesliga LIVE Ergebnisse"), GetTab(2), GetTab(3))
{

  std::string zble_file;
  zble_file = DataDir;
  zble_file = zble_file + "/2BL_Ergebnisse.txt";

  //SetHelp(tr(NULL,NULL,NULL,NULL);
  SetHelp(NULL,NULL,NULL,NULL);

  if(loadZBLE(zble_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, zble_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, zble_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//##################################################################
cZweiteBundesliga::~cZweiteBundesliga()
{
}
int cZweiteBundesliga::GetTab(int Tab) {
  if(Tab == 2) return SportSetup.tab2;
  if(Tab == 3) return SportSetup.tab3;

  return 0;
}


//##################################################################
int cZweiteBundesliga::loadZBLE(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(1, 32);

     // Games  = line.substr(len -14, 8);
     Games  = line.substr(len -9, 9);



  //    snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s",count +1, Verein.c_str(), Games.c_str());
     snprintf(ranking,sizeof(ranking),"%s\t%s", Verein.c_str(), Games.c_str());

 //     snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}

//#############################################################################
eOSState cZweiteBundesliga::ProcessKey(eKeys Key)
{

bool hadSubmenu = HasSubMenu();

    eOSState state = cOsdMenu::ProcessKey(Key);

if(hadSubmenu && !HasSubMenu())
return osContinue;



  if(state==osUnknown) {
    switch (Key) {


    case kOk:
	    return osBack;
	   break;


        default:
	   state=osContinue;
    }
  }

 return state;
}


//################################################################################
// ---------- D F B    E R G E B N I S S E ---------------
//################################################################################

 cDfbPokal::cDfbPokal(void)
:cOsdMenu(tr("DFB LIVE Ergebnisse"), GetTab(2), GetTab(3))
{

  std::string dfbe_file;
  dfbe_file = DataDir;
  dfbe_file = dfbe_file + "/DFB_Ergebnisse.txt";

  //SetHelp(tr(NULL,NULL,NULL,NULL);
  SetHelp(NULL,NULL,NULL,NULL);

  if(loadDFBE(dfbe_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, dfbe_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, dfbe_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//##################################################################
cDfbPokal::~cDfbPokal()
{
}
int cDfbPokal::GetTab(int Tab) {
  if(Tab == 2) return SportSetup.tab2;
  if(Tab == 3) return SportSetup.tab3;

  return 0;
}


//##################################################################
int cDfbPokal::loadDFBE(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(1, 32);

     // Games  = line.substr(len -14, 8);
     Games  = line.substr(len -9, 9);



  //    snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s",count +1, Verein.c_str(), Games.c_str());
     snprintf(ranking,sizeof(ranking),"%s\t%s", Verein.c_str(), Games.c_str());

 //     snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}

//#############################################################################
eOSState cDfbPokal::ProcessKey(eKeys Key)
{

bool hadSubmenu = HasSubMenu();

    eOSState state = cOsdMenu::ProcessKey(Key);

if(hadSubmenu && !HasSubMenu())
return osContinue;



  if(state==osUnknown) {
    switch (Key) {


    case kOk:
	    return osBack;
	   break;


        default:
	   state=osContinue;
    }
  }

 return state;
}

 //################################################################################
// ---------- U E F A    E R G E B N I S S E ---------------
//################################################################################

cUEFAPokal::cUEFAPokal(void)
:cOsdMenu(tr("UEFA LIVE Ergebnisse"), GetTab(2), GetTab(3))
{

  std::string uefae_file;
  uefae_file = DataDir;
  uefae_file = uefae_file + "/UEFA_Ergebnisse.txt";

  //SetHelp(tr(NULL,NULL,NULL,NULL);
  SetHelp(NULL,NULL,NULL,NULL);

  if(loadUEFA(uefae_file.c_str())) {

    dsyslog("%s: Loaded ranking from '%s'\n",PLUGIN_NAME_I18N, uefae_file.c_str());
    }
  else {
    esyslog("%s: Can't load ranking from '%s'\n",PLUGIN_NAME_I18N, uefae_file.c_str());
    Skins.Message(mtError, tr("Error while loading ranking"));
  }

  Display();
}
//##################################################################
cUEFAPokal::~cUEFAPokal()
{
}
int cUEFAPokal::GetTab(int Tab) {
  if(Tab == 2) return SportSetup.tab2;
  if(Tab == 3) return SportSetup.tab3;

  return 0;
}


//##################################################################
int cUEFAPokal::loadUEFA(const char *r_file) {
  std::ifstream filestr;
  std::string line;
  std::string Verein;
  std::string Games;

  char ranking[80];
  int len=0;
  bool result=false;

  filestr.open(r_file);
  if(filestr) {
    dsyslog("%s: Open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    while (getline(filestr, line, '\n')) {
      len = line.length();
      Verein = line.substr(1, 32);

     // Games  = line.substr(len -14, 8);
     Games  = line.substr(len -9, 9);



  //    snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s",count +1, Verein.c_str(), Games.c_str());
     snprintf(ranking,sizeof(ranking),"%s\t%s", Verein.c_str(), Games.c_str());

 //     snprintf(ranking,sizeof(ranking),"%02d.\t%s\t%s   %s   %s",count +1, Verein.c_str(), Games.c_str(), Goals.c_str(), Points.c_str());
      cOsdItem *item = new cOsdItem(hk(ranking));
      Add(item,osUnknown);
    }
    filestr.close();
    result = true;
  }
  else {
    esyslog("%s: Can't open ranking '%s'\n",PLUGIN_NAME_I18N, r_file);
    result = false;
    }

  return result;
}

//#############################################################################
eOSState cUEFAPokal::ProcessKey(eKeys Key)
{

bool hadSubmenu = HasSubMenu();

    eOSState state = cOsdMenu::ProcessKey(Key);

if(hadSubmenu && !HasSubMenu())
return osContinue;



  if(state==osUnknown) {
    switch (Key) {


    case kOk:
	    return osBack;
	   break;


        default:
	   state=osContinue;
    }
  }

 return state;
}




