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

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

#include "soccerosd.h"
#include "soccertools.h"
#include "setup.h"
#include "i18n.h"


// -------------- cSoccer ----------------------------

cSoccer::cSoccer(void)
//:cOsdMenu(tr("Ranking"))
:cOsdMenu(tr("Ranking"), GetTab(1), GetTab(2), GetTab(3), GetTab(4), GetTab(5))

{

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

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

  if(loadRanking(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();
}

cSoccer::~cSoccer()
{
}


int cSoccer::GetTab(int Tab) {
  if(Tab == 1) return SoccerSetup.tab1;
  if(Tab == 2) return SoccerSetup.tab2;
  if(Tab == 3) return SoccerSetup.tab3;
  if(Tab == 4) return SoccerSetup.tab4;
  if(Tab == 5) return SoccerSetup.tab5;

  return 0;
}



int cSoccer::loadRanking(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;
}


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

  Skins.Message(mtStatus, tr("Reloading data..."));  
  
  file = DataDir;
  file = file + "/tabelle.txt";
  
  asprintf(&buffer, "%s%s \"%s\"", ScriptDir, "/getcontent.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();
    loadRanking(file.c_str());

    Display();

    }   
  free(buffer);
}


eOSState cSoccer::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. cSoccerInfos) dafuer zu erstellen und hier nur den "Index" auswerten.
// Anstatt 'return AdSubMenu...blahblah' dann hier 'return new cSoccerInfos(Index);
/*    if(strstr(item->Text(),"Bayern")) 
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Hamburger"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Bremen"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Schalke"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Hertha"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Dortmund"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Stuttgart"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Leverkusen"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Hannover"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"gladbach"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Frankfurt"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Nürnberg"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Bielefeld"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Mainz"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Wolfsburg"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Kaiserslautern"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Duisburg"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
    else if (strstr(item->Text(),"Köln"))
      return AddSubMenu(new cMenuText(tr("Club"), item->Text()));
*/      
    }
  return osContinue;
  
}

eOSState cSoccer::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 cResults());
	   break;
        case kGreen:	
           break;
        case kYellow:
	   reloadData();   	          
	   break;
        case kBlue:
	   return osEnd;
	   break;
	case kOk:
	   return Execute();
	   break;   
        default:
	   state = osContinue;
    }
  }

 return state;
}



// ---------- cResults --------------------------------------------------------------/

cResults::cResults()
:cMenuText(tr("Results"),"")
{
  std::string file;
  file = DataDir;
  file = file + "/ergebnisse.txt";

  SetHelp(NULL,NULL,NULL,NULL);


  if(loadResults(file.c_str())) {
    SetText(text.c_str());
    dsyslog("%s: Loaded results from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    }
  else {
    esyslog("%s: Can't load results from '%s'\n",PLUGIN_NAME_I18N, file.c_str());
    Skins.Message(mtError, tr("Error while loading results"));  
  }
  
  Display();
}

int cResults::loadResults(const char *file) {

  std::ifstream filestr;
  std::string line;
  
  bool result = false;
  
  text = "";

  filestr.open(file);
  if(filestr) {
    while (getline(filestr,line,'\n')) {
    text = text + line + "\n";
    }
    filestr.close();
    dsyslog("%s: Opened results '%s'\n",PLUGIN_NAME_I18N, file);
    result = true;
  }
  else {
    esyslog("%s: Can't open '%s'\n",PLUGIN_NAME_I18N, file);
    result = false;
    }

  return result; 
}

eOSState cResults::ProcessKey(eKeys Key)
{
  eOSState state = cMenuText::ProcessKey(Key);

  if(state==osUnknown) {
    switch (Key) {
        case kOk:
	    return osBack;
	   break;
        default:
	   state=osContinue;
    }
  }

 return state;
}

